Skip to content

Commit dcca861

Browse files
authored
Merge pull request #5 from codelicia/accept-directories
Accept directories
2 parents 4d1406a + 575f800 commit dcca861

14 files changed

+328
-64
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
test/fixture/actual-json/temp_json_file.json

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ before_script:
1111

1212
script:
1313
- npm test
14-
- ./node_modules/.bin/eslint ./lib
14+
- npm run lint

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Verify if the code style is fine:
1313

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

16-
Fix the code style of a file:
16+
Fix the code style of a file or multiple files:
1717

18-
./node_modules/.bin/jsoncs --fix my/file.json
18+
./node_modules/.bin/jsoncs --fix my/directory/
1919

2020
### Options
2121

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

2626
Options:
2727
-f, --fix Fix the file
28+
-q, --quiet Run in quiet mode
2829
-v, --version print version and exit
2930

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

3738
## TO-DO
3839

39-
* Make it accept multiple files
4040
* Leave the code style to the user in a config file

lib/cli.js

+20-28
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,52 @@ const path = require("path");
55
const formatter = require("./formatter.js").formatter;
66
const commander = require("commander");
77
const packageVersion = require("../package").version;
8-
const jsdiff = require("diff");
8+
const { getFiles, fixJsonFile, showDiff } = require("./modules.js");
99

1010
const program = new commander.Command();
1111

1212
let jsonFile = "";
13-
let hasErrors = false;
13+
let errors = [];
14+
let fixedFiles = [];
1415

1516
program.
1617
version(packageVersion,
1718
"-v, --version",
1819
"json code style version")
1920
.option("-f, --fix",
2021
"fix json file")
22+
.option("-q, --quiet",
23+
"quiet mode")
2124
.arguments("[json]")
2225
.action(function (json) {
2326
jsonFile = json;
2427
});
2528

2629
program.parse(process.argv);
2730

28-
function parse(filePath) {
29-
let source = fs.readFileSync(filePath, "utf8");
30-
31-
let formatted = formatter.formatJson(source);
32-
let diff = jsdiff.diffJson(source, formatted);
31+
function parse(path) {
32+
getFiles(path).map(processFile);
3333

34-
showDiff(diff);
35-
fixJson(filePath, formatted);
36-
37-
if (hasErrors) {
34+
if (errors.includes(true)) {
3835
process.exit(1);
3936
}
40-
37+
38+
!program.fix || console.log(fixedFiles);
4139
process.exit(0);
4240
}
4341

44-
function fixJson(filePath, formatted) {
45-
if (!program.fix) {
46-
return;
47-
}
42+
function processFile(filePath) {
43+
let source = fs.readFileSync(filePath, "utf8");
44+
let formatted = formatter.formatJson(source);
45+
let fileWithErrors = showDiff(program.quiet, source, formatted, filePath);
4846

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

54-
function showDiff(diff) {
55-
diff.forEach((part) => {
56-
let color = part.added ? "\x1b[34m" : part.removed ? "\x1b[31m" : "\x1b[37m";
57-
if (part.added || part.removed) {
58-
console.log(color, part.value);
59-
hasErrors = true;
60-
}
61-
});
49+
if (program.fix) {
50+
let fixedFile = fixJsonFile(filePath, formatted);
51+
fixedFiles.push(fixedFile);
52+
errors = [];
53+
}
6254
}
6355

6456
function main(jsonFilePath) {

lib/modules.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fs = require("fs");
2+
const jsdiff = require("diff");
3+
4+
const blue = "\x1b[34m";
5+
const red = "\x1b[31m";
6+
const withe = "\x1b[37m";
7+
8+
module.exports = {
9+
getFiles: (path) => {
10+
if (fs.lstatSync(path).isDirectory()) {
11+
return readDirectory(path);
12+
}
13+
14+
return [path];
15+
},
16+
fixJsonFile: (filePath, formatted) => {
17+
fs.writeFileSync(filePath, formatted);
18+
19+
return filePath + " - has been fixed";
20+
},
21+
showDiff: (quiet, source, formatted, filePath) => {
22+
let fileWithErrors = false;
23+
let diff = jsdiff.diffJson(source, formatted);
24+
25+
diff.forEach((part) => {
26+
let color = part.added ? blue : part.removed ? red : withe;
27+
28+
if (part.added || part.removed) {
29+
quiet || console.log(color, part.value);
30+
fileWithErrors = true;
31+
}
32+
});
33+
34+
!fileWithErrors || console.log(withe, " ============== ");
35+
!fileWithErrors || console.log(withe, filePath, "\n");
36+
37+
return fileWithErrors;
38+
}
39+
};
40+
41+
function readDirectory(directoryPath) {
42+
let files = fs.readdirSync(directoryPath);
43+
let filesPath = [];
44+
45+
files.forEach((file) => {
46+
filesPath.push(directoryPath + file);
47+
});
48+
49+
return filesPath;
50+
}

0 commit comments

Comments
 (0)