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:

  1. Take pug files
  2. 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:

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...