Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed May 22, 2021
1 parent 1921097 commit 50d65d2
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Combine-json

Combine-json is a CLI tool that combines all json files found in a directory into one big json file.
Combine-json is a CLI tool that combines all json files found in a directory into one big json file using streaming.

It takes as argument a directory in which to find the json files and a output file name in which the json files will combined.
The json file will be created in the directory where the CLI is used.
Expand Down
34 changes: 34 additions & 0 deletions misc/nested_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"id": "106678",
"title": "Jungle Erotic",
"poster": "https://image.tmdb.org/t/p/w1280/rkMHlp42CDJUEB4GKsn3LC5znGC.jpg",
"overview": "Young women have an adventure in the African jungle - where they are harassed by gentle men, and wild monkeys.",
"release_date": 26265600,
"genre": [
"Adventure",
"Drama"
]
},
{
"id": "490410",
"title": "48 Christmas Wishes",
"poster": "https://image.tmdb.org/t/p/w1280/mrcA62O9j8y4gsSpftMKVcvrzCI.jpg",
"overview": "Holiday magic has never gleamed brighter as Santa’s Elves head out on an adventure to bring every boy and girl their Christmas wish while also bringing a family back together in this heartfelt Christmas movie from Gaumont. After accidentally destroying a bag of Christmas wishes from a small town on the eve of Christmas Eve, two junior elves learn that if even one wish goes unfulfilled, Christmas could be extinguished forever. For the first time in their lives, the elves Mindy and Cam venture out of the North Pole and sneak into Minnedoza to collect the lost wishes. Along the way they enlist the help of young Blake, whose family has a difficult time celebrating Christmas since his father died. With their deadline fast approaching, Mindy and Cam have only one more wish to find…but whose could it be? It’s up to Mindy and Cam to find out and save Christmas!",
"release_date": 1512086400,
"genre": [
"Family",
"Adventure",
"Comedy",
"TV Movie"
]
},
{
"id": "285841",
"title": "Elephant Song",
"poster": "https://image.tmdb.org/t/p/w1280/9pHl6RmMIDyPmQW1XUJ2Mahsr3M.jpg",
"overview": "A psychiatrist is drawn into a complex mind game when he questions a disturbed patient about the disappearance of a colleague.",
"release_date": 1409792400,
"genre": []
}
]
24 changes: 24 additions & 0 deletions misc/nested_array_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"id": "481370",
"title": "Bigger",
"poster": "https://image.tmdb.org/t/p/w1280/f6rmiFTfT3Rf9XDrXfLO6XcAgWn.jpg",
"overview": "The inspirational tale of the grandfathers of the fitness movement as we now know it, Joe & Ben Weider. Battling anti-Semitism, racism and extreme poverty, the brothers beat all odds to build an empire & inspire future generations.",
"release_date": 1539392400,
"genre": [
"Drama"
]
},
{
"id": "31357",
"title": "Waiting to Exhale",
"poster": "https://image.tmdb.org/t/p/w1280/4wjGMwPsdlvi025ZqR4rXnFDvBz.jpg",
"overview": "Cheated on, mistreated and stepped on, the women are holding their breath, waiting for the elusive 'good man' to break a string of less-than-stellar lovers. Friends and confidants Vannah, Bernie, Glo and Robin talk it all out, determined to find a better way to breathe.",
"release_date": 819590400,
"genre": [
"Comedy",
"Drama",
"Romance"
]
}
]
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "combine-json",
"version": "1.0.0",
"description": "",
"main": "src/combine-json.js",
"main": "src/index.js",
"preferGlobal": true,
"bin": "./src/combine-json.js",
"bin": "./src/cli.js",
"scripts": {
"link": "npm link",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "./src/cli.js misc"
},
"author": "",
"license": "ISC",
Expand Down
18 changes: 18 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node
const combineJson = require('./combine-json');

(async () => {
console.log(combineJson);
try {
if (process.argv.length === 2) {
console.log(chalk.red('Error: Missing path argument'));
}
else {
await combineJson(process.argv[2], process.argv[3])
}
}
catch(e) {
console.error(e);
throw(e)
}
})()
98 changes: 35 additions & 63 deletions src/combine-json.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
#!/usr/bin/env node
const fs = require('fs');
const chalk = require('chalk');
const path = require('path')

function outputPath(outputDir) {
let fileName = (process.argv[3]) ? process.argv[3] : 'combined.json';
return `${outputDir}/${fileName}`;
}

function createIfNotExist(file) {
if (fs.existsSync(file)) {
fs.writeFileSync(file, "");
}
}

function listOfJsonFiles(files){
return files.reduce((acc, file)=>{
if (path.extname(file)=== '.json') return [...acc, file];
return acc;
}, []);
}
const {
inputFilesAndDir,
resolveOutputFilePath,
filterNonJson
} = require('./file_utils')

function findLastBracket(filePath, fd, buffer, position) {
let charRed = fs.readSync(fd, buffer, 0, 8, position);
Expand Down Expand Up @@ -59,22 +45,25 @@ function getFirstBracketType(fd) {
return findFirstBracketType(fd, buffer, 0);
}

async function combineJson(files, dir, outputDir) {

let outputFile = outputPath(outputDir);
let filePath = dir + ((dir[dir.length - 1] === '/') ? '' : '/') // add slash at the end of the dir if it is not there yet
createIfNotExist(outputFile)

fs.writeFileSync(outputFile, "["); // start of new file
const jsonFiles = listOfJsonFiles(files);
const numberOfFiles = jsonFiles.length
async function combine({inputFiles, inputDirPath, outputFilePath}) {
fs.writeFileSync(outputFilePath, "["); // start of new file
const numberOfFiles = inputFiles.length
numberOfFiles.map(( fileName, index) => {
let inputFile = `${inputDirPath}${fileName}`;

// open destination file for appending
const writeStreamPath = fs.createWriteStream(outputFilePath, {
flags: 'a'
});

let start = (isArray) ? firstBracketType.pos + 1 : firstBracketType.pos;

})
for (let index = 0; index < numberOfFiles; index++) {
let file = jsonFiles[index];
let inputFile = `${filePath}${file}`;
// console.log({ inputFile });

// let content = require(inputFile);
const fd = fs.openSync(`${filePath}${file}`);
let file = inputFiles[index];
let inputFile = `${inputDirPath}${file}`;

const fd = fs.openSync(`${inputDirPath}${file}`);
let firstBracketType = getFirstBracketType(fd);
let lastBracket = undefined;

Expand All @@ -86,7 +75,7 @@ async function combineJson(files, dir, outputDir) {
lastBracket = getLastBracket(inputFile, fd, stats.size - 8) - 2;
}
// open destination file for appending
var w = fs.createWriteStream(outputFile, {
var w = fs.createWriteStream(outputFilePath, {
flags: 'a'
});
// open source file for reading
Expand All @@ -110,7 +99,7 @@ async function combineJson(files, dir, outputDir) {

if (!last) {
let coma = path.resolve(__dirname, '../assets/coma')
let comaWrite = fs.createWriteStream(outputFile, {
let comaWrite = fs.createWriteStream(outputFilePath, {
flags: 'a'
});
let comaRead = fs.createReadStream(coma);
Expand All @@ -124,7 +113,7 @@ async function combineJson(files, dir, outputDir) {
await addComa
} else {
let closingBracket = path.resolve(__dirname, '../assets/closing_bracket');
let closingBracketWrite = fs.createWriteStream(outputFile, {
let closingBracketWrite = fs.createWriteStream(outputFilePath, {
flags: 'a'
});
let closingBracketRead = fs.createReadStream(closingBracket);
Expand All @@ -147,34 +136,17 @@ async function combineJson(files, dir, outputDir) {
}


function determineDir(dir) {
dir = path.resolve(dir);
if (!fs.existsSync(dir)) { // test for Fully Qualified path
console.log(`Error: ${process.argv[2]} no such named directory`);
process.exit()
async function combineJson(inputDir, outputFile = undefined) {
try {
const { inputDirPath, filesName } = inputFilesAndDir({ inputDir })
const outputFilePath = resolveOutputFilePath({ fileName: outputFile })
const inputFiles = filterNonJson({ filesName });
await combine({inputFiles, inputDirPath, outputFilePath})
}catch(e) {
throw(e)
}
return dir;
}

(async () => {
console.log();


if (process.argv.length === 2) {
console.log(chalk.red('Error: Missing path argument'));
}
else {
try {
let dir = determineDir(process.argv[2])
console.log({ dir });
let outputDir = process.cwd();
let files = fs.readdirSync(dir)

await combineJson(files, dir, outputDir)
}catch(e) {
throw(e)
}
}
})()
module.exports = combineJson


56 changes: 56 additions & 0 deletions src/file_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const path = require('path')
const fs = require('fs')

function resolveDir({ dir }) {
dir = path.resolve(dir);
if (!fs.existsSync(dir)) { // test for Fully Qualified path
console.log(`Error: ${dir} no such named directory`);
process.exit()
}
return dir;
}

function outputFile({ dirName, fileName = undefined }) {
fileName = fileName || 'combined.json';
return `${dirName}/${fileName}`;
}

function inputFilesAndDir({ inputDir }) {
const resolvedDir = resolveDir({ dir: inputDir})
const inputDirPath = resolvedDir + ((resolvedDir[resolvedDir.length - 1] === '/') ? '' : '/') // add slash at the end of the dir if it is not there yet
const filesName = fs.readdirSync(inputDirPath) // read all files names in dir
return {
inputDirPath,
filesName
}
}

function resolveOutputFilePath({ fileName }) {
const workingDir = process.cwd();
const outputFilePath = outputFile({ dirName: workingDir, fileName});
createFileIfNotExist(outputFilePath)
return outputFilePath
}


function createFileIfNotExist({ file }) {
if (fs.existsSync(file)) {
fs.writeFileSync(file, "");
}
}

function filterNonJson({ filesName }){
return filesName.reduce((acc, file)=>{
if (path.extname(file)=== '.json') return [...acc, file];
return acc;
}, []);
}

module.exports = {
resolveDir,
outputFile,
inputFilesAndDir,
resolveOutputFilePath,
createFileIfNotExist,
filterNonJson
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const combineJson = require('./combine-json')


module.exports = combineJson

0 comments on commit 50d65d2

Please sign in to comment.