Skip to content

Provides a cross-platform interface to the Hugo binary from Node.js.

License

Notifications You must be signed in to change notification settings

small-tech/node-hugo

Repository files navigation

Node Hugo

A basic cross-platform interface to the Hugo binary from Node.js that uses the 64-bit release binaries to support Linux, macOS, and Windows.

It should just work™

System requirements

Runs on x64, arm, and arm64 on Linux, macOS, and Windows.

Installation

npm i @small-tech/node-hugo

Usage

const Hugo = require('@small-tech/node-hugo')

(async function main () {

  const optionalPathToHugoConfiguration = 'optional/path/to/hugo/configuration/'

  const hugo = new Hugo()

  // Run a build.
  try {
    await hugo.build(optionalPathToHugoConfiguration)
  } catch (error) {
    console.log('There was an error building the site with Hugo'. error)
    return
  }

  console.log('Site build successful. Output:', hugo.output)

  // Start Hugo server. Returns a ChildProcess instance.
  const hugoServerProcess = hugo.createServer(optionalPathToHugoConfiguration)

  hugoServerProcess.on('error', (error) => {
    console.log('Hugo server encountered an error', error)
  })

  hugoServerProcess.stdout.on('data', (data) => {
    console.log(`[Hugo] ${data}`)
  })

  hugoServerProcess.stderr.on('data', (data) => {
    console.log(`[Hugo] [ERROR] ${data}`)
  })

  hugoServerProcess.on('close', (code) => {
    console.log('Hugo server process exited with code', code)
  })

  // Close the server after 5 seconds.
  setTimeout(() => {
    hugoServerProcess.kill()
  }, 5000)

})()

(Version 1.2.0+) You can also run any Hugo command using the simple pass-through command() method:

const Hugo = require('@small-tech/node-hugo')

(async function main () {

  const hugo = new Hugo()

  try {
    // Tell the built-in Hugo binary to create a new site at ./my-new-site/.
    await hugo.command('new site my-new-site')
  } catch (error) {
    console.log('There was an error creating the new site with Hugo'. error)
    return
  }

  console.log('Site build successful. Output:', hugo.output)
})()

(Version 1.3.0+) For creating Hugo servers with an arbitrary argument string, you can use the serverWithArgs() method. This works like the blocking command() method but provides streaming output from the Hugo server.

(Version 1.4.0+) You can retrieve the version of Hugo that’s bundled using the version property:

const Hugo = require('@small-tech/node-hugo')
const hugo = new Hugo()
console.log(`Hugo version: ${hugo.version}`)

Maintenance

Update Hugo binaries

On Linux-esque operating systems (with Bash shell):

npm run update-hugo-bin

This will remove the current binaries, download the latest ones, and rename them properly.

About

Provides a cross-platform interface to the Hugo binary from Node.js.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published