ソースを参照

Merge pull request #455 from janl/improve/cli-parse-error

Improve CLI parsing error
tags/v2.1.0
David da Silva Contín 11年前
コミット
519669d2cb
2個のファイルの変更21行の追加1行の削除
  1. +14
    -1
      bin/mustache
  2. +7
    -0
      test/cli-test.js

+ 14
- 1
bin/mustache ファイルの表示

@@ -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);


+ 7
- 0
test/cli-test.js ファイルの表示

@@ -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);


読み込み中…
キャンセル
保存