You've already forked hugo-extended
mirror of
https://github.com/jakejarvis/hugo-extended.git
synced 2025-09-15 22:35:31 -04:00
refactor directory structure and modularize a few common functions
This commit is contained in:
9
lib/cli.js
Executable file
9
lib/cli.js
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawn } from "child_process";
|
||||
import hugo from "../index.js";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
spawn(hugo, args, { stdio: "inherit" })
|
||||
.on("exit", process.exit);
|
57
lib/install.js
Normal file
57
lib/install.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import downloader from "careful-downloader";
|
||||
import logSymbols from "log-symbols";
|
||||
import { getPkgVersion, getReleaseUrl, getReleaseFilename, getBinFilename, getBinVersion, getChecksumFilename, isExtended } from "./utils.js";
|
||||
|
||||
installHugo()
|
||||
.then((bin) =>
|
||||
// try querying hugo's version via CLI
|
||||
getBinVersion(bin),
|
||||
)
|
||||
.then((version) => {
|
||||
// print output of `hugo version` to console
|
||||
console.log(`${logSymbols.success} Hugo installed successfully!`);
|
||||
console.log(version);
|
||||
})
|
||||
.catch((error) => {
|
||||
// pass whatever error occured along the way to console
|
||||
console.error(`${logSymbols.error} Hugo installation failed. :(`);
|
||||
throw error;
|
||||
});
|
||||
|
||||
async function installHugo() {
|
||||
const version = getPkgVersion();
|
||||
const releaseFile = getReleaseFilename(version);
|
||||
const checksumFile = getChecksumFilename(version);
|
||||
const binFile = getBinFilename();
|
||||
|
||||
// stop here if there's nothing we can download
|
||||
if (!releaseFile) {
|
||||
throw new Error(`Are you sure this platform is supported? See: https://github.com/gohugoio/hugo/releases/tag/v${version}`);
|
||||
}
|
||||
|
||||
// warn if platform doesn't support Hugo Extended, proceed with vanilla Hugo
|
||||
if (!isExtended(releaseFile)) {
|
||||
console.warn(`${logSymbols.info} Hugo Extended isn't supported on this platform, downloading vanilla Hugo instead.`);
|
||||
}
|
||||
|
||||
const download = await downloader(
|
||||
getReleaseUrl(version, releaseFile),
|
||||
getReleaseUrl(version, checksumFile),
|
||||
{
|
||||
filename: releaseFile,
|
||||
destDir: "vendor",
|
||||
cleanDestDir: false,
|
||||
algorithm: "sha256",
|
||||
encoding: "binary",
|
||||
extract: true,
|
||||
},
|
||||
);
|
||||
|
||||
// ensure hugo[.exe] is executable
|
||||
fs.chmodSync(path.join(download, binFile), 0o755);
|
||||
|
||||
// return the full path to our Hugo binary
|
||||
return path.join(download, binFile);
|
||||
}
|
92
lib/utils.js
Normal file
92
lib/utils.js
Normal file
@@ -0,0 +1,92 @@
|
||||
import { readPackageUpSync } from "read-pkg-up";
|
||||
import { execFileSync } from "child_process";
|
||||
|
||||
// This package's version number (should) always match the Hugo release we want.
|
||||
export function getPkgVersion() {
|
||||
const { packageJson } = readPackageUpSync();
|
||||
return packageJson.version;
|
||||
}
|
||||
|
||||
// Generate the full GitHub URL to a given release file.
|
||||
export function getReleaseUrl(version, filename) {
|
||||
return `https://github.com/gohugoio/hugo/releases/download/v${version}/${filename}`;
|
||||
}
|
||||
|
||||
// Binary is named `hugo.exe` on Windows, and simply `hugo` otherwise.
|
||||
export function getBinFilename() {
|
||||
return process.platform === "win32" ? "hugo.exe" : "hugo";
|
||||
}
|
||||
|
||||
// Returns the output of the `hugo version` command, i.e.:
|
||||
// "hugo v0.88.1-5BC54738+extended darwin/arm64 BuildDate=..."
|
||||
export function getBinVersion(bin) {
|
||||
const stdout = execFileSync(bin, ["version"]);
|
||||
return stdout.toString();
|
||||
}
|
||||
|
||||
// Hugo Extended supports: macOS x64, macOS ARM64, Linux x64, Windows x64.
|
||||
// all other combos fall back to vanilla Hugo. There are surely much better ways
|
||||
// to do this but this is easy to read/update. :)
|
||||
export function getReleaseFilename(version) {
|
||||
const { platform, arch } = process;
|
||||
|
||||
const filename =
|
||||
// macOS
|
||||
platform === "darwin" && arch === "x64" ?
|
||||
`hugo_extended_${version}_macOS-64bit.tar.gz` :
|
||||
platform === "darwin" && arch === "arm64" ?
|
||||
`hugo_extended_${version}_macOS-ARM64.tar.gz` :
|
||||
|
||||
// Windows
|
||||
platform === "win32" && arch === "x64" ?
|
||||
`hugo_extended_${version}_Windows-64bit.zip` :
|
||||
platform === "win32" && arch.endsWith("32") ?
|
||||
`hugo_${version}_Windows-32bit.zip` :
|
||||
platform === "win32" && "arm" ?
|
||||
`hugo_${version}_Windows-ARM.zip` :
|
||||
|
||||
// Linux
|
||||
platform === "linux" && arch === "x64" ?
|
||||
`hugo_extended_${version}_Linux-64bit.tar.gz` :
|
||||
platform === "linux" && arch.endsWith("32") ?
|
||||
`hugo_${version}_Linux-32bit.tar.gz` :
|
||||
platform === "linux" && arch === "arm" ?
|
||||
`hugo_${version}_Linux-ARM.tar.gz` :
|
||||
platform === "linux" && arch === "arm64" ?
|
||||
`hugo_${version}_Linux-ARM64.tar.gz` :
|
||||
|
||||
// FreeBSD
|
||||
platform === "freebsd" && arch === "x64" ?
|
||||
`hugo_${version}_FreeBSD-64bit.tar.gz` :
|
||||
platform === "freebsd" && arch.endsWith("32") ?
|
||||
`hugo_${version}_FreeBSD-32bit.tar.gz` :
|
||||
platform === "freebsd" && arch === "arm" ?
|
||||
`hugo_${version}_FreeBSD-ARM.tar.gz` :
|
||||
platform === "freebsd" && arch === "arm64" ?
|
||||
`hugo_${version}_FreeBSD-ARM64.tar.gz` :
|
||||
|
||||
// OpenBSD
|
||||
platform === "openbsd" && arch === "x64" ?
|
||||
`hugo_${version}_OpenBSD-64bit.tar.gz` :
|
||||
platform === "openbsd" && arch.endsWith("32") ?
|
||||
`hugo_${version}_OpenBSD-32bit.tar.gz` :
|
||||
platform === "openbsd" && arch === "arm" ?
|
||||
`hugo_${version}_OpenBSD-ARM.tar.gz` :
|
||||
platform === "openbsd" && arch === "arm64" ?
|
||||
`hugo_${version}_OpenBSD-ARM64.tar.gz` :
|
||||
|
||||
// not gonna work :(
|
||||
null;
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
// Simple formula for the checksums.txt file.
|
||||
export function getChecksumFilename(version) {
|
||||
return `hugo_${version}_checksums.txt`;
|
||||
}
|
||||
|
||||
// Check if Hugo extended is being downloaded (as opposed to plain Hugo) based on the release filename.
|
||||
export function isExtended(releaseFile) {
|
||||
return releaseFile.startsWith("hugo_extended_");
|
||||
}
|
Reference in New Issue
Block a user