Skip to content

Commit

Permalink
Merge pull request #455 from janl/improve/cli-parse-error
Browse files Browse the repository at this point in the history
Improve CLI parsing error
  • Loading branch information
dasilvacontin committed May 21, 2015
2 parents cb14bb2 + b2fb42e commit 519669d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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

0 comments on commit 519669d

Please sign in to comment.