Can you run a script on build?

So for my website, I wrote a node.js script that generates the appropriate content files in markdown. I was wondering if there was a way to add this script to be run automatically whenever I do hugo or hugo server -D.

I can rewrite the script in another language if js is the issue.

you can just make a bash or zsh function, or, use another script program like you mention. You can also consider git hooks as well, if that timing makes sense.

But how would I bundle that command with hugo?

You should figure out how you want to build and deploy your site. Your question is too broad to answer. I suggest searching for communities that focus on scripting. :slight_smile:

Here’s an example of my hugo testsite where I start different environments with bash scripts:

example-script.sh:

#!/usr/bin/env bash
HUGO_ENV="production" hugo -d build/public --config config.yml,config_public.yml

# https://www.npmjs.com/package/html-minifier
html-minifier --input-dir build/public --output-dir build/public --file-ext html --collapse-whitespace
html-minifier --input-dir build/public --output-dir build/public --file-ext xml --collapse-whitespace

The scripts starts Hugo in Production environment and writes the generated site to build/public. 2 config files are used (the second overwrites certain values from the first).

Finally it minimizes the HTML files.

Hope this helps.

If you are already using Node.js and need to bundle modules, just use npm instead of a separate shell script. In fact, I just did an npm init in my sites root folder so that I could have a number of scripts:

  "scripts": {
    "start": "C:/src/Hugo/bin/hugo.exe server -D --disableFastRender",
    "build": "C:/src/Hugo/bin/hugo",
    "build-theme": "cd ./themes/twenty-sixteen; C:/src/Hugo/bin/hugo",
    "hugo-version": "C:/src/Hugo/bin/hugo version",
    "test": "echo \"No tests available\" && exit 1"
  },

Saves me having to remember the hugo commands, especially as my hugo.exe is not in my path so I need to specify the right path to it. I also have the build-theme script so that I can easily build the scss and minify things without having to remember all the details.

I am trying to do something like myself.

Reading answers here, looks like you can have a bash/zsh script do all what your node.js script is doing right now, and in the end when everything is ready, run hugo server from that script itself.