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

Initial add

parents
Pipeline #913 canceled with stages
hugo-bin/** filter=lfs diff=lfs merge=lfs -text
node_modules
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
Nothing yet.
## [1.0.0] - TBA
Initial release.
Pull requests welcome over at the Github mirror: TBA
\ No newline at end of file
This diff is collapsed.
# 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™__ 🤞
## Installation
```sh
npm i @small-tech/node-hugo
```
## Usage
```js
const Hugo = require('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.
const hugoServer = hugo.createServer(optionalPathToHugoConfiguration)
hugoServer.on('error', (error) => {
console.log('Hugo server encountered an error', error)
})
hugoServer.on('output', (newOutput) => {
console.log(`[Hugo] ${newOutput}`)
})
hugoServer.on('stop', (code) => {
console.log('Hugo server closed with code', code)
})
await hugoServer.start()
// Close the server after 5 seconds.
setTimeout(() => {
hugoServer.stop()
}, 5000)
})()
```
#!/usr/bin/env node
(async function main () {
const Hugo = require('../index.js')
const hugo = new Hugo()
const command = process.argv[2]
if (command === 'build') {
const sourcePath = process.argv[3]
const destinationPath = process.argv[4]
if (!sourcePath || !destinationPath) {
console.log('\nError: Build command requires <sourcePath> and <destinationPath> arguments')
showUsage()
process.exit(1)
}
console.log(`[Hugo] Build. Source: ${sourcePath} Destination: ${destinationPath}`)
const result = hugo.build(sourcePath, destinationPath)
console.log(result)
} else if (command === 'serve') {
console.log('serve (todo)', process.argv[3])
} else {
console.log(`\nUnknown command (${command})`)
showUsage()
}
})()
function showUsage () {
console.log('\nUsage: hugo <command> [args]')
console.log('Supported commands: build, serve\n')
}
const os = require('os')
const path = require('path')
const fs = require('fs-extra')
const util = require('util')
const childProcess = require('child_process')
const exec = util.promisify(childProcess.exec)
const homeDir = os.homedir()
class Hugo {
constructor (nodeHugoDir = path.join(homeDir, '.small-tech.org', 'node-hugo')) {
this.machine = {
platform: os.platform(),
architecture: os.arch()
}
this.nodeHugoDir = nodeHugoDir
// Ensure the node-hugo directory exists.
fs.ensureDirSync(nodeHugoDir)
this.hugoBinaryPath = this.hugoBinaryForThisMachine()
}
//
// Public.
//
// Builds the Hugo source from sourcePath and writes the output at
// destinationPath. Note that destinationPath is relevant to sourcePath
// (don’t shoot me, this is a Hugo convention so I’m mirroring it for
// consistency with regular Hugo usage). The returned result is the
// object returned from the exec() call with stdout and stderr properties.
async build (sourcePath = '.', destinationPath = 'public/') {
const hugoBuildCommand = `${this.hugoBinaryPath} --source=${sourcePath} --destination=${destinationPath}`
const options = {
env: process.env
}
const result = await exec(hugoBuildCommand, options)
console.log('Hugo build result', result)
return result
}
// Starts a Hugo server at the requested path to serve and returns the Hugo server instance.
async serve (pathToServe) {
throw new Error('Unimplemented.')
}
//
// Private.
//
// Returns the Hugo binary for this machine (platform + architecture) and
// throws an error if there isn’t one for it.
//
// Note: this expects the Hugo binaries to be manually renamed prior to being
// ===== added to the hugo-bin folder. The naming convention we use is the same
// as the one we use in the nodecert project for the mkcert binaries.
hugoBinaryForThisMachine () {
const platformMap = {
linux: 'linux',
darwin: 'darwin',
win32: 'windows'
}
const architectureMap = {
arm: 'arm',
x64: 'amd64'
}
const platform = platformMap[this.machine.platform]
const architecture = architectureMap[this.machine.architecture]
if (platform === undefined) throw new Error('Unsupported platform', this.machine.platform)
if (architecture === undefined) throw new Error('Unsupported architecture', this.machine.architecture)
const hugoVersion = '0.64.0'
const hugoBinaryName = `hugo-v${hugoVersion}-${platform}-${architecture}`
const hugoBinaryRelativePath = path.join('hugo-bin', hugoBinaryName)
let hugoBinaryInternalPath = path.join(__dirname, hugoBinaryRelativePath)
if (platform === 'windows') hugoBinaryInternalPath += '.exe'
// Check if the platform + architecture combination is supported.
if (!fs.existsSync(hugoBinaryInternalPath)) throw new Error(`[node-hugo] Unsupported platform + architecture combination for ${platform}-${architecture}`)
// Copy the Hugo binary to the external node-hugo directory so that we can call execSync() on it if
// the app using this module is wrapped into an executable using Nexe (https://github.com/nexe/nexe) – like
// Site.js (https://sitejs.org) is, for example. We use readFileSync() and writeFileSync() as
// Nexe does not support copyFileSync() yet (see https://github.com/nexe/nexe/issues/607).
const hugoBinaryExternalPath = path.join(this.nodeHugoDir, hugoBinaryName)
try {
const hugoBinaryBuffer = fs.readFileSync(hugoBinaryInternalPath, 'binary')
fs.writeFileSync(hugoBinaryExternalPath, hugoBinaryBuffer, {encoding: 'binary', mode: 0o755})
} catch (error) {
throw new Error(` 🤯 [node-hugo] Panic: Could not copy Hugo binary to external directory: ${error.message}`)
}
return hugoBinaryExternalPath
}
}
module.exports = Hugo
{
"name": "@small-tech/node-hugo",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
}
},
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
"dev": true
},
"deep-equal": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz",
"integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=",
"dev": true
},
"define-properties": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
"integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
"dev": true,
"requires": {
"object-keys": "^1.0.12"
}
},
"defined": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz",
"integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=",
"dev": true
},
"es-abstract": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz",
"integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==",
"dev": true,
"requires": {
"es-to-primitive": "^1.2.0",
"function-bind": "^1.1.1",
"has": "^1.0.3",
"is-callable": "^1.1.4",
"is-regex": "^1.0.4",
"object-keys": "^1.0.12"
}
},
"es-to-primitive": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz",
"integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==",
"dev": true,
"requires": {
"is-callable": "^1.1.4",
"is-date-object": "^1.0.1",
"is-symbol": "^1.0.2"
}
},
"for-each": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz",
"integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==",
"dev": true,
"requires": {
"is-callable": "^1.1.3"
}
},
"fs-extra": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
"integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
"dev": true
},
"function-bind": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==",
"dev": true
},
"glob": {
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz",
"integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==",
"dev": true,
"requires": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
"inherits": "2",
"minimatch": "^3.0.4",
"once": "^1.3.0",
"path-is-absolute": "^1.0.0"
}
},
"graceful-fs": {
"version": "4.2.3",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.3.tgz",
"integrity": "sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ=="
},
"has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
"integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
"dev": true,
"requires": {
"function-bind": "^1.1.1"
}
},
"has-symbols": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz",
"integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=",
"dev": true
},
"inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
"dev": true,
"requires": {
"once": "^1.3.0",
"wrappy": "1"
}
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=",
"dev": true
},
"is-callable": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz",
"integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==",
"dev": true
},
"is-date-object": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz",
"integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=",
"dev": true
},
"is-regex": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz",
"integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=",
"dev": true,
"requires": {
"has": "^1.0.1"
}
},
"is-symbol": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz",
"integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==",
"dev": true,
"requires": {
"has-symbols": "^1.0.0"
}
},
"jsonfile": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"requires": {
"graceful-fs": "^4.1.6"
}
},
"minimatch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": {
"brace-expansion": "^1.1.7"
}
},
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=",
"dev": true
},
"object-inspect": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz",
"integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==",
"dev": true
},
"object-keys": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.0.tgz",
"integrity": "sha512-6OO5X1+2tYkNyNEx6TsCxEqFfRWaqx6EtMiSbGrw8Ob8v9Ne+Hl8rBAgLBZn5wjEz3s/s6U1WXFUFOcxxAwUpg==",
"dev": true
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"dev": true,
"requires": {
"wrappy": "1"
}
},
"path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
"dev": true
},
"path-parse": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz",
"integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==",
"dev": true
},
"resolve": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.0.tgz",
"integrity": "sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==",
"dev": true,
"requires": {
"path-parse": "^1.0.6"
}
},
"resumer": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz",
"integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=",
"dev": true,
"requires": {
"through": "~2.3.4"
}
},
"string.prototype.trim": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz",
"integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=",
"dev": true,
"requires": {
"define-properties": "^1.1.2",
"es-abstract": "^1.5.0",
"function-bind": "^1.0.2"
}
},
"tape": {
"version": "4.10.1",
"resolved": "https://registry.npmjs.org/tape/-/tape-4.10.1.tgz",
"integrity": "sha512-G0DywYV1jQeY3axeYnXUOt6ktnxS9OPJh97FGR3nrua8lhWi1zPflLxcAHavZ7Jf3qUfY7cxcVIVFa4mY2IY1w==",
"dev": true,
"requires": {
"deep-equal": "~1.0.1",
"defined": "~1.0.0",
"for-each": "~0.3.3",
"function-bind": "~1.1.1",
"glob": "~7.1.3",
"has": "~1.0.3",
"inherits": "~2.0.3",
"minimist": "~1.2.0",
"object-inspect": "~1.6.0",
"resolve": "~1.10.0",
"resumer": "~0.0.0",
"string.prototype.trim": "~1.1.2",
"through": "~2.3.8"
}
},
"through": {
"version": "2.3.8",
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=",
"dev": true
},
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
},
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true
}
}
}
{
"name": "@small-tech/node-hugo",
"version": "1.0.0",
"description": "A basic cross-platform interface to the Hugo binary from Node.js.",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "tape test.js"
},
"repository": {
"type": "git",
"url": "git@source.ind.ie:site.js/lib/node-hugo.git"
},
"author": {
"name": "Aral Balkan",
"email": "aral@small-tech.org",
"url": "https://ar.al"
},
"license": "AGPL-3.0-or-later",
"dependencies": {
"fs-extra": "^8.1.0"
},
"devDependencies": {
"tape": "^4.13.0"
},
"pkg": {
"assets": "hugo-bin/*"
}
}
// Nothing yet.
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Categories on My New Hugo Site</title>
<link>http://example.org/categories/</link>
<description>Recent content in Categories on My New Hugo Site</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<atom:link href="http://example.org/categories/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My New Hugo Site</title>
<link>http://example.org/</link>
<description>Recent content on My New Hugo Site</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<atom:link href="http://example.org/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>