Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/cli.js
Expand Up @@ -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) {
Copy link
Member

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 after args.

Copy link
Member

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.

console.error(USAGE);
process.exit(1);
}
Expand All @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the output look like for an invalid file? I assume JSON5.parse will throw an error, so you'll just get the stack trace. But that doesn't seem any different from what you reported in #72 (comment)?

The fix, I think, would be to wrap the whole FS.readFileSync + JSON5.parse in a try/catch, and in the catch, log " is invalid" along w/ the error stack trace. (But important: re-throw the error if compiling.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also minor code style: if ( instead of if(.

} else {
var json = JSON.stringify(obj, null, 4); // 4 spaces; TODO configurable?

path = Path.join(dirname, basename + '.json');
FS.writeFileSync(path, json, 'utf8');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style: this file uses 4-space indentation.

});