Add optional `output` argument to mustache bintags/v2.3.0
| @@ -16,13 +16,14 @@ while ((partialArgIndex = process.argv.indexOf('-p')) > -1) { | |||||
| var viewArg = process.argv[2]; | var viewArg = process.argv[2]; | ||||
| var templateArg = process.argv[3]; | var templateArg = process.argv[3]; | ||||
| var outputArg = process.argv[4]; | |||||
| if (hasVersionArg()) { | if (hasVersionArg()) { | ||||
| return console.log(pkg.version); | return console.log(pkg.version); | ||||
| } | } | ||||
| if (!templateArg || !viewArg) { | if (!templateArg || !viewArg) { | ||||
| console.error('Syntax: mustache <view> <template>'); | |||||
| console.error('Syntax: mustache <view> <template> [output]'); | |||||
| process.exit(1); | process.exit(1); | ||||
| } | } | ||||
| @@ -92,7 +93,11 @@ function render (cb, templateStr, jsonView) { | |||||
| } | } | ||||
| function toStdout (cb, str) { | function toStdout (cb, str) { | ||||
| cb(process.stdout.write(str)); | |||||
| if (outputArg) { | |||||
| cb(fs.writeFileSync(outputArg, str)); | |||||
| } else { | |||||
| cb(process.stdout.write(str)); | |||||
| } | |||||
| } | } | ||||
| function streamToStr (stream, cb) { | function streamToStr (stream, cb) { | ||||
| @@ -60,6 +60,18 @@ describe('Mustache CLI', function () { | |||||
| }); | }); | ||||
| }); | }); | ||||
| it('writes rendered template into the file specified by the third argument', function(done) { | |||||
| var outputFile = 'test/_files/cli_output.txt'; | |||||
| exec('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile, function(err, stdout, stderr) { | |||||
| assert.equal(err, null); | |||||
| assert.equal(stderr, ''); | |||||
| assert.equal(stdout, ''); | |||||
| assert.equal(fs.readFileSync(outputFile), expectedOutput); | |||||
| fs.unlink('test/_files/cli_output.txt'); | |||||
| done(); | |||||
| }); | |||||
| }); | |||||
| it('reads view data from stdin when first argument equals "-"', function(done){ | it('reads view data from stdin when first argument equals "-"', function(done){ | ||||
| exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) { | exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) { | ||||
| assert.equal(err, null); | assert.equal(err, null); | ||||