1
mirror of https://github.com/jakejarvis/hugo-extended.git synced 2025-09-15 22:35:31 -04:00

When Hugo seems to disappear, just reinstall and then continue normally (#82)

should fix #81 (or at least mitigate it)
This commit is contained in:
2021-11-01 14:06:51 -04:00
committed by GitHub
parent b14f7abeb2
commit 8251c012de
10 changed files with 165 additions and 93 deletions

View File

@@ -3,10 +3,13 @@
import { spawn } from "child_process";
import hugo from "../index.js";
const args = process.argv.slice(2);
(async () => {
const args = process.argv.slice(2);
const bin = await hugo();
spawn(hugo, args, { stdio: "inherit" })
.on("exit", (code) => {
// forward Hugo's exit code so this module itself reports success/failure
process.exit(code);
});
spawn(bin, args, { stdio: "inherit" })
.on("exit", (code) => {
// forward Hugo's exit code so this module itself reports success/failure
process.exit(code);
});
})();

View File

@@ -1,5 +1,6 @@
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import downloader from "careful-downloader";
import logSymbols from "log-symbols";
import {
@@ -12,50 +13,50 @@ import {
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) => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function install() {
try {
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.`);
}
// download release from GitHub and verify its checksum
const download = await downloader(getReleaseUrl(version, releaseFile), {
checksumUrl: getReleaseUrl(version, checksumFile),
filename: releaseFile,
destDir: path.join(__dirname, "..", "vendor"),
algorithm: "sha256",
extract: true,
});
// full path to the binary
const installedToPath = path.join(download, binFile);
// ensure hugo[.exe] is executable
fs.chmodSync(installedToPath, 0o755);
console.info(`${logSymbols.success} Hugo installed successfully!`);
console.info(getBinVersion(installedToPath));
// return the full path to our Hugo binary
return installedToPath;
} 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.`);
}
// download release from GitHub and verify its checksum
const download = await downloader(getReleaseUrl(version, releaseFile), {
checksumUrl: getReleaseUrl(version, checksumFile),
filename: releaseFile,
destDir: "vendor",
algorithm: "sha256",
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);
}
export default install;

View File

@@ -1,11 +1,16 @@
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import { execFileSync } from "child_process";
import { readPackageUpSync } from "read-pkg-up";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// This package's version number (should) always match the Hugo release we want.
// We check for a `hugoVersion` field in package.json just in case it doesn't
// match in the future (from pushing an emergency package update, etc.).
export function getPkgVersion() {
const { packageJson } = readPackageUpSync();
const { packageJson } = readPackageUpSync({ cwd: __dirname });
return packageJson.hugoVersion || packageJson.version;
}
@@ -19,6 +24,16 @@ export function getBinFilename() {
return process.platform === "win32" ? "hugo.exe" : "hugo";
}
// Simple shortcut to ./vendor/hugo[.exe] from package root.
export function getBinPath() {
return path.join(
__dirname,
"..",
"vendor",
getBinFilename(),
);
}
// Returns the output of the `hugo version` command, i.e.:
// "hugo v0.88.1-5BC54738+extended darwin/arm64 BuildDate=..."
export function getBinVersion(bin) {
@@ -26,6 +41,22 @@ export function getBinVersion(bin) {
return stdout.toString().trim();
}
// Simply detect if the given file exists.
export function doesBinExist(bin) {
try {
if (fs.existsSync(bin)) {
return true;
}
} catch (error) {
// something bad happened besides Hugo not existing
if (error.code !== "ENOENT") {
throw error;
}
return false;
}
}
// 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. :)