25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

132 satır
3.0KB

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