New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding a --validate CLI option #84
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,15 @@ var Path = require('path'); | |
|
||
var USAGE = [ | ||
'Usage: json5 -c path/to/file.json5 ...', | ||
'Compiles JSON5 files into sibling JSON files with the same basenames.', | ||
' Compiles JSON5 files into sibling JSON files with the same basenames.', | ||
'or: json5 --validate path/to/file.json5 ...', | ||
' Validates JSON5 files' | ||
].join('\n'); | ||
|
||
// if valid, args look like [node, json5, -c, file1, file2, ...] | ||
// if valid, args look like [node, json5, -c || -t, file1, file2, ...] | ||
var args = process.argv; | ||
|
||
if (args.length < 4 || args[2] !== '-c') { | ||
if (args[2] === '-c' || args[2] === '--validate' && args.length < 4) { | ||
console.error(USAGE); | ||
process.exit(1); | ||
} | ||
|
@@ -34,8 +36,13 @@ files.forEach(function (file) { | |
|
||
var json5 = FS.readFileSync(path, 'utf8'); | ||
var obj = JSON5.parse(json5); | ||
var json = JSON.stringify(obj, null, 4); // 4 spaces; TODO configurable? | ||
|
||
path = Path.join(dirname, basename + '.json'); | ||
FS.writeFileSync(path, json, 'utf8'); | ||
// if we are here for validation, we'll just output things are OK. | ||
if(args[2] === '--validate') { | ||
console.log('\t\x1b[32;1m' + file + ' is valid.\x1b[0m'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the output look like for an invalid file? I assume The fix, I think, would be to wrap the whole There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also minor code style: |
||
} else { | ||
var json = JSON.stringify(obj, null, 4); // 4 spaces; TODO configurable? | ||
|
||
path = Path.join(dirname, basename + '.json'); | ||
FS.writeFileSync(path, json, 'utf8'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code style: this file uses 4-space indentation. |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of referencing
args[2]
in multiple spots, let's add a variable to save it, e.g.cmd
afterargs
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is this boolean logic right? I thought
&&
takes precedence over||
, so you might need to wrap the-c || --validate
check in parentheses.