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