mustache 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env node
  2. var fs = require('fs'),
  3. path = require('path');
  4. var Mustache = require('..');
  5. var pkg = require('../package');
  6. var partials = {};
  7. var partialsPaths = [];
  8. var partialArgIndex = -1;
  9. while ((partialArgIndex = process.argv.indexOf('-p')) > -1) {
  10. partialsPaths.push(process.argv.splice(partialArgIndex, 2)[1]);
  11. }
  12. var viewArg = process.argv[2];
  13. var templateArg = process.argv[3];
  14. var outputArg = process.argv[4];
  15. if (hasVersionArg()) {
  16. return console.log(pkg.version);
  17. }
  18. if (!templateArg || !viewArg) {
  19. console.error('Syntax: mustache <view> <template> [output]');
  20. process.exit(1);
  21. }
  22. run(readPartials, readView, readTemplate, render, toStdout);
  23. /**
  24. * Runs a list of functions as a waterfall.
  25. * Functions are runned one after the other in order, providing each
  26. * function the returned values of all the previously invoked functions.
  27. * Each function is expected to exit the process if an error occurs.
  28. */
  29. function run (/*args*/) {
  30. var values = [];
  31. var fns = Array.prototype.slice.call(arguments);
  32. function invokeNextFn (val) {
  33. values.unshift(val);
  34. if (fns.length === 0) return;
  35. invoke(fns.shift());
  36. }
  37. function invoke (fn) {
  38. fn.apply(null, [invokeNextFn].concat(values));
  39. }
  40. invoke(fns.shift());
  41. }
  42. function readView (cb) {
  43. var view = isStdin(viewArg) ? process.openStdin() : fs.createReadStream(viewArg);
  44. streamToStr(view, function onDone (str) {
  45. cb(parseView(str));
  46. });
  47. }
  48. function parseView (str) {
  49. try {
  50. return JSON.parse(str);
  51. } catch (ex) {
  52. console.error(
  53. 'Shooot, could not parse view as JSON.\n' +
  54. 'Tips: functions are not valid JSON and keys / values must be surround with double quotes.\n\n' +
  55. ex.stack);
  56. process.exit(1);
  57. }
  58. }
  59. function readPartials (cb) {
  60. if (!partialsPaths.length) return cb();
  61. var partialPath = partialsPaths.pop();
  62. var partial = fs.createReadStream(partialPath);
  63. streamToStr(partial, function onDone (str) {
  64. partials[getPartialName(partialPath)] = str;
  65. readPartials(cb);
  66. });
  67. }
  68. function readTemplate (cb) {
  69. var template = fs.createReadStream(templateArg);
  70. streamToStr(template, cb);
  71. }
  72. function render (cb, templateStr, jsonView) {
  73. cb(Mustache.render(templateStr, jsonView, partials));
  74. }
  75. function toStdout (cb, str) {
  76. if (outputArg) {
  77. cb(fs.writeFileSync(outputArg, str));
  78. } else {
  79. cb(process.stdout.write(str));
  80. }
  81. }
  82. function streamToStr (stream, cb) {
  83. var data = '';
  84. stream.on('data', function onData (chunk) {
  85. data += chunk;
  86. }).once('end', function onEnd () {
  87. cb(data.toString());
  88. }).on('error', function onError (err) {
  89. if (wasNotFound(err)) {
  90. console.error('Could not find file:', err.path);
  91. } else {
  92. console.error('Error while reading file:', err.message);
  93. }
  94. process.exit(1);
  95. });
  96. }
  97. function isStdin (view) {
  98. return view === '-';
  99. }
  100. function wasNotFound (err) {
  101. return err.code && err.code === 'ENOENT';
  102. }
  103. function hasVersionArg () {
  104. return ['--version', '-v'].some(function matchInArgs (opt) {
  105. return process.argv.indexOf(opt) > -1;
  106. });
  107. }
  108. function getPartialName (filename) {
  109. return path.basename(filename, '.mustache');
  110. }