Skip to content

Commit

Permalink
Merge pull request #5 from codelicia/accept-directories
Browse files Browse the repository at this point in the history
Accept directories
  • Loading branch information
airtonzanon committed Aug 29, 2019
2 parents 4d1406a + 575f800 commit dcca861
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
test/fixture/actual-json/temp_json_file.json
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ before_script:

script:
- npm test
- ./node_modules/.bin/eslint ./lib
- npm run lint
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Verify if the code style is fine:

./node_modules/.bin/jsoncs my/file.json

Fix the code style of a file:
Fix the code style of a file or multiple files:

./node_modules/.bin/jsoncs --fix my/file.json
./node_modules/.bin/jsoncs --fix my/directory/

### Options

Expand All @@ -25,6 +25,7 @@ Fix the code style of a file:

Options:
-f, --fix Fix the file
-q, --quiet Run in quiet mode
-v, --version print version and exit

### Example
Expand All @@ -36,5 +37,4 @@ Fix the code style of a file:

## TO-DO

* Make it accept multiple files
* Leave the code style to the user in a config file
48 changes: 20 additions & 28 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,52 @@ const path = require("path");
const formatter = require("./formatter.js").formatter;
const commander = require("commander");
const packageVersion = require("../package").version;
const jsdiff = require("diff");
const { getFiles, fixJsonFile, showDiff } = require("./modules.js");

const program = new commander.Command();

let jsonFile = "";
let hasErrors = false;
let errors = [];
let fixedFiles = [];

program.
version(packageVersion,
"-v, --version",
"json code style version")
.option("-f, --fix",
"fix json file")
.option("-q, --quiet",
"quiet mode")
.arguments("[json]")
.action(function (json) {
jsonFile = json;
});

program.parse(process.argv);

function parse(filePath) {
let source = fs.readFileSync(filePath, "utf8");

let formatted = formatter.formatJson(source);
let diff = jsdiff.diffJson(source, formatted);
function parse(path) {
getFiles(path).map(processFile);

showDiff(diff);
fixJson(filePath, formatted);

if (hasErrors) {
if (errors.includes(true)) {
process.exit(1);
}


!program.fix || console.log(fixedFiles);
process.exit(0);
}

function fixJson(filePath, formatted) {
if (!program.fix) {
return;
}
function processFile(filePath) {
let source = fs.readFileSync(filePath, "utf8");
let formatted = formatter.formatJson(source);
let fileWithErrors = showDiff(program.quiet, source, formatted, filePath);

fs.writeFileSync(filePath, formatted);
console.log(filePath + " - has been fixed");
hasErrors = false;
}
errors.push(fileWithErrors);

function showDiff(diff) {
diff.forEach((part) => {
let color = part.added ? "\x1b[34m" : part.removed ? "\x1b[31m" : "\x1b[37m";
if (part.added || part.removed) {
console.log(color, part.value);
hasErrors = true;
}
});
if (program.fix) {
let fixedFile = fixJsonFile(filePath, formatted);
fixedFiles.push(fixedFile);
errors = [];
}
}

function main(jsonFilePath) {
Expand Down
50 changes: 50 additions & 0 deletions lib/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require("fs");
const jsdiff = require("diff");

const blue = "\x1b[34m";
const red = "\x1b[31m";
const withe = "\x1b[37m";

module.exports = {
getFiles: (path) => {
if (fs.lstatSync(path).isDirectory()) {
return readDirectory(path);
}

return [path];
},
fixJsonFile: (filePath, formatted) => {
fs.writeFileSync(filePath, formatted);

return filePath + " - has been fixed";
},
showDiff: (quiet, source, formatted, filePath) => {
let fileWithErrors = false;
let diff = jsdiff.diffJson(source, formatted);

diff.forEach((part) => {
let color = part.added ? blue : part.removed ? red : withe;

if (part.added || part.removed) {
quiet || console.log(color, part.value);
fileWithErrors = true;
}
});

!fileWithErrors || console.log(withe, " ============== ");
!fileWithErrors || console.log(withe, filePath, "\n");

return fileWithErrors;
}
};

function readDirectory(directoryPath) {
let files = fs.readdirSync(directoryPath);
let filesPath = [];

files.forEach((file) => {
filesPath.push(directoryPath + file);
});

return filesPath;
}
Loading

0 comments on commit dcca861

Please sign in to comment.