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

Improve CLI parsing error #455

Merged
merged 1 commit into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion bin/mustache
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,23 @@ function readView(cb) {
var view = isStdin(viewArg) ? process.openStdin() : fs.createReadStream(viewArg);

streamToStr(view, function(str) {
cb(JSON.parse(str));
cb(parseView(str));
});
}

function parseView(str) {
try {
return JSON.parse(str);
} catch (ex) {
console.error(
'Shooot, could not parse view as JSON.\n'+
'Tips: functions are not valid JSON and keys / values must be surround with double quotes.\n\n'+
ex.stack);

process.exit(1);
}
}

function readTemplate(cb) {
var template = fs.createReadStream(templateArg);
streamToStr(template, cb);
Expand Down
7 changes: 7 additions & 0 deletions test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ describe('Mustache CLI', function () {
});
});

it('writes hints about JSON parsing errors when given invalid JSON', function(done) {
exec('echo {name:"lebron"} | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Shooot, could not parse view as JSON'), -1);
done();
});
});

it('writes module version into stdout when runned with --version', function(done){
exec('bin/mustache --version', function(err, stdout, stderr) {
assert.notEqual(stdout.indexOf(moduleVersion), -1);
Expand Down