Skip to content

Commit d647b8a

Browse files
authored
Merge pull request #9 from codelicia/glob
Use Glob Pattern
2 parents 096c2c5 + f338de2 commit d647b8a

File tree

6 files changed

+53
-46
lines changed

6 files changed

+53
-46
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ Install jsoncs with npm to use the command line interface:
1111

1212
Verify if the code style is fine:
1313

14-
./node_modules/.bin/jsoncs my/file.json
14+
./node_modules/.bin/jsoncs 'my/file.json'
15+
16+
or to multiple files
17+
18+
./node_modules/.bin/jsoncs 'my/*.json'
1519

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

18-
./node_modules/.bin/jsoncs --fix my/directory/
22+
./node_modules/.bin/jsoncs --fix 'my/directory/*'
1923

2024
### Options
2125

lib/cli.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,32 @@ program.parse(process.argv);
3131
function parse(path) {
3232
getFiles(path).map(processFile);
3333

34+
!program.fix || console.log(fixedFiles);
35+
3436
if (errors.includes(true)) {
3537
process.exit(1);
3638
}
3739

38-
!program.fix || console.log(fixedFiles);
3940
process.exit(0);
4041
}
4142

4243
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);
44+
try {
45+
const source = fs.readFileSync(filePath, "utf8");
46+
const formatted = formatter.formatJson(source);
47+
const fileWithErrors = showDiff(program.quiet, source, formatted, filePath);
4648

47-
errors.push(fileWithErrors);
49+
errors.push(fileWithErrors);
4850

49-
if (program.fix) {
50-
let fixedFile = fixJsonFile(filePath, formatted);
51+
let fixedFile = fixJsonFile(program.fix, filePath, formatted);
5152
fixedFiles.push(fixedFile);
52-
errors = [];
53+
} catch (e) {
54+
console.log("Couldn't read the file / directory");
55+
console.log("Try to:");
56+
console.log(" - use quotes (eg: jsoncs 'my/file.json')");
57+
console.log(" - use glob pattern (eg: jsoncs 'my/*.json')");
58+
console.log(" - use glob pattern (eg: jsoncs 'my/*/*.json')");
59+
process.exit(1);
5360
}
5461
}
5562

lib/modules.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
const fs = require("fs");
22
const jsdiff = require("diff");
3+
const glob = require("glob");
34

45
const blue = "\x1b[34m";
56
const red = "\x1b[31m";
67
const withe = "\x1b[37m";
78

89
module.exports = {
910
getFiles: (path) => {
10-
if (fs.lstatSync(path).isDirectory()) {
11-
return readDirectory(path);
11+
return glob.sync(path);
12+
},
13+
fixJsonFile: (shouldFix, filePath, formatted) => {
14+
if (!shouldFix) {
15+
return;
1216
}
1317

14-
return [path];
15-
},
16-
fixJsonFile: (filePath, formatted) => {
1718
fs.writeFileSync(filePath, formatted);
1819

1920
return filePath + " - has been fixed";
@@ -37,14 +38,3 @@ module.exports = {
3738
return fileWithErrors;
3839
}
3940
};
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-
}

package-lock.json

+6-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
},
2727
"dependencies": {
2828
"commander": "^3.0.0",
29-
"diff": "^4.0.1"
29+
"diff": "^4.0.1",
30+
"glob": "^7.1.4"
3031
},
3132
"devDependencies": {
3233
"eslint": "^6.2.1",

test/modulesTest.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ const { fixJsonFile, getFiles, showDiff } = require("../lib/modules.js");
44

55
test("test write formatted json on the file", (assert) => {
66
let path = __dirname + "/fixture/actual-json/temp_json_file.json";
7-
let result = fixJsonFile(path, "{a}");
7+
let result = fixJsonFile(true, path, "{a}");
88
let file = fs.readFileSync(path).toString();
99

1010
assert.equal(path + " - has been fixed", result);
1111
assert.equal("{a}", file);
1212
assert.end();
1313
});
1414

15+
test("test not write formatted json on the file", (assert) => {
16+
let path = __dirname + "/fixture/actual-json/temp_json_file.json";
17+
let result = fixJsonFile(false, path, "{a}");
18+
19+
assert.equal(undefined, result);
20+
assert.end();
21+
});
22+
1523
test("test get file path", (assert) => {
1624
let path = __dirname + "/fixture/actual-json/temp_json_file.json";
1725
fs.writeFileSync(path, "");
@@ -23,13 +31,21 @@ test("test get file path", (assert) => {
2331
});
2432

2533
test("test get directory path", (assert) => {
26-
let path = __dirname + "/fixture/expected-json/";
34+
let path = __dirname + "/fixture/expected-json/*.json";
2735
let result = getFiles(path);
2836

2937
assert.equal(result.length, 2);
3038
assert.end();
3139
});
3240

41+
test("test get all files inside a directory path", (assert) => {
42+
let path = __dirname + "/fixture/*/*.json";
43+
let result = getFiles(path);
44+
45+
assert.equal(result.length, 5);
46+
assert.end();
47+
});
48+
3349
test("test show diff between two different json objects", (assert) => {
3450
let json1 = {foo:"bar"};
3551
let json2 = {ble:"bar"};

0 commit comments

Comments
 (0)