Comparing version 0.6.1 to 0.7.0
38
index.js
#!/usr/bin/env node | ||
var assert = require("assert"); | ||
var fs = require("fs"); | ||
var sortJson = require("good-sort-json"); | ||
var getStdin = require("get-stdin"); | ||
var jsonic = require("jsonic"); | ||
var program = require("commander"); | ||
var sortJson = require("good-sort-json"); | ||
var packagejson = require("./package.json"); | ||
var getStdin = require("get-stdin") | ||
var inflect = require("inflect-json"); | ||
var allowedInflection = ["none", "pascalcase"]; | ||
program | ||
.version(packagejson.version) | ||
.option("-s, --spaces [amount]", "Number of spaces for indentation") | ||
.option("-s, --spaces <amount>", "Number of spaces for indentation", parseInt) | ||
.option("-i, --inflection <none|pascalcase>", "Inflection type, defaults to none") | ||
.option("--no-sort", "Don't sort") | ||
.usage("<file>") | ||
.usage("[options] [file ...]") | ||
.parse(process.argv); | ||
@@ -19,6 +24,8 @@ | ||
if (spaces === undefined) { | ||
spaces = "4"; | ||
spaces = 4; | ||
} | ||
var spaces = parseInt(spaces); | ||
var spaces = spaces; | ||
var sort = program.sort; | ||
var inflection = program.inflection || allowedInflection[0]; | ||
assert([allowedInflection].indexOf(inflection), `Inflection ${inflection} not allowed`); | ||
@@ -28,9 +35,11 @@ if (program.args.length) { | ||
} else { | ||
getStdin().then((src) => { | ||
const result = parseString(src); | ||
process.stdout.write(result); | ||
}).catch((err) => { | ||
console.err(err); | ||
process.exit(1); | ||
}); | ||
getStdin() | ||
.then((src) => { | ||
const result = parseString(src); | ||
process.stdout.write(result); | ||
}) | ||
.catch((err) => { | ||
console.err(err); | ||
process.exit(1); | ||
}); | ||
} | ||
@@ -46,2 +55,5 @@ | ||
var js = jsonic(str); | ||
if (inflection === "pascalcase") { | ||
js = inflect.pascalcase(js); | ||
} | ||
if (sort) { | ||
@@ -48,0 +60,0 @@ return sortJson(js, { space: spaces }) + "\n"; |
{ | ||
"name": "json-fix", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "", | ||
@@ -27,5 +27,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"assert": "^1.4.1", | ||
"commander": "^2.12.2", | ||
"get-stdin": "^6.0.0", | ||
"good-sort-json": "^0.1.2", | ||
"inflect-json": "^0.2.0", | ||
"jsonic": "^0.3.0" | ||
@@ -37,4 +39,5 @@ }, | ||
"mocha": "^5.0.4", | ||
"node-cmd": "^3.0.0" | ||
"node-cmd": "^3.0.0", | ||
"prettier": "^1.11.1" | ||
} | ||
} |
@@ -9,6 +9,16 @@ # json-fix | ||
## Option spaces | ||
## Options | ||
You can specify spaces with `--spaces %number of spaces%`, it uses `4` as default | ||
$ json-fix --help | ||
Usage: index [options] [file ...] | ||
Options: | ||
-V, --version output the version number | ||
-s, --spaces <amount> Number of spaces for indentation | ||
-i, --inflection <none|pascalcase> Inflection type, defaults to none | ||
--no-sort Don't sort | ||
-h, --help output usage information | ||
## Examples | ||
@@ -15,0 +25,0 @@ |
@@ -9,3 +9,3 @@ const cmd = require("node-cmd"); | ||
describe("json-fix", function () { | ||
describe("json-fix", function() { | ||
const indexsrc = path.resolve(__dirname, "../index.js"); | ||
@@ -19,4 +19,4 @@ beforeEach(async function() { | ||
it("should work with exapmle", async function() { | ||
const testsrc = path.join(__dirname, "fixture/test.json"); | ||
const testjson = "{a:3,b:2}" | ||
const testsrc = path.join(__dirname, "fixture/test.json"); | ||
const testjson = "{a:3,b:2}"; | ||
const command = `node ${indexsrc} ${testsrc}`; | ||
@@ -34,3 +34,3 @@ const expected = `{ | ||
it("should work from stdin", async function() { | ||
const testjson = "{a:3,b:2}" | ||
const testjson = "{a:3,b:2}"; | ||
const command = `echo ${testjson} | node ${indexsrc}`; | ||
@@ -43,5 +43,15 @@ const expected = `{ | ||
const result = await get(command); | ||
console.log(result); | ||
expect(result.toString()).to.be.eq(expected); | ||
}); | ||
it("should work with inflection=pascalcase", async function() { | ||
const testsrc = path.join(__dirname, "fixture/test.json"); | ||
await fs.writeFile(testsrc, "{a:3,b:2}"); | ||
await get(`node ${indexsrc} ${testsrc} --inflection pascalcase`); | ||
const result = await fs.readFile(testsrc); | ||
expect(result.toString()).to.be.eq(`{ | ||
"A": 3, | ||
"B": 2 | ||
} | ||
`); | ||
}); | ||
}); |
6540
6
105
76
6
5
+ Addedassert@^1.4.1
+ Addedinflect-json@^0.2.0
+ Addedassert@1.5.1(transitive)
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinherits@2.0.3(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.7(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedutil@0.10.4(transitive)