Commit 7dac3aa8 authored by Aral Balkan's avatar Aral Balkan

Generic hugo command and hugo server command working as intended

parent 4db33d30
//////////////////////////////////////////////////////////////////////
//
// Command: hugo
//
// Gives generic access to the built-in Hugo binary. For advanced
// use if you don’t have Hugo installed locally.
//
//////////////////////////////////////////////////////////////////////
const path = require('path')
const Graceful = require('node-graceful')
const Hugo = require('@small-tech/node-hugo')
const Site = require('../../index')
module.exports = function (args) {
Site.logAppNameAndVersion()
// Recreate the command-line arguments (that were parsed by the general CLI
// (which is what we usually want) to pass to the Hugo process.
const positionalArgs = args.positional.join(' ')
const namedArgs = Object.entries(args.named).reduce((namedArgsString, currentValue) => {
const [key, value] = currentValue
return `${namedArgsString} --${key}=${value}`
}, /* initialValue = */ '')
const hugoArgs = `${positionalArgs}${namedArgs}`
const hugo = new Hugo(path.join(Site.settingsDirectory, 'node-hugo'))
console.log(` 🎠 ❨Site.js❩ Running Hugo with command ${hugoArgs}\n`)
;(async () => {
if (positionalArgs.startsWith('server')) {
//
// This is a server request. Pass it to a method that pipes the
// responses back instead of blocking.
//
let result
try {
result = await hugo.serverWithArgs(hugoArgs)
} catch (error) {
error.split('\n').forEach(line => {
console.log(`${Site.HUGO_LOGO} ${line}`)
})
console.log('\n ❌ Hugo encountered an error. Exiting… ')
console.log('\n 💕 ❨Site.js❩ Goodbye!\n')
process.exit(1)
}
const { hugoServerProcess, hugoBuildOutput } = result
// At this point, the build process is complete and the .generated folder should exist.
// Note: the following three handlers are repeated here from /index.js.
// Listen for standard output and error output on the server instance.
hugoServerProcess.stdout.on('data', (data) => {
const lines = data.toString('utf-8').split('\n')
lines.forEach(line => console.log(`${Site.HUGO_LOGO} ${line}`))
})
hugoServerProcess.stderr.on('data', (data) => {
const lines = data.toString('utf-8').split('\n')
lines.forEach(line => console.log(`${Site.HUGO_LOGO} [ERROR] ${line}`))
})
// Print the output received so far.
hugoBuildOutput.split('\n').forEach(line => {
console.log(`${Site.HUGO_LOGO} ${line}`)
})
// Handle graceful exit.
goodbye = (done) => {
console.log('\n 💃 ❨Site.js❩ Preparing to exit gracefully, please wait…\n')
if (hugoServerProcess) {
console.log(' 🚮 ❨Site.js❩ Killing Hugo server process.')
hugoServerProcess => hugoServerProcess.kill()
}
console.log('\n 💕 ❨Site.js❩ Goodbye!\n')
done()
}
Graceful.on('SIGINT', this.goodbye)
Graceful.on('SIGTERM', this.goodbye)
} else {
//
// All other commands return when done so it’s ok if we block here.
//
let output = null
try {
output = await hugo.command(hugoArgs)
} catch (error) {
error.message.split('\n').forEach(line => {
console.log(`${Site.HUGO_LOGO} ${line}`)
})
console.log('\n ❌ Hugo encountered an error. Exiting… ')
console.log('\n 💕 ❨Site.js❩ Goodbye!\n')
process.exit(1)
}
output.split('\n').forEach(line => {
console.log(`${Site.HUGO_LOGO} ${line}`)
})
console.log('\n 💕 ❨Site.js❩ Goodbye!\n')
}
})()
}
......@@ -28,6 +28,8 @@ class CommandLineInterface {
['logs', arg0 === 'logs'],
['status', arg0 === 'status'],
['hugo', arg0 === 'hugo'],
['update', arg0 === 'update'],
['uninstall', arg0 === 'uninstall'],
......
......@@ -52,10 +52,13 @@ const Stats = require('./lib/Stats')
const errors = require('./lib/errors')
const HUGO_LOGO = `${clr('🅷', 'magenta')} ${clr('🆄', 'blue')} ${clr('🅶', 'green')} ${clr('🅾', 'yellow')} `
class Site {
static get HUGO_LOGO () {
return `${clr('🅷', 'magenta')} ${clr('🆄', 'blue')} ${clr('🅶', 'green')} ${clr('🅾', 'yellow')} `
}
// Emitted when the address the server is trying to use is already in use by a different process on the system.
static get SMALL_TECH_ORG_ERROR_HTTP_SERVER () { return 'small-tech.org-error-http-server' }
......@@ -357,12 +360,12 @@ class Site {
// Listen for standard output and error output on the server instance.
hugoServerProcess.stdout.on('data', (data) => {
const lines = data.toString('utf-8').split('\n')
lines.forEach(line => console.log(`${HUGO_LOGO} ${line}`))
lines.forEach(line => console.log(`${Site.HUGO_LOGO} ${line}`))
})
hugoServerProcess.stderr.on('data', (data) => {
const lines = data.toString('utf-8').split('\n')
lines.forEach(line => console.log(`${HUGO_LOGO} [ERROR] ${line}`))
lines.forEach(line => console.log(`${Site.HUGO_LOGO} [ERROR] ${line}`))
})
// Save a reference to all hugo server processes so we can
......@@ -374,7 +377,7 @@ class Site {
// Print the output received so far.
hugoBuildOutput.split('\n').forEach(line => {
console.log(`${HUGO_LOGO} ${line}`)
console.log(`${Site.HUGO_LOGO} ${line}`)
})
}
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment