11#!/usr/bin/env node
22
33var optimist = require ( 'optimist' )
4- . usage ( 'Precompile handlebar templates.\nUsage: $0 template...' , {
5- 'f' : {
6- 'type' : 'string' ,
7- 'description' : 'Output File' ,
8- 'alias' : 'output'
9- } ,
10- 'a' : {
11- 'type' : 'boolean' ,
12- 'description' : 'Exports amd style (require.js)' ,
13- 'alias' : 'amd'
14- } ,
15- 'c' : {
16- 'type' : 'string' ,
17- 'description' : 'Exports CommonJS style, path to Handlebars module' ,
18- 'alias' : 'commonjs' ,
19- 'default' : null
20- } ,
21- 'h' : {
22- 'type' : 'string' ,
23- 'description' : 'Path to handlebar.js (only valid for amd-style)' ,
24- 'alias' : 'handlebarPath' ,
25- 'default' : ''
26- } ,
27- 'k' : {
28- 'type' : 'string' ,
29- 'description' : 'Known helpers' ,
30- 'alias' : 'known'
31- } ,
32- 'o' : {
33- 'type' : 'boolean' ,
34- 'description' : 'Known helpers only' ,
35- 'alias' : 'knownOnly'
36- } ,
37- 'm' : {
38- 'type' : 'boolean' ,
39- 'description' : 'Minimize output' ,
40- 'alias' : 'min'
41- } ,
42- 'n' : {
43- 'type' : 'string' ,
44- 'description' : 'Template namespace' ,
45- 'alias' : 'namespace' ,
46- 'default' : 'Handlebars.templates'
47- } ,
48- 's' : {
49- 'type' : 'boolean' ,
50- 'description' : 'Output template function only.' ,
51- 'alias' : 'simple'
52- } ,
53- 'r' : {
54- 'type' : 'string' ,
55- 'description' : 'Template root. Base value that will be stripped from template names.' ,
56- 'alias' : 'root'
57- } ,
58- 'p' : {
59- 'type' : 'boolean' ,
60- 'description' : 'Compiling a partial template' ,
61- 'alias' : 'partial'
62- } ,
63- 'd' : {
64- 'type' : 'boolean' ,
65- 'description' : 'Include data when compiling' ,
66- 'alias' : 'data'
67- } ,
68- 'e' : {
69- 'type' : 'string' ,
70- 'description' : 'Template extension.' ,
71- 'alias' : 'extension' ,
72- 'default' : 'handlebars'
73- } ,
74- 'b' : {
75- 'type' : 'boolean' ,
76- 'description' : 'Removes the BOM (Byte Order Mark) from the beginning of the templates.' ,
77- 'alias' : 'bom'
78- } ,
79- 'v' : {
80- 'type' : 'boolean' ,
81- 'description' : 'Prints the current compiler version' ,
82- 'alias' : 'version'
83- }
84- } )
85-
86- . check ( function ( argv ) {
87- if ( argv . version ) {
88- return ;
89- }
90-
91- var template = [ 0 ] ;
92- if ( ! argv . _ . length ) {
93- throw 'Must define at least one template or directory.' ;
94- }
95-
96- argv . _ . forEach ( function ( template ) {
97- try {
98- fs . statSync ( template ) ;
99- } catch ( err ) {
100- throw 'Unable to open template file "' + template + '"' ;
101- }
102- } ) ;
103- } )
104- . check ( function ( argv ) {
105- if ( argv . simple && argv . min ) {
106- throw 'Unable to minimze simple output' ;
107- }
108- if ( argv . simple && ( argv . _ . length !== 1 || fs . statSync ( argv . _ [ 0 ] ) . isDirectory ( ) ) ) {
109- throw 'Unable to output multiple templates in simple mode' ;
110- }
111- } ) ;
112-
113- var fs = require ( 'fs' ) ,
114- handlebars = require ( '../lib' ) ,
115- basename = require ( 'path' ) . basename ,
116- uglify = require ( 'uglify-js' ) ;
117-
118- var argv = optimist . argv ,
119- template = argv . _ [ 0 ] ;
120-
121- if ( argv . version ) {
122- return console . log ( handlebars . VERSION ) ;
123- }
124-
125- // Convert the known list into a hash
126- var known = { } ;
127- if ( argv . known && ! Array . isArray ( argv . known ) ) {
128- argv . known = [ argv . known ] ;
129- }
130- if ( argv . known ) {
131- for ( var i = 0 , len = argv . known . length ; i < len ; i ++ ) {
132- known [ argv . known [ i ] ] = true ;
133- }
134- }
135-
136- // Build file extension pattern
137- var extension = argv . extension . replace ( / [ \\ ^ $ * + ? . ( ) : = ! | { } \- \[ \] ] / g, function ( arg ) { return '\\' + arg ; } ) ;
138- extension = new RegExp ( '\\.' + extension + '$' ) ;
139-
140- var output = [ ] ;
141- if ( ! argv . simple ) {
142- if ( argv . amd ) {
143- output . push ( 'define([\'' + argv . handlebarPath + 'handlebars.runtime\'], function(Handlebars) {\n Handlebars = Handlebars["default"];' ) ;
144- } else if ( argv . commonjs ) {
145- output . push ( 'var Handlebars = require("' + argv . commonjs + '");' ) ;
146- } else {
147- output . push ( '(function() {\n' ) ;
148- }
149- output . push ( ' var template = Handlebars.template, templates = ' ) ;
150- output . push ( argv . namespace ) ;
151- output . push ( ' = ' ) ;
152- output . push ( argv . namespace ) ;
153- output . push ( ' || {};\n' ) ;
154- }
155- function processTemplate ( template , root , explicit ) {
156- var path = template ,
157- stat = fs . statSync ( path ) ;
158- if ( stat . isDirectory ( ) ) {
159- fs . readdirSync ( template ) . map ( function ( file ) {
160- var path = template + '/' + file ;
161-
162- if ( extension . test ( path ) || fs . statSync ( path ) . isDirectory ( ) ) {
163- processTemplate ( path , root || template ) ;
164- }
165- } ) ;
166- } else if ( explicit || extension . test ( path ) ) {
167- var data = fs . readFileSync ( path , 'utf8' ) ;
168-
169- if ( argv . bom && data . indexOf ( '\uFEFF' ) === 0 ) {
170- data = data . substring ( 1 ) ;
171- }
172-
173- var options = {
174- knownHelpers : known ,
175- knownHelpersOnly : argv . o
176- } ;
177-
178- if ( argv . data ) {
179- options . data = true ;
180- }
181-
182- // Clean the template name
183- if ( ! root ) {
184- template = basename ( template ) ;
185- } else if ( template . indexOf ( root ) === 0 ) {
186- template = template . substring ( root . length + 1 ) ;
4+ . usage ( 'Precompile handlebar templates.\nUsage: $0 template...' , {
5+ 'f' : {
6+ 'type' : 'string' ,
7+ 'description' : 'Output File' ,
8+ 'alias' : 'output'
9+ } ,
10+ 'a' : {
11+ 'type' : 'boolean' ,
12+ 'description' : 'Exports amd style (require.js)' ,
13+ 'alias' : 'amd'
14+ } ,
15+ 'c' : {
16+ 'type' : 'string' ,
17+ 'description' : 'Exports CommonJS style, path to Handlebars module' ,
18+ 'alias' : 'commonjs' ,
19+ 'default' : null
20+ } ,
21+ 'h' : {
22+ 'type' : 'string' ,
23+ 'description' : 'Path to handlebar.js (only valid for amd-style)' ,
24+ 'alias' : 'handlebarPath' ,
25+ 'default' : ''
26+ } ,
27+ 'k' : {
28+ 'type' : 'string' ,
29+ 'description' : 'Known helpers' ,
30+ 'alias' : 'known'
31+ } ,
32+ 'o' : {
33+ 'type' : 'boolean' ,
34+ 'description' : 'Known helpers only' ,
35+ 'alias' : 'knownOnly'
36+ } ,
37+ 'm' : {
38+ 'type' : 'boolean' ,
39+ 'description' : 'Minimize output' ,
40+ 'alias' : 'min'
41+ } ,
42+ 'n' : {
43+ 'type' : 'string' ,
44+ 'description' : 'Template namespace' ,
45+ 'alias' : 'namespace' ,
46+ 'default' : 'Handlebars.templates'
47+ } ,
48+ 's' : {
49+ 'type' : 'boolean' ,
50+ 'description' : 'Output template function only.' ,
51+ 'alias' : 'simple'
52+ } ,
53+ 'r' : {
54+ 'type' : 'string' ,
55+ 'description' : 'Template root. Base value that will be stripped from template names.' ,
56+ 'alias' : 'root'
57+ } ,
58+ 'p' : {
59+ 'type' : 'boolean' ,
60+ 'description' : 'Compiling a partial template' ,
61+ 'alias' : 'partial'
62+ } ,
63+ 'd' : {
64+ 'type' : 'boolean' ,
65+ 'description' : 'Include data when compiling' ,
66+ 'alias' : 'data'
67+ } ,
68+ 'e' : {
69+ 'type' : 'string' ,
70+ 'description' : 'Template extension.' ,
71+ 'alias' : 'extension' ,
72+ 'default' : 'handlebars'
73+ } ,
74+ 'b' : {
75+ 'type' : 'boolean' ,
76+ 'description' : 'Removes the BOM (Byte Order Mark) from the beginning of the templates.' ,
77+ 'alias' : 'bom'
78+ } ,
79+ 'v' : {
80+ 'type' : 'boolean' ,
81+ 'description' : 'Prints the current compiler version' ,
82+ 'alias' : 'version'
18783 }
188- template = template . replace ( extension , '' ) ;
84+ } )
18985
190- if ( argv . simple ) {
191- output . push ( handlebars . precompile ( data , options ) + '\n' ) ;
192- } else if ( argv . partial ) {
193- if ( argv . amd && ( argv . _ . length == 1 && ! fs . statSync ( argv . _ [ 0 ] ) . isDirectory ( ) ) ) {
194- output . push ( 'return ' ) ;
195- }
196- output . push ( 'Handlebars.partials[\'' + template + '\'] = template(' + handlebars . precompile ( data , options ) + ');\n' ) ;
197- } else {
198- if ( argv . amd && ( argv . _ . length == 1 && ! fs . statSync ( argv . _ [ 0 ] ) . isDirectory ( ) ) ) {
199- output . push ( 'return ' ) ;
200- }
201- output . push ( 'templates[\'' + template + '\'] = template(' + handlebars . precompile ( data , options ) + ');\n' ) ;
86+ . check ( function ( argv ) {
87+ if ( argv . version ) {
88+ return ;
20289 }
203- }
204- }
205-
206- argv . _ . forEach ( function ( template ) {
207- processTemplate ( template , argv . root , true ) ;
208- } ) ;
209-
210- // Output the content
211- if ( ! argv . simple ) {
212- if ( argv . amd ) {
213- if ( argv . _ . length > 1 || ( argv . _ . length == 1 && fs . statSync ( argv . _ [ 0 ] ) . isDirectory ( ) ) ) {
214- if ( argv . partial ) {
215- output . push ( 'return Handlebars.partials;\n' ) ;
216- } else {
217- output . push ( 'return templates;\n' ) ;
218- }
219- }
220- output . push ( '});' ) ;
221- } else if ( ! argv . commonjs ) {
222- output . push ( '})();' ) ;
223- }
224- }
225- output = output . join ( '' ) ;
226-
227- if ( argv . min ) {
228- output = uglify . minify ( output , { fromString : true } ) . code ;
229- }
90+ } ) ;
23091
231- if ( argv . output ) {
232- fs . writeFileSync ( argv . output , output , 'utf8' ) ;
233- } else {
234- console . log ( output ) ;
235- }
92+ var argv = optimist . argv ;
93+ argv . templates = argv . _ ;
94+ delete argv . _ ;
95+ return require ( '../lib/handlebars/precompiler' ) ( argv ) ;
0 commit comments