Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

168 строки
4.3KB

  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. var keysArray = getPartialNames(partialPath);
  65. keysArray.forEach(function addPartialNames (key) {
  66. partials[key] = str;
  67. });
  68. readPartials(cb);
  69. });
  70. }
  71. function readTemplate (cb) {
  72. var template = fs.createReadStream(templateArg);
  73. streamToStr(template, cb);
  74. }
  75. function render (cb, templateStr, jsonView) {
  76. cb(Mustache.render(templateStr, jsonView, partials));
  77. }
  78. function toStdout (cb, str) {
  79. if (outputArg) {
  80. cb(fs.writeFileSync(outputArg, str));
  81. } else {
  82. cb(process.stdout.write(str));
  83. }
  84. }
  85. function streamToStr (stream, cb) {
  86. var data = '';
  87. stream.on('data', function onData (chunk) {
  88. data += chunk;
  89. }).once('end', function onEnd () {
  90. cb(data.toString());
  91. }).on('error', function onError (err) {
  92. if (wasNotFound(err)) {
  93. console.error('Could not find file:', err.path);
  94. } else {
  95. console.error('Error while reading file:', err.message);
  96. }
  97. process.exit(1);
  98. });
  99. }
  100. function isStdin (view) {
  101. return view === '-';
  102. }
  103. function wasNotFound (err) {
  104. return err.code && err.code === 'ENOENT';
  105. }
  106. function hasVersionArg () {
  107. return ['--version', '-v'].some(function matchInArgs (opt) {
  108. return process.argv.indexOf(opt) > -1;
  109. });
  110. }
  111. function getPartialNames (filename) {
  112. // get path relative to the template file
  113. // in order to use e.g. {{> ../common/footer }}
  114. // before, {{> footer }} used the -p file whose streamToStr happened to
  115. // finish first, as seen above in readPartials
  116. // this can be extended to the mustache API by using the relative path as
  117. // the key in the partials object
  118. // e.g. mustache.render(template, view, { '../common/footer': '...' })
  119. var relativePath = path
  120. .relative(templateArg, filename)
  121. // without this, you would have to use {{> ..\\common\\footer }} for windows
  122. // and {{> ../common/footer }} for *nix
  123. .replace(/\\{1,2}/g, '/')
  124. // since path.relative shows paths relative to the file, and not the
  125. // directory (i.e. what everyone is used to), change the reference to the
  126. // current directory from '../' to './'
  127. .replace(/^\.\.\//, './')
  128. // obviously if we want parents of the current directory, we want '../../'
  129. // as opposed to './../../', so this replace fixes that
  130. .replace(/^\.\/\.\.\//, '../')
  131. .replace('.mustache', '');
  132. return [
  133. path.basename(filename, '.mustache'),
  134. relativePath
  135. ];
  136. }