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