Projects > pug-cli
A Little Script for Building Pug Templates
Apparently pug-cli
is dead. Perhaps pug itself is dead, although slightly less so.
Regardless, npm audit error messages prompted me to
realize that pug-cli has not seen a commit since 2017.
I decided to peek under the hood at pug-cli... and it's nothing. For some reason, it pulls in a couple of pretty big dependencies, and has a bit more "business" logic than I was expecting. At the end of the day, here's what pug-cli should do:
- Take pug files
- Compile them into html files
The various dependencies of pug-cli are used for parsing
command-line arguments, adding color to console output, and
creating directories (apparently
fs.mkdir('a/b/c', { recursive: true }) did not exist in
2017).
So, okay. If you want to parse some command-line arguments, that
feels like an easier job for zsh. I don't need colored
console output, so we can forget about that. All I need is a
script that will require("pug").compileFile(f)({}), in a
loop over a set of source files, and then write the resulting html
to an output directory. So:
--sources: pug files to compile.--outdir: directory for the resulting html files.--strip-prefix: strip a prefix from the source files. This is a nicer, more explicit version of the behavior of@pugjs/pug-cliwhen it is given a directory instead of a specific file for its input. Basically you can have your pug insrc/and compile it todist/without copying thesrc/part of the directory structure, so with e.g.--strip-prefix src/pug --outdir dist, the source filesrc/pug/projects/my-project/index.pugwill be output todist/projects/my-project/index.pug.
Seems easy enough, and a couple hours (whoops) later, we've got our own little pug-cli. It's about 160 lines, but it would be substantially less if I wasn't obsessively tracking and logging the time taken by various steps of the process. (It's surprisingly slow, and node.js cold-boot startup time accounts for an average of 40% of the time it takes to compile this site.)
Without further ado...