-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (164 loc) · 5.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var fs = require('fs');
var basename = require('path').basename;
exports.summary = 'Compile Handlebars templates to JavaScript templates file';
exports.usage = '<src> [options]';
exports.options = {
"dest" : {
alias : 'd'
,default : '<src>'
,describe : 'destination file or directory'
},
'amd': {
'type': 'boolean',
'description': 'Exports amd style (require.js)'
},
'commonjs': {
'type': 'string',
'description': 'Exports CommonJS style, path to Handlebars module',
'default': null
},
'handlebarPath': {
'type': 'string',
'description': 'Path to handlebar.js (only valid for amd-style)',
'default': ''
},
'known': {
'type': 'string',
'description': 'Known helpers'
},
'knownOnly': {
'type': 'boolean',
'description': 'Known helpers only'
},
'namespace': {
'type': 'string',
'description': 'Template namespace',
'default': 'Handlebars.templates'
},
'simple': {
'type': 'boolean',
'description': 'Output template function only.'
},
'root': {
'type': 'string',
'description': 'Template root. Base value that will be stripped from template names.'
},
'partial' : {
'type': 'boolean',
'description': 'Compiling a partial template'
},
'extension': {
'type': 'string',
'description': 'Template extension.',
'default': 'handlebars|hbs'
},
"charset" : {
alias : 'c'
,default : 'utf-8'
,describe : 'file encoding type'
},
"postParse" : {
type: "function"
, describe: "post parse the handlebars template into it's AST"
}
};
exports.run = function (options) {
var src = options.src;
var dest = options.dest;
options.extension = options.extension.replace(/[\\^$*+?.():=!{}\-\[\]]/g, function(arg) {
return '\\' + arg;
}).split('|').map(function(ext){
return '\\.' + ext;
}).join('|');
options.extension = new RegExp( options.extension + '$');
exports.files.forEach(function(inputFile){
var outputFile = dest;
if(exports.file.isDirFormat(dest)){
outputFile = path.join(dest , path.basename(inputFile) );
outputFile = outputFile.replace(options.extension, '.js');
}
exports.compileHandlebars(inputFile, outputFile, options);
});
};
exports.compileHandlebars = function(inputFile, outputFile, options){
var Handlebars = require('handlebars');
// Convert the known list into a hash
var known = {};
if (options.known && !Array.isArray(options.known)) {
options.known = [options.known];
}
if (options.known) {
for (var i = 0, len = options.known.length; i < len; i++) {
known[options.known[i]] = true;
}
}
// template name
var templateName = inputFile;
// Clean the template name
if (!options.root) {
templateName = basename(templateName);
} else if (templateName.indexOf(root) === 0) {
templateName = templateName.substring(root.length+1);
}
templateName = templateName.replace(options.extension, '');
// output stack
var output = [];
if (!options.simple) {
if (options.amd) {
output.push('define([\'' + options.handlebarPath + 'handlebars\'], function(Handlebars) {\n');
} else if (options.commonjs) {
output.push('var Handlebars = require("' + options.commonjs + '");');
} else {
output.push('(function() {\n');
}
output.push(' var template = Handlebars.template, templates = ');
output.push(options.namespace);
output.push(' = ');
output.push(options.namespace);
output.push(' || {};\n');
}
// compile
var code = exports.file.read(inputFile);
var compilerOptions = {
knownHelpers: known,
knownHelpersOnly: options.knownOnly
};
// parse the handlebars template into it's AST
var ast = Handlebars.parse(code);
if(options.postParse) ast = options.postParse(ast);
var compiled = Handlebars.precompile(ast, compilerOptions);
if (options.simple) {
output.push(compiled + '\n');
} else if (options.partial) {
if(options.amd && (options._.length == 1 && !fs.statSync(options._[0]).isDirectory())) {
output.push('return ');
}
output.push('Handlebars.partials[\'' + templateName + '\'] = template(' + compiled + ');\n');
} else {
if(options.amd && (options._.length == 1 && !fs.statSync(options._[0]).isDirectory())) {
output.push('return ');
}
output.push('templates[\'' + templateName + '\'] = template(' + compiled + ');\n');
}
// Output the content
if (!options.simple) {
if (options.amd) {
if(options._.length > 1 || (options._.length == 1 && fs.statSync(options._[0]).isDirectory())) {
if(options.partial){
output.push('return Handlebars.partials;\n');
} else {
output.push('return templates;\n');
}
}
output.push('});');
} else if (!options.commonjs) {
output.push('})();');
}
}
output = output.join('');
if (outputFile) {
exports.file.write(outputFile, output);
exports.log(inputFile, '>', outputFile);
}
return output;
}