Verified Commit 83918a94 authored by Aral Balkan's avatar Aral Balkan
Browse files

Use stdio: pipe instead of inherit and handle output manually

parent c80904cf
......@@ -33,26 +33,28 @@ const Hugo = require('node-hugo')
console.log('Site build successful. Output:', hugo.output)
// Start Hugo server.
const hugoServer = hugo.createServer(optionalPathToHugoConfiguration)
// Start Hugo server. Returns a ChildProcess instance.
const hugoServerProcess = hugo.createServer(optionalPathToHugoConfiguration)
hugoServer.on('error', (error) => {
hugoServerProcess.on('error', (error) => {
console.log('Hugo server encountered an error', error)
})
hugoServer.on('output', (newOutput) => {
console.log(`[Hugo] ${newOutput}`)
hugoServerProcess.stdout.on('data', (data) => {
console.log(`[Hugo] ${data}`)
})
hugoServer.on('stop', (code) => {
console.log('Hugo server closed with code', code)
hugoServerProcess.stderr.on('data', (data) => {
console.log(`[Hugo] [ERROR] ${data}`)
})
await hugoServer.start()
hugoServerProcess.on('close', (code) => {
console.log('Hugo server process exited with code', code)
})
// Close the server after 5 seconds.
setTimeout(() => {
hugoServer.stop()
hugoServerProcess.kill()
}, 5000)
})()
......
......@@ -66,9 +66,29 @@
process.exit(1)
}
// Logs the data returned from a child process’s stdout and stderr
// to the console, prepending an extensible custom prefix to each line.
function log (data, additionalPrefix = ' ') {
const lines = data.toString('utf-8').split('\n')
lines.forEach(line => console.log(`[node-hugo] [server]${additionalPrefix}${line}`))
}
console.log(`\n[node-hugo] Serve. Source: ${sourcePath} Destination: ${destinationPath} Base URL: ${baseURL}\n`)
hugo.serve(sourcePath, destinationPath, baseURL)
const hugoServerProcess = hugo.serve(sourcePath, destinationPath, baseURL)
hugoServerProcess.stdout.on('data', (data) => {
log(data)
})
hugoServerProcess.stderr.on('data', (data) => {
log(data, ' [ERROR] ')
})
hugoServerProcess.on('close', (code) => {
console.log('[node-hugo] [server] Hugo server process exited with code', code)
})
} else {
//
// Error.
......
......@@ -74,7 +74,7 @@ class Hugo {
'--disableLiveReload',
'--appendPort=false',
]
const options = { env: process.env, stdio: 'inherit' }
const options = { env: process.env }
const hugoServer = spawn(this.hugoBinaryPath, args, options)
return hugoServer
}
......
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