Skip to content

Commit 8473e4c

Browse files
authored
Merge pull request #45 from codelicia/user_choose_indentation
Let user choose indentation
2 parents c3c7b0a + 17dae8b commit 8473e4c

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

README.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ Fix the code style of a file or multiple files:
2828
Usage: jsoncs [file]
2929

3030
Options:
31-
-f, --fix Fix the file
32-
-q, --quiet Run in quiet mode
33-
-v, --version print version and exit
31+
-f, --fix fix json file
32+
-q, --quiet quiet mode
33+
-v, --version json code style version
34+
-s, --spaces <integer> quantity of spaces to indent the json (default: 2)
3435

3536
### Example
3637

3738
* _RED_: wrong json
3839
* _BLUE_: correct json
3940

4041
![](./example.png)
41-
42-
## TO-DO
43-
44-
* Leave the code style to the user in a config file

lib/cli.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ program.
2121
"fix json file")
2222
.option("-q, --quiet",
2323
"quiet mode")
24+
.option("-s, --spaces <integer>",
25+
"quantity of spaces to indent the json",
26+
2)
2427
.arguments("[json]")
2528
.action(function (json) {
2629
jsonFile = json;
@@ -36,21 +39,27 @@ function parse(path) {
3639
if (errors.includes(true)) {
3740
process.exit(1);
3841
}
39-
42+
4043
process.exit(0);
4144
}
4245

4346
function processFile(filePath) {
4447
try {
4548
const source = fs.readFileSync(filePath, "utf8");
46-
const formatted = formatter.formatJson(source);
49+
const indentation = parseInt(program.spaces);
50+
const formatted = formatter.formatJson(source, indentation);
4751
const fileWithErrors = showDiff(program.quiet, source, formatted, filePath);
4852

4953
errors.push(fileWithErrors);
5054

5155
let fixedFile = fixJsonFile(program.fix, filePath, formatted);
5256
fixedFiles.push(fixedFile);
5357
} catch (e) {
58+
if (typeof program.spaces != "number") {
59+
console.log("-s / --spaces should receive integer as parameter");
60+
process.exit(1);
61+
}
62+
5463
console.log("Couldn't read the file / directory");
5564
console.log("Try to:");
5665
console.log(" - use quotes (eg: jsoncs 'my/file.json')");
@@ -61,6 +70,9 @@ function processFile(filePath) {
6170
}
6271

6372
function main(jsonFilePath) {
73+
if (jsonFilePath == undefined) {
74+
program.help();
75+
}
6476
let filePath = path.normalize(jsonFilePath);
6577
parse(filePath);
6678
}

lib/formatter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ var formatter = (function () {
66
return new Array(count + 1).join(s);
77
}
88

9-
function formatJson(json) {
9+
function formatJson(json, indentation) {
1010
var i = 0,
1111
il = 0,
12-
tab = " ",
12+
tab = repeat(" ", indentation),
1313
newJson = "",
1414
indentLevel = 0,
1515
inString = false,

lib/modules.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const glob = require("glob");
44

55
const blue = "\x1b[34m";
66
const red = "\x1b[31m";
7-
const withe = "\x1b[37m";
7+
const white = "\x1b[37m";
88

99
module.exports = {
1010
getFiles: (path) => {
@@ -24,16 +24,16 @@ module.exports = {
2424
let diff = jsdiff.diffJson(source, formatted);
2525

2626
diff.forEach((part) => {
27-
let color = part.added ? blue : part.removed ? red : withe;
27+
let color = part.added ? blue : part.removed ? red : white;
2828

2929
if (part.added || part.removed) {
3030
quiet || console.log(color, part.value);
3131
fileWithErrors = true;
3232
}
3333
});
3434

35-
!fileWithErrors || console.log(withe, " ============== ");
36-
!fileWithErrors || console.log(withe, filePath, "\n");
35+
!fileWithErrors || console.log(white, " ============== ");
36+
!fileWithErrors || console.log(white, filePath, "\n");
3737

3838
return fileWithErrors;
3939
}

test/formatterTest.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ const formatter = require("../lib/formatter.js").formatter;
55
test("format a simple json file", (assert) => {
66
let actualJson = fs.readFileSync(__dirname + "/fixture/actual-json/1.json").toString();
77
let expectedJson = fs.readFileSync(__dirname + "/fixture/expected-json/1.json").toString();
8-
assert.equal(formatter.formatJson(actualJson), expectedJson);
8+
const indetation = 2;
9+
assert.equal(formatter.formatJson(actualJson, indetation), expectedJson);
910
assert.end();
1011
});
1112

1213
test("format a complex json file", (assert) => {
1314
let actualJson = fs.readFileSync(__dirname + "/fixture/actual-json/2.json").toString();
1415
let expectedJson = fs.readFileSync(__dirname + "/fixture/expected-json/2.json").toString();
15-
assert.equal(formatter.formatJson(actualJson), expectedJson);
16+
const indetation = 2;
17+
assert.equal(formatter.formatJson(actualJson, indetation), expectedJson);
1618
assert.end();
1719
});

0 commit comments

Comments
 (0)