Skip to content

How to Auto-Rerun Baseprinter Upon Edits

This guide shows how to automatically rerun Baseprinter whenever you save changes to a source text file.

If you auto-rerun Baseprinter, you'll likely also want your browser to auto-reload the previews generated by Baseprinter.

Prerequisites

You will need either:

OR

Alternatives

This guide uses nodemon, but there are many alternatives with similar functionality (such as entr).

How it works

You can use any text editor to save changes to your source files. nodemon runs independently of your text editor (e.g. in a separate terminal window). When nodemon detects that a source file has been saved, it will automatically rerun baseprinter to generate a new Baseprint snapshot and/or HTML/PDF preview.

Warning

Some editors, like vim on Linux, by default change files in a way that prevents the proper functioning of file monitoring. Either monitor a folder or, in the case of vim, use set backupcopy=yes.

Press CTRL-C to stop nodemon.

Running nodemon locally

Use the --exec parameter of nodemon to specify the baseprinter command line you want to run. If you are passing source files to Baseprinter, then add them after the --exec argument.

For example, if you just have a single source file doc.md for Baseprinter, you can do the following:

nodemon --exec 'baseprinter --outdir=preview' doc.md

Additionally, you can use the -w parameter to add paths for nodemon to watch.

Running in a container

If you are running Baseprinter in a container, the Baseprinter container image includes nodemon. It can be used by setting up a shell function:

nodemon() {
  podman run --rm -v=.:/mnt -w=/mnt -it --init \
    registry.gitlab.com/perm.pub/dock/baseprinter nodemon "$@"
}

After defining the nodemon shell function, you can run nodemon as if it were installed locally.

Warning

When running nodemon and baseprinter inside a container, be aware that they will not have access to any paths outside the current working directory.