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.

1 Like

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.

1 Like

But how would I bundle that command with hugo?

1 Like

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:

1 Like

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

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

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.

1 Like

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.

1 Like

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.

1 Like