hugo-installer
Version:
Installs hugo into your repository.
114 lines (112 loc) • 4.76 kB
JavaScript
#!/usr/bin/env node
import * as fs from 'node:fs';
import objectPath from 'object-path';
import * as os from 'node:os';
import * as path from 'node:path';
import semver from 'semver';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { installHugo } from '../index.js';
const argv = yargs(hideBin(process.argv))
.version(false)
.option('arch', {
choices: ['arm', 'arm64', 'x64', 'x86'],
default: os.arch(),
describe: 'System architecture that the binary will run on. It is recommended to use auto-detect by not using this option.',
})
.option('destination', {
default: 'bin/hugo',
describe: 'Path to the folder into which the binary will be put. Make sure to add this path to your "gitignore" file.',
type: 'string',
})
.option('downloadUrl', {
default: 'https://github.com/gohugoio/hugo/releases/download/',
describe: 'Source base URL from where the Hugo binary will be fetched. By default, GitHub will be used. When using a custom URL, make sure to replicate GitHub release asset URLs and append a trailing slash to the custom URL.',
type: 'string',
})
.option('extended', {
default: false,
describe: 'Download the extended version of Hugo.',
type: 'boolean',
})
.option('force', {
default: false,
describe: 'Force clean install of Hugo, ignoring already installed / cached binaries.',
type: 'boolean',
})
.option('httpProxy', {
default: process.env.HTTP_PROXY || null,
describe: 'HTTP Proxy URL, used when downloading Hugo binaries. Useful when working behind corporate proxies. Can also be configured using the "HTTP_PROXY" environment variable, the CLI argument (if used) will take precedence.',
type: 'string',
})
.option('httpsProxy', {
default: process.env.HTTPS_PROXY || null,
describe: 'HTTPS Proxy URL, used when downloading Hugo binaries. Useful when working behind corporate proxies. Can also be configured using the "HTTPS_PROXY" environment variable, the CLI argument (if used) will take precedence.',
type: 'string',
})
.option('os', {
choices: ['darwin', 'freebsd', 'linux', 'openbsd', 'win32'],
default: os.platform(),
describe: 'Operating system that the binary should run on. It is recommended to use auto-detect by not using this option.',
})
.option('skipChecksumCheck', {
default: false,
describe: 'Skip checksum checks for downloaded binaries. It is recommended to leave this option enabled.',
type: 'boolean',
})
.option('skipHealthCheck', {
default: false,
describe: 'Skip health checks for downloaded binaries. It is recommended to leave this option enabled.',
type: 'boolean',
})
.option('version', {
describe: 'Hugo version to install, or path to package.json entry with the version. Make sure to use the exact version number as defined in the official Hugo GitHub releases.',
type: 'string',
required: true,
})
.strict().argv;
const bin = async (options) => {
if (semver.valid(argv.version) === null) {
let packageJsonContent;
try {
const packageJsonRaw = await fs.promises.readFile(path.resolve(process.cwd(), 'package.json'), { encoding: 'utf-8' });
packageJsonContent = JSON.parse(packageJsonRaw);
}
catch (error) {
console.error(`The version points to a property in the "package.json" file, but the file cannot be read. Details: ${error.message}`);
process.exit(1);
}
const packageJsonHugoVersion = objectPath.get(packageJsonContent, argv.version, null);
if (packageJsonHugoVersion === null) {
console.error(`The version points to a property in the "package.json" file, but the property does not exist.`);
process.exit(1);
}
if (semver.valid(packageJsonHugoVersion) === null) {
console.error('The version points to a property in the "package.json" file, but the version defined there is not valid.');
process.exit(1);
}
options.version = packageJsonHugoVersion;
}
installHugo(options)
.then(() => {
console.log('Success!');
process.exit();
})
.catch((error) => {
console.log(error);
process.exit(1);
});
};
bin({
arch: argv.arch,
destination: argv.destination,
downloadUrl: argv.downloadUrl,
extended: argv.extended,
force: argv.force,
httpProxy: argv.httpProxy,
httpsProxy: argv.httpsProxy,
os: argv.os,
skipChecksumCheck: argv.skipChecksumCheck,
skipHealthCheck: argv.skipHealthCheck,
version: argv.version,
});