Skip to content

Commit

Permalink
Add editor config
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed May 22, 2021
1 parent 50d65d2 commit 5fe4b19
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 39 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# top-most EditorConfig file
root = true

# all files
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
max_line_length = 80

[*.{js,ts}]
quote_type = single
curly_bracket_next_line = false
spaces_around_brackets = inside
indent_brace_style = BSD KNF

# HTML
[*.html]
quote_type = double
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ lerna-debug.log
.DS_Store
Thumbs.db

combined.json
combined.json
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ npm i
npm run link
```

## Usage
## Usage
`combine-json [inputDir] [outputFile(optionnal)]`
```bash
combine-json directory
```
default output file is combined.json at the root of where you execute the cli.

## Try it out
Go at the root of the CLI
Go at the root of the CLI
```bash
combine-json misc
```
Expand Down
58 changes: 56 additions & 2 deletions combined.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,61 @@
{ "name": "Hello" },
{ "name": "Hello" },
{ "name": "Hello" }
,{
,
{
"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": []
}
,
{
"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"
,{
"name": "not an array"
},
1,
Expand All @@ -20,4 +74,4 @@
{ "name": "world" },
{ "name": "world" },
{ "name": "world" }
]
]
2 changes: 1 addition & 1 deletion misc/far_away.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
{
"name": "far away"
}
]
]
2 changes: 1 addition & 1 deletion misc/hello.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
{ "name": "Hello" },
{ "name": "Hello" },
{ "name": "Hello" }
]
]
2 changes: 1 addition & 1 deletion misc/nested_array_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"Romance"
]
}
]
]
2 changes: 1 addition & 1 deletion misc/not_array.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"name": "not an array"
}
}
2 changes: 1 addition & 1 deletion misc/number.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
1,
2,
3
]
]
2 changes: 1 addition & 1 deletion misc/world.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
{ "name": "world" },
{ "name": "world" },
{ "name": "world" }
]
]
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ const combineJson = require('./combine-json');
console.error(e);
throw(e)
}
})()
})()
24 changes: 12 additions & 12 deletions src/combine-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
function findLastBracket(filePath, fd, buffer, position) {
let charRed = fs.readSync(fd, buffer, 0, 8, position);
let array = [...buffer].map(char => String.fromCharCode(char));
let bracket = array.indexOf("]");
let bracket = array.indexOf("]");
if (charRed === 0) return null;
if (bracket > -1) return position + bracket + 1;
fs.closeSync(fd)
Expand All @@ -26,13 +26,13 @@ function getLastBracket(filePath, fd, position) {
function findFirstBracketType(fileFd, buffer, position) {
let charRed = fs.readSync(fileFd, buffer, 0, 8)
let array = [...buffer].map(char => String.fromCharCode(char));
let curly = array.indexOf("{");
let bracket = array.indexOf("[");
let curly = array.indexOf("{");
let bracket = array.indexOf("[");
if (charRed === 0) return null;
if ((curly < bracket || bracket === -1) && curly > -1) return {
type: "{",
pos: position + curly
}
}
if ((bracket < curly || curly === -1) && bracket > -1) return {
type: "[",
pos: position + bracket
Expand All @@ -45,7 +45,7 @@ function getFirstBracketType(fd) {
return findFirstBracketType(fd, buffer, 0);
}

async function combine({inputFiles, inputDirPath, outputFilePath}) {
async function combine({inputFiles, inputDirPath, outputFilePath}) {
fs.writeFileSync(outputFilePath, "["); // start of new file
const numberOfFiles = inputFiles.length
numberOfFiles.map(( fileName, index) => {
Expand All @@ -56,7 +56,7 @@ async function combine({inputFiles, inputDirPath, outputFilePath}) {
flags: 'a'
});

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

})
for (let index = 0; index < numberOfFiles; index++) {
Expand All @@ -66,8 +66,8 @@ async function combine({inputFiles, inputDirPath, outputFilePath}) {
const fd = fs.openSync(`${inputDirPath}${file}`);
let firstBracketType = getFirstBracketType(fd);
let lastBracket = undefined;


if (firstBracketType) {
let isArray = firstBracketType.type === '[';
if (isArray) {
Expand All @@ -79,12 +79,12 @@ async function combine({inputFiles, inputDirPath, outputFilePath}) {
flags: 'a'
});
// open source file for reading
let start = (isArray) ? firstBracketType.pos + 1 : firstBracketType.pos;
let start = (isArray) ? firstBracketType.pos + 1 : firstBracketType.pos;
var r = fs.createReadStream(inputFile, {
start,
end: lastBracket
});

r.pipe(w);
const combineFiles = new Promise(function(resolve, reject) {
w.on('close', function() {
Expand All @@ -94,8 +94,8 @@ async function combine({inputFiles, inputDirPath, outputFilePath}) {
});
await combineFiles;

let last = (index === numberOfFiles - 1);

let last = (index === numberOfFiles - 1);

if (!last) {
let coma = path.resolve(__dirname, '../assets/coma')
Expand Down
4 changes: 2 additions & 2 deletions src/file_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function inputFilesAndDir({ inputDir }) {

function resolveOutputFilePath({ fileName }) {
const workingDir = process.cwd();
const outputFilePath = outputFile({ dirName: workingDir, fileName});
const outputFilePath = outputFile({ dirName: workingDir, fileName});
createFileIfNotExist(outputFilePath)
return outputFilePath
}
Expand All @@ -53,4 +53,4 @@ module.exports = {
resolveOutputFilePath,
createFileIfNotExist,
filterNonJson
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const combineJson = require('./combine-json')


module.exports = combineJson
module.exports = combineJson
24 changes: 12 additions & 12 deletions src/slow-combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ function listOfJsonFiles(files){


async function combineJson(files, dir, outputDir) {
let outputFile = outputPath(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

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

let inputFile = `${filePath}${file}`;
let content = require(inputFile);
content = JSON.stringify(content);
if (Array.isArray(content)) content = content.substr(1, content.length - 2);
let last = (index === numberOfFiles - 1);
let last = (index === numberOfFiles - 1);
fs.appendFileSync(outputFile, `${content}${(last)? "" : ","}\n`);
console.log(chalk.green(
'file: ' +
chalk.blue.underline.bold(file) +
` has been added! last : ${last}, index: ${index}, numberOfFiles: ${numberOfFiles}`
))

}
fs.appendFileSync(outputFile, `]`);
}
Expand All @@ -65,14 +65,14 @@ function determineDir(dir) {
(async () => {
if (process.argv.length === 2) {
console.log(chalk.red('Error: Missing path argument'));
}
}
else {
try {
let dir = determineDir(process.argv[2])
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)
Expand Down

0 comments on commit 5fe4b19

Please sign in to comment.