forked from pugjs/pug-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·382 lines (335 loc) · 10.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env node
"use strict";
var fs = require("fs");
var path = require("path");
var { program } = require("commander");
var mkdirp = require("mkdirp");
var chalk = require("chalk");
var pug = require("pug");
var yaml = require("js-yaml");
var matter = require("gray-matter");
var basename = path.basename;
var dirname = path.dirname;
var resolve = path.resolve;
var normalize = path.normalize;
var join = path.join;
var relative = path.relative;
var Debug = require("debug");
var debug = {
always: Debug("pug3"),
watched: Debug("pug3:watched"),
render: Debug("pug3:render"),
};
// options
program
.version(
"pug version: " +
require("pug/package.json").version +
"\n" +
"@tokilabs/pug3-cli version: " +
require("./package.json").version
)
.usage("[options] [dir|file ...]")
.option("-O, --obj <str|path>", "JSON/JavaScript/YAML options object or file")
.option("-o, --out <dir>", "output the rendered HTML or compiled JavaScript to <dir>")
.option("-p, --path <path>", "filename used to resolve includes")
.option("-b, --basedir <path>", "path used as root directory to resolve absolute includes")
.option("-P, --pretty", "compile pretty HTML output")
.option("-c, --client", "compile function for client-side")
.option("-n, --name <str>", "the name of the compiled template (requires --client)")
.option("-D, --no-debug", "compile without debugging (smaller functions)")
.option("-w, --watch", "watch files for changes and automatically re-render")
.option("-E, --extension <ext>", "specify the output file extension")
.option("-s, --silent", "do not output logs")
.option(
"--name-after-file",
"name the template after the last section of the file path (requires --client and overriden by --name)"
)
.option("--doctype <str>", "specify the doctype on the command line (useful if it is not specified by the template)");
program.on("--help", function () {
console.log(" Examples:");
console.log("");
console.log(" # Render all files in the `templates` directory:");
console.log(" $ pug3 templates");
console.log("");
console.log(" # Create {foo,bar}.html:");
console.log(" $ pug3 {foo,bar}.pug");
console.log("");
console.log(" # Using `pug` over standard input and output streams");
console.log(" $ pug3 < my.pug > my.html");
console.log(" $ echo 'h1 Pug!' | pug");
console.log("");
console.log(" # Render all files in `foo` and `bar` directories to `/tmp`:");
console.log(" $ pug3 foo bar --out /tmp");
console.log("");
console.log(" # Specify options through a string:");
console.log(' $ pug -O \'{"doctype": "html"}\' foo.pug');
console.log(" # or, using JavaScript instead of JSON");
console.log(" $ pug3 -O \"{doctype: 'html'}\" foo.pug");
console.log("");
console.log(" # Specify options through a file:");
console.log(" $ echo \"exports.doctype = 'html';\" > options.js");
console.log(" $ pug3 -O options.js foo.pug");
console.log(" # or, JSON works too");
console.log(' $ echo \'{"doctype": "html"}\' > options.json');
console.log(" $ pug3 -O options.json foo.pug");
console.log("");
});
program.parse(process.argv);
var cmdOptions = program.opts();
var pugOptions = cmdOptions.obj ? parseObj(cmdOptions.obj) : {};
/**
* Parse object either in `input` or in the file called `input`. The latter is
* searched first.
*/
function parseObj(input) {
let err = `PARSING ${JSON.stringify(input)}\n`;
try {
const resolved = path.resolve(input);
delete require[resolved]; // delete cache
return require(resolved);
} catch (e) {
err += `Require didn't work for ${input}: ${e}\n`;
let str;
try {
str = fs.readFileSync(cmdOptions.obj, "utf8");
} catch (e) {
err += ` and ${cmdOptions.obj} doesn't exist\n`;
str = cmdOptions.obj;
}
err += `Trying JSON.parse ${str}\n`;
try {
return JSON.parse(str);
} catch (e) {
err += `JSON.parse ERROR ${e}\n`;
try {
return yaml.load(str, "utf-8");
} catch (e) {
err += `YAML ERROR ${e}\n`;
err += `Trying EVAL ${str}`;
try {
return eval("(" + str + ")");
} catch (e) {
err += `EVAL ERROR: ${e}\n`;
console.error(err);
throw new Error(err);
}
}
}
}
}
[
["path", "filename"], // --path
["debug", "compileDebug"], // --no-debug
["client", "client"], // --client
["pretty", "pretty"], // --pretty
["basedir", "basedir"], // --basedir
["doctype", "doctype"], // --doctype
].forEach(function (o) {
pugOptions[o[1]] = cmdOptions[o[0]] !== undefined ? cmdOptions[o[0]] : pugOptions[o[1]];
});
// --name
if (typeof cmdOptions.name === "string") {
pugOptions.name = cmdOptions.name;
}
// --silent
var consoleLog = cmdOptions.silent ? function () {} : console.log;
// left-over args are file paths
var files = program.args;
// object of reverse dependencies of a watched file, including itself if
// applicable
var watchList = {};
// function for rendering
var render = cmdOptions.watch ? tryRender : renderFile;
// compile files
if (files.length) {
consoleLog();
// If we are running in watch mode
if (cmdOptions.watch) {
process.on("SIGINT", function () {
process.exit(1);
});
// and watch the configuration file for changes
if (cmdOptions.obj && fs.existsSync(cmdOptions.obj)) {
fs.watchFile(cmdOptions.obj, { persistent: true, interval: 200 }, function () {
consoleLog(chalk.gray("File ") + chalk.yellow(cmdOptions.obj) + " " + chalk.gray("changed"));
// update the options object
pugOptions = parseObj(cmdOptions.obj);
files.forEach(render);
});
debug.watched(" " + chalk.gray("watching") + " " + chalk.yellow(cmdOptions.obj));
}
}
files.forEach(render);
} else {
stdin();
}
/**
* Watch for changes on path
*
* Renders `base` if specified, otherwise renders `path`.
*/
function watchFile(path, base, rootPath) {
path = normalize(path);
var log = " " + chalk.gray("watching") + " " + chalk.cyan(path);
if (!base) {
base = path;
} else {
base = normalize(base);
log += "\n " + chalk.gray("as a dependency of") + " ";
log += chalk.cyan(base);
}
if (watchList[path]) {
if (watchList[path].indexOf(base) !== -1) return;
debug.watched(log);
watchList[path].push(base);
return;
}
debug.watched(log);
watchList[path] = [base];
fs.watchFile(path, { persistent: true, interval: 200 }, function (curr, prev) {
consoleLog(`\n${chalk.gray("File")} ${chalk.yellow(path)} ${chalk.gray("changed")}`);
// File doesn't exist anymore. Keep watching.
if (curr.mtime.getTime() === 0) return;
// istanbul ignore if
if (curr.mtime.getTime() === prev.mtime.getTime()) return;
watchList[path].forEach(function (file) {
tryRender(file, rootPath);
});
});
}
/**
* Convert error to string
*/
function errorToString(e) {
return e.stack || /* istanbul ignore next */ e.message || e;
}
/**
* Try to render `path`; if an exception is thrown it is printed to stderr and
* otherwise ignored.
*
* This is used in watch mode.
*/
function tryRender(path, rootPath) {
try {
renderFile(path, rootPath);
} catch (e) {
// keep watching when error occured.
console.error(errorToString(e) + "\x07");
}
}
/**
* Compile from stdin.
*/
function stdin() {
var buf = "";
process.stdin.setEncoding("utf8");
process.stdin
.on("data", function (chunk) {
buf += chunk;
})
.on("end", function () {
var output;
if (pugOptions.client) {
output = pug.compileClient(buf, pugOptions);
} else {
var fn = pug.compile(buf, pugOptions);
var output = fn(pugOptions);
}
process.stdout.write(output);
})
.on("error", function (err) {
console.error("Error in stdin ", err);
})
.resume();
}
/**
* Process the given path, compiling the pug files found.
* Always walk the subdirectories.
*
* @param path path of the file, might be relative
* @param rootPath path relative to the directory specified in the command
*/
function renderFile(path, rootPath) {
var isPug = /\.(?:pug|jade)$/;
var isIgnored = /([\/\\]_)|(^_)/;
var stat = fs.statSync(path);
// Found pug file
if (stat.isFile() && isPug.test(path) && !isIgnored.test(path)) {
debug.render(`Rendering file ${chalk.green(path)} with rootPath ${rootPath}`);
// Try to watch the file if needed. watchFile takes care of duplicates.
if (cmdOptions.watch) watchFile(path, null, rootPath);
if (cmdOptions.nameAfterFile) {
pugOptions.name = getNameFromFileName(path);
}
let page = matter.read(path);
pugOptions.filename = page.path;
if (page.data.layout) {
page.extended = `extends ${pugOptions.includes}/${page.data.layout}
${page.content}`;
} else {
page.extended = page.content;
}
pugOptions.page = page.data;
var fn = pugOptions.client ? pug.compileClient(page.extended, pugOptions) : pug.compile(page.extended, pugOptions);
if (cmdOptions.watch && fn.dependencies) {
// watch dependencies, and recompile the base
fn.dependencies.forEach(function (dep) {
watchFile(dep, path, rootPath);
});
}
// --extension
var extname;
if (cmdOptions.extension) extname = "." + cmdOptions.extension;
else if (pugOptions.client) extname = ".js";
else if (cmdOptions.extension === "") extname = "";
else extname = ".html";
// path: foo.pug -> foo.<ext>
path = path.replace(isPug, extname);
if (cmdOptions.out) {
// prepend output directory
if (rootPath) {
// replace the rootPath of the resolved path with output directory
if (typeof rootPath !== "string" || typeof path !== "string") {
const e = new Error(`rootPath and path must be strings. Receive rootPath ${rootPath} and ${path}`);
console.error(e.message, e.stack);
process.exit(1);
}
path = relative(rootPath, path);
} else {
// if no rootPath handling is needed
path = basename(path);
}
path = resolve(cmdOptions.out, path);
}
var dir = resolve(dirname(path));
mkdirp.sync(dir);
var output = pugOptions.client ? fn : fn(pugOptions);
fs.writeFileSync(path, output);
consoleLog(" " + chalk.gray("rendered") + " " + chalk.cyan("%s"), normalize(path));
// Found directory
} else if (stat.isDirectory()) {
var files = fs.readdirSync(path);
files
.map(function (filename) {
return path + "/" + filename;
})
.forEach(function (file) {
render(file, rootPath || path);
});
}
}
/**
* Get a sensible name for a template function from a file path
*
* @param {String} filename
* @returns {String}
*/
function getNameFromFileName(filename) {
var file = basename(filename).replace(/\.(?:pug|jade)$/, "");
return (
file.toLowerCase().replace(/[^a-z0-9]+([a-z])/g, function (_, character) {
return character.toUpperCase();
}) + "Template"
);
}