|
- #!/usr/bin/env node
-
- var fs = require('fs');
-
- var Mustache = require('..');
- var pkg = require('../package');
-
- var viewArg = process.argv[2];
- var templateArg = process.argv[3];
-
- if (hasVersionArg()) {
- return console.log(pkg.version);
- }
-
- if (!templateArg || !viewArg) {
- console.error('Syntax: mustache <view> <template>');
- process.exit(1);
- }
-
- run(readView, readTemplate, render, toStdout);
-
- /**
- * Runs a list of functions as a waterfall.
- * Functions are runned one after the other in order, providing each
- * function the returned values of all the previously invoked functions.
- * Each function is expected to exit the process if an error occurs.
- */
- function run(/*args*/) {
- var values = [];
- var fns = Array.prototype.slice.call(arguments);
-
- function invokeNextFn(val) {
- values.unshift(val);
- if (fns.length == 0) return;
- invoke(fns.shift());
- }
-
- function invoke(fn) {
- fn.apply(null, [invokeNextFn].concat(values));
- }
-
- invoke(fns.shift());
- }
-
- function readView(cb) {
- var view = isStdin(viewArg) ? process.openStdin() : fs.createReadStream(viewArg);
-
- streamToStr(view, function(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);
- }
-
- function render(cb, templateStr, jsonView) {
- cb(Mustache.render(templateStr, jsonView));
- }
-
- function toStdout(cb, str) {
- cb(process.stdout.write(str));
- }
-
- function streamToStr(stream, cb) {
- var data = '';
-
- stream.on('data', function(chunk) {
- data += chunk;
- }).once('end', function() {
- cb(data.toString());
- }).on('error', function(err) {
- if (wasNotFound(err)) {
- console.error('Could not find file:', err.path);
- } else {
- console.error('Error while reading file:', err.message);
- }
- process.exit(1);
- });
- }
-
- function isStdin(viewArg) {
- return viewArg === '-';
- }
-
- function wasNotFound(err) {
- return err.code && err.code === 'ENOENT';
- }
-
- function hasVersionArg() {
- return ['--version', '-v'].some(function(opt) {
- return process.argv.indexOf(opt) > -1;
- });
- }
|