Verified Commit 81bb7181 authored by Aral Balkan's avatar Aral Balkan
Browse files

Build command is working

parent 81ddb3f8
Pipeline #914 canceled with stages
#!/usr/bin/env node
//////////////////////////////////////////////////////////////////////
//
// node-hugo command-line interface (CLI).
//
// Copyright (c) 2020 Aral Balkan. All Rights Reserved.
// Licensed under AGPLv3 or later.
//
// Note: this CLI is not really useful; just use hugo directly :)
// ===== I built it to have an easy way to test the functionality
// as I was building it. (node-hugo is useful when integrated
// into an existing app, like Site.js – https://sitejs.org).
//
//////////////////////////////////////////////////////////////////////
(async function main () {
const Hugo = require('../index.js')
const hugo = new Hugo()
......@@ -13,14 +27,35 @@
showUsage()
process.exit(1)
}
console.log(`[Hugo] Build. Source: ${sourcePath} Destination: ${destinationPath}`)
console.log(`\n[Hugo] Build. Source: ${sourcePath} Destination: ${destinationPath}\n`)
let result
try {
result = await hugo.build(sourcePath, destinationPath)
} catch (error) {
console.log(error.message, error.output)
exit(1)
}
const result = hugo.build(sourcePath, destinationPath)
// Build ok.
const hugoOutput = result.split('\n')
hugoOutput.forEach(line => {
if (line.includes('WARN')) {
line = ` ⚠ ${line}`
}
line = `[node-hugo] ${line}`
console.log(line)
})
console.log(result)
} else if (command === 'serve') {
//
// TODO
//
console.log('serve (todo)', process.argv[3])
} else {
//
// Error.
//
console.log(`\nUnknown command (${command})`)
showUsage()
}
......
......@@ -9,6 +9,14 @@ const exec = util.promisify(childProcess.exec)
const homeDir = os.homedir()
class HugoError extends Error {
constructor (message, output) {
super(message)
this.sdterr = output
this.name = 'HugoError'
}
}
class Hugo {
constructor (nodeHugoDir = path.join(homeDir, '.small-tech.org', 'node-hugo')) {
......@@ -40,8 +48,13 @@ class Hugo {
env: process.env
}
const result = await exec(hugoBuildCommand, options)
console.log('Hugo build result', result)
return result
if (result.stderr !== '') {
// There was an error.
throw new HugoError('Build failed', result.stderr)
}
return result.stdout
}
// Starts a Hugo server at the requested path to serve and returns the Hugo server instance.
......@@ -105,4 +118,5 @@ class Hugo {
}
}
module.exports = Hugo
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