Skip to content

Commit

Permalink
Lint code
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed May 22, 2021
1 parent 7d25934 commit cdbcc6e
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 281 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ lerna-debug.log
Thumbs.db

combined.json

# Ignore artifacts:
build
coverage
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
combined.json
/misc
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
57 changes: 0 additions & 57 deletions package-lock.json

This file was deleted.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
"bin": "./src/cli.js",
"scripts": {
"link": "npm link",
"lint": "prettier --write .",
"test": "./src/cli.js misc"
},
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^3.0.0"
},
"devDependencies": {
"prettier": "2.3.0"
}
}
16 changes: 7 additions & 9 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
const combineJson = require('./combine-json');

(async () => {
console.log(combineJson);
console.log(combineJson);
try {
if (process.argv.length === 2) {
console.log(chalk.red('Error: Missing path argument'));
console.log(chalk.red('Error: Missing path argument'));
} else {
await combineJson(process.argv[2], process.argv[3]);
}
else {
await combineJson(process.argv[2], process.argv[3])
}
}
catch(e) {
} catch (e) {
console.error(e);
throw(e)
throw e;
}
})()
})();
255 changes: 128 additions & 127 deletions src/combine-json.js
Original file line number Diff line number Diff line change
@@ -1,152 +1,153 @@
const fs = require('fs');
const chalk = require('chalk');
const path = require('path')
const path = require('path');
const {
inputFilesAndDir,
resolveOutputFilePath,
filterNonJson
} = require('./file_utils')
inputFilesAndDir,
resolveOutputFilePath,
filterNonJson,
} = require('./file_utils');

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("]");
if (charRed === 0) return null;
if (bracket > -1) return position + bracket + 1;
fs.closeSync(fd)
fd = fs.openSync(filePath);
return getLastBracket(filePath, fd, position - 8)
let charRed = fs.readSync(fd, buffer, 0, 8, position);
let array = [...buffer].map((char) => String.fromCharCode(char));
let bracket = array.indexOf(']');
if (charRed === 0) return null;
if (bracket > -1) return position + bracket + 1;
fs.closeSync(fd);
fd = fs.openSync(filePath);
return getLastBracket(filePath, fd, position - 8);
}

function getLastBracket(filePath, fd, position) {
let buffer = new Int8Array(8);
return findLastBracket(filePath, fd, buffer, position);
let buffer = new Int8Array(8);
return findLastBracket(filePath, fd, buffer, 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("[");
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
}
return findFirstBracketType(fileFd, buffer, position + 8)
let charRed = fs.readSync(fileFd, buffer, 0, 8);
let array = [...buffer].map((char) => String.fromCharCode(char));
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,
};
return findFirstBracketType(fileFd, buffer, position + 8);
}

function getFirstBracketType(fd) {
let buffer = new Int8Array(8);
return findFirstBracketType(fd, buffer, 0);
let buffer = new Int8Array(8);
return findFirstBracketType(fd, buffer, 0);
}

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'
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 = inputFiles[index];
let inputFile = `${inputDirPath}${file}`;

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

if (firstBracketType) {
let isArray = firstBracketType.type === '[';
if (isArray) {
let stats = fs.statSync(inputFile);
lastBracket = getLastBracket(inputFile, fd, stats.size - 8) - 2;
}
// open destination file for appending
var w = fs.createWriteStream(outputFilePath, {
flags: 'a',
});
// open source file for reading
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 () {
resolve('foo');
console.log('done writing');
});
});
await combineFiles;

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

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

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


if (firstBracketType) {
let isArray = firstBracketType.type === '[';
if (isArray) {
let stats = fs.statSync(inputFile)
lastBracket = getLastBracket(inputFile, fd, stats.size - 8) - 2;
}
// open destination file for appending
var w = fs.createWriteStream(outputFilePath, {
flags: 'a'
});
// open source file for reading
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() {
resolve('foo');
console.log("done writing");
});
});
await combineFiles;

let last = index === numberOfFiles - 1;

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

if (!last) {
let coma = path.resolve(__dirname, '../assets/coma')
let comaWrite = fs.createWriteStream(outputFilePath, {
flags: 'a'
});
let comaRead = fs.createReadStream(coma);
comaRead.pipe(comaWrite);
const addComa = new Promise(function(resolve, reject) {
comaWrite.on('close', function() {
resolve('foo');
console.log("done writing coma");
});
});
await addComa
} else {
let closingBracket = path.resolve(__dirname, '../assets/closing_bracket');
let closingBracketWrite = fs.createWriteStream(outputFilePath, {
flags: 'a'
});
let closingBracketRead = fs.createReadStream(closingBracket);
closingBracketRead.pipe(closingBracketWrite);
const addclosingBracket = new Promise(function(resolve, reject) {
closingBracketWrite.on('close', function() {
resolve('foo');
console.log("done writing closingBracket");
});
});
await addclosingBracket
}
console.log(chalk.green(
'file: ' +
chalk.blue.underline.bold(file) +
` has been added! last : ${last}, index: ${index}, numberOfFiles: ${numberOfFiles}`
))
}
if (!last) {
let coma = path.resolve(__dirname, '../assets/coma');
let comaWrite = fs.createWriteStream(outputFilePath, {
flags: 'a',
});
let comaRead = fs.createReadStream(coma);
comaRead.pipe(comaWrite);
const addComa = new Promise(function (resolve, reject) {
comaWrite.on('close', function () {
resolve('foo');
console.log('done writing coma');
});
});
await addComa;
} else {
let closingBracket = path.resolve(
__dirname,
'../assets/closing_bracket'
);
let closingBracketWrite = fs.createWriteStream(outputFilePath, {
flags: 'a',
});
let closingBracketRead = fs.createReadStream(closingBracket);
closingBracketRead.pipe(closingBracketWrite);
const addclosingBracket = new Promise(function (resolve, reject) {
closingBracketWrite.on('close', function () {
resolve('foo');
console.log('done writing closingBracket');
});
});
await addclosingBracket;
}
console.log(
chalk.green(
'file: ' +
chalk.blue.underline.bold(file) +
` has been added! last : ${last}, index: ${index}, numberOfFiles: ${numberOfFiles}`
)
);
}
}
}


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)
}
try {
const { inputDirPath, filesName } = inputFilesAndDir({ inputDir });
const outputFilePath = resolveOutputFilePath({ fileName: outputFile });
const inputFiles = filterNonJson({ filesName });
await combine({ inputFiles, inputDirPath, outputFilePath });
} catch (e) {
throw e;
}
}

module.exports = combineJson


module.exports = combineJson;
Loading

0 comments on commit cdbcc6e

Please sign in to comment.