Hi everyone! I’m attempting to write a simple CLI build tool for a Hugo theme. One of my goals is to make it easy for an end-user to run two processes at the same time:
hugo server
-
webpack --watch
(plus various other options) in athemes/
subfolder
I’d like to provide one simple command that saves a user from having to open two separate terminal windows and give two separate commands.
When I run each process in a separate terminal window, the site automatically reloads fine.But I’m having trouble getting LiveReload to work when running both processes at once.
I’ve been attempting to do this within a Node.js CLI app (based on the Commander library), but I’m also open to using shell scripts for this.
Here’s how I attempted to do this in a shell script:
# bin/dev.sh
trap "kill %1" EXIT
hugo server & cd "themes/my-theme-folder" && ./node_modules/.bin/webpack --watch
And here’s an attempt in Node:
const spawn = require('child_process').spawn
let webpackCmd = 'node_modules/.bin/webpack'
let webpackProcess = spawn(webpackCmd, ['--watch'], { cwd: path.join(process.cwd(), 'themes', 'my-theme')})
webpackProcess.stdout.on('data', (data) => { console.log(`${data}`)})
webpackProcess.stderr.on('data', (data) => { console.log(`${data}`)})
webpackProcess.on('close', (code) => { console.log(`Exited with code: ${code}`)})
let hugoProcess = spawn('hugo', ['server'])
hugoProcess.stdout.on('data', (data) => { console.log(`${data}`)})
hugoProcess.stderr.on('data', (data) => { console.log(`${data}`)})
hugoProcess.on('close', (code) => { console.log(`Exited with code: ${code}`)})
Both of these approaches build the theme assets (scss, js, etc) on first run but it seems like changes after that are not gettting picked up by Hugo.
I’m not a Node expert, so maybe I’m not allowing these processes to communicate properly?
Any suggestions are greatly appreciated.