Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80698 views
1
define(
2
[],
3
function() {
4
"use strict";
5
6
var fs = require('fs'),
7
Handlebars = require('./index'),
8
basename = require('path').basename,
9
SourceMap = require('source-map'),
10
SourceMapConsumer = SourceMap.SourceMapConsumer,
11
SourceNode = SourceMap.SourceNode,
12
uglify = require('uglify-js');
13
14
module.exports.cli = function(opts) {
15
if (opts.version) {
16
console.log(Handlebars.VERSION);
17
return;
18
}
19
20
if (!opts.templates.length) {
21
throw new Handlebars.Exception('Must define at least one template or directory.');
22
}
23
24
opts.templates.forEach(function(template) {
25
try {
26
fs.statSync(template);
27
} catch (err) {
28
throw new Handlebars.Exception('Unable to open template file "' + template + '"');
29
}
30
});
31
32
if (opts.simple && opts.min) {
33
throw new Handlebars.Exception('Unable to minimize simple output');
34
}
35
if (opts.simple && (opts.templates.length !== 1 || fs.statSync(opts.templates[0]).isDirectory())) {
36
throw new Handlebars.Exception('Unable to output multiple templates in simple mode');
37
}
38
39
// Convert the known list into a hash
40
var known = {};
41
if (opts.known && !Array.isArray(opts.known)) {
42
opts.known = [opts.known];
43
}
44
if (opts.known) {
45
for (var i = 0, len = opts.known.length; i < len; i++) {
46
known[opts.known[i]] = true;
47
}
48
}
49
50
// Build file extension pattern
51
var extension = opts.extension.replace(/[\\^$*+?.():=!|{}\-\[\]]/g, function(arg) { return '\\' + arg; });
52
extension = new RegExp('\\.' + extension + '$');
53
54
var output = new SourceNode();
55
if (!opts.simple) {
56
if (opts.amd) {
57
output.add('define([\'' + opts.handlebarPath + 'handlebars.runtime\'], function(Handlebars) {\n Handlebars = Handlebars["default"];');
58
} else if (opts.commonjs) {
59
output.add('var Handlebars = require("' + opts.commonjs + '");');
60
} else {
61
output.add('(function() {\n');
62
}
63
output.add(' var template = Handlebars.template, templates = ');
64
if (opts.namespace) {
65
output.add(opts.namespace);
66
output.add(' = ');
67
output.add(opts.namespace);
68
output.add(' || ');
69
}
70
output.add('{};\n');
71
}
72
function processTemplate(template, root) {
73
var path = template,
74
stat = fs.statSync(path);
75
if (stat.isDirectory()) {
76
fs.readdirSync(template).map(function(file) {
77
var path = template + '/' + file;
78
79
if (extension.test(path) || fs.statSync(path).isDirectory()) {
80
processTemplate(path, root || template);
81
}
82
});
83
} else {
84
var data = fs.readFileSync(path, 'utf8');
85
86
if (opts.bom && data.indexOf('\uFEFF') === 0) {
87
data = data.substring(1);
88
}
89
90
var options = {
91
knownHelpers: known,
92
knownHelpersOnly: opts.o
93
};
94
95
if (opts.map) {
96
options.srcName = path;
97
}
98
if (opts.data) {
99
options.data = true;
100
}
101
102
// Clean the template name
103
if (!root) {
104
template = basename(template);
105
} else if (template.indexOf(root) === 0) {
106
template = template.substring(root.length+1);
107
}
108
template = template.replace(extension, '');
109
110
var precompiled = Handlebars.precompile(data, options);
111
112
// If we are generating a source map, we have to reconstruct the SourceNode object
113
if (opts.map) {
114
var consumer = new SourceMapConsumer(precompiled.map);
115
precompiled = SourceNode.fromStringWithSourceMap(precompiled.code, consumer);
116
}
117
118
if (opts.simple) {
119
output.add([precompiled, '\n']);
120
} else if (opts.partial) {
121
if(opts.amd && (opts.templates.length == 1 && !fs.statSync(opts.templates[0]).isDirectory())) {
122
output.add('return ');
123
}
124
output.add(['Handlebars.partials[\'', template, '\'] = template(', precompiled, ');\n']);
125
} else {
126
if(opts.amd && (opts.templates.length == 1 && !fs.statSync(opts.templates[0]).isDirectory())) {
127
output.add('return ');
128
}
129
output.add(['templates[\'', template, '\'] = template(', precompiled, ');\n']);
130
}
131
}
132
}
133
134
opts.templates.forEach(function(template) {
135
processTemplate(template, opts.root);
136
});
137
138
// Output the content
139
if (!opts.simple) {
140
if (opts.amd) {
141
if(opts.templates.length > 1 || (opts.templates.length == 1 && fs.statSync(opts.templates[0]).isDirectory())) {
142
if(opts.partial){
143
output.add('return Handlebars.partials;\n');
144
} else {
145
output.add('return templates;\n');
146
}
147
}
148
output.add('});');
149
} else if (!opts.commonjs) {
150
output.add('})();');
151
}
152
}
153
154
155
if (opts.map) {
156
output.add('\n//# sourceMappingURL=' + opts.map + '\n');
157
}
158
159
output = output.toStringWithSourceMap();
160
output.map = output.map + '';
161
162
if (opts.min) {
163
output = uglify.minify(output.code, {
164
fromString: true,
165
166
outSourceMap: opts.map,
167
inSourceMap: JSON.parse(output.map)
168
});
169
if (opts.map) {
170
output.code += '\n//# sourceMappingURL=' + opts.map + '\n';
171
}
172
}
173
174
if (opts.map) {
175
fs.writeFileSync(opts.map, output.map, 'utf8');
176
}
177
output = output.code;
178
179
if (opts.output) {
180
fs.writeFileSync(opts.output, output, 'utf8');
181
} else {
182
console.log(output);
183
}
184
};
185
});
186