handlebars 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env node
  2. var argv = parseArgs({
  3. 'f': {
  4. 'type': 'string',
  5. 'description': 'Output File',
  6. 'alias': 'output'
  7. },
  8. 'map': {
  9. 'type': 'string',
  10. 'description': 'Source Map File'
  11. },
  12. 'a': {
  13. 'type': 'boolean',
  14. 'description': 'Exports amd style (require.js)',
  15. 'alias': 'amd'
  16. },
  17. 'c': {
  18. 'type': 'string',
  19. 'description': 'Exports CommonJS style, path to Handlebars module',
  20. 'alias': 'commonjs',
  21. 'default': null
  22. },
  23. 'h': {
  24. 'type': 'string',
  25. 'description': 'Path to handlebar.js (only valid for amd-style)',
  26. 'alias': 'handlebarPath',
  27. 'default': ''
  28. },
  29. 'k': {
  30. 'type': 'string',
  31. 'description': 'Known helpers',
  32. 'alias': 'known'
  33. },
  34. 'o': {
  35. 'type': 'boolean',
  36. 'description': 'Known helpers only',
  37. 'alias': 'knownOnly'
  38. },
  39. 'm': {
  40. 'type': 'boolean',
  41. 'description': 'Minimize output',
  42. 'alias': 'min'
  43. },
  44. 'n': {
  45. 'type': 'string',
  46. 'description': 'Template namespace',
  47. 'alias': 'namespace',
  48. 'default': 'Handlebars.templates'
  49. },
  50. 's': {
  51. 'type': 'boolean',
  52. 'description': 'Output template function only.',
  53. 'alias': 'simple'
  54. },
  55. 'N': {
  56. 'type': 'string',
  57. 'description': 'Name of passed string templates. Optional if running in a simple mode. Required when operating on multiple templates.',
  58. 'alias': 'name'
  59. },
  60. 'i': {
  61. 'type': 'string',
  62. 'description': 'Generates a template from the passed CLI argument.\n"-" is treated as a special value and causes stdin to be read for the template value.',
  63. 'alias': 'string'
  64. },
  65. 'r': {
  66. 'type': 'string',
  67. 'description': 'Template root. Base value that will be stripped from template names.',
  68. 'alias': 'root'
  69. },
  70. 'p': {
  71. 'type': 'boolean',
  72. 'description': 'Compiling a partial template',
  73. 'alias': 'partial'
  74. },
  75. 'd': {
  76. 'type': 'boolean',
  77. 'description': 'Include data when compiling',
  78. 'alias': 'data'
  79. },
  80. 'e': {
  81. 'type': 'string',
  82. 'description': 'Template extension.',
  83. 'alias': 'extension',
  84. 'default': 'handlebars'
  85. },
  86. 'b': {
  87. 'type': 'boolean',
  88. 'description': 'Removes the BOM (Byte Order Mark) from the beginning of the templates.',
  89. 'alias': 'bom'
  90. },
  91. 'v': {
  92. 'type': 'boolean',
  93. 'description': 'Prints the current compiler version',
  94. 'alias': 'version'
  95. },
  96. 'help': {
  97. 'type': 'boolean',
  98. 'description': 'Outputs this message'
  99. }
  100. });
  101. argv.files = argv._;
  102. delete argv._;
  103. var Precompiler = require('../dist/cjs/precompiler');
  104. Precompiler.loadTemplates(argv, function(err, opts) {
  105. if (err) {
  106. throw err;
  107. }
  108. if (opts.help || (!opts.templates.length && !opts.version)) {
  109. printUsage(argv._spec, 120);
  110. } else {
  111. Precompiler.cli(opts);
  112. }
  113. });
  114. function pad(n) {
  115. var str = '';
  116. while (str.length < n) {
  117. str += ' ';
  118. }
  119. return str;
  120. }
  121. function parseArgs(spec) {
  122. var opts = { alias: {}, boolean: [], default: {}, string: [] };
  123. Object.keys(spec).forEach(function (arg) {
  124. var opt = spec[arg];
  125. opts[opt.type].push(arg);
  126. if ('alias' in opt) opts.alias[arg] = opt.alias;
  127. if ('default' in opt) opts.default[arg] = opt.default;
  128. });
  129. var argv = require('minimist')(process.argv.slice(2), opts);
  130. argv._spec = spec;
  131. return argv;
  132. }
  133. function printUsage(spec, wrap) {
  134. var wordwrap = require('wordwrap');
  135. console.log('Precompile handlebar templates.');
  136. console.log('Usage: handlebars [template|directory]...');
  137. var opts = [];
  138. var width = 0;
  139. Object.keys(spec).forEach(function (arg) {
  140. var opt = spec[arg];
  141. var name = (arg.length === 1 ? '-' : '--') + arg;
  142. if ('alias' in opt) name += ', --' + opt.alias;
  143. var meta = '[' + opt.type + ']';
  144. if ('default' in opt) meta += ' [default: ' + JSON.stringify(opt.default) + ']';
  145. opts.push({ name: name, desc: opt.description, meta: meta });
  146. if (name.length > width) width = name.length;
  147. });
  148. console.log('Options:');
  149. opts.forEach(function (opt) {
  150. var desc = wordwrap(width + 4, wrap + 1)(opt.desc);
  151. console.log(' %s%s%s%s%s',
  152. opt.name,
  153. pad(width - opt.name.length + 2),
  154. desc.slice(width + 4),
  155. pad(wrap - opt.meta.length - desc.split(/\n/).pop().length),
  156. opt.meta
  157. );
  158. });
  159. }