1
mirror of https://github.com/jakejarvis/hugo-extended.git synced 2025-04-26 07:55:21 -04:00

Switch to more modern and lightweight dependencies

This commit is contained in:
Jake Jarvis 2021-06-01 19:15:48 -04:00
parent 2c4d747aee
commit d87c63f5d3
8 changed files with 253 additions and 1895 deletions

View File

@ -1,3 +1,7 @@
'use strict';
module.exports = require('./lib').path();
module.exports = require("path").join(
__dirname,
'vendor',
process.platform === 'win32' ? 'hugo.exe' : 'hugo'
);

104
install.js Normal file
View File

@ -0,0 +1,104 @@
'use strict';
const fs = require("fs");
const path = require("path");
const { https } = require("follow-redirects");
const decompress = require('decompress');
(async () => {
install()
.then(() => {
console.log('✔ Hugo installed successfully!');
})
.catch((error) => {
console.error('✖ ERROR: Hugo installation failed. :(\n', error);
});
})();
async function install() {
// this package's version number (should) always matches the Hugo version we want
const { version } = require('./package.json');
const downloadBaseUrl = `https://github.com/gohugoio/hugo/releases/download/v${version}/`;
// Hugo Extended supports: macOS x64, macOS ARM64, Linux x64, Windows x64.
// all other combos fall back to vanilla Hugo.
const downloadFile =
process.platform === 'darwin' && process.arch === 'x64'
? `hugo_extended_${version}_macOS-64bit.tar.gz` :
process.platform === 'darwin' && process.arch === 'arm64'
? `hugo_extended_${version}_macOS-ARM64.tar.gz` :
process.platform === 'win32' && process.arch === 'x64'
? `hugo_extended_${version}_Windows-64bit.zip` :
process.platform === 'win32' && process.arch.endsWith('32')
? `hugo_${version}_Windows-32bit.zip` :
process.platform === 'linux' && process.arch === 'x64'
? `hugo_extended_${version}_Linux-64bit.tar.gz` :
process.platform === 'linux' && process.arch.endsWith('32')
? `hugo_${version}_Linux-32bit.tar.gz` :
process.platform === 'linux' && process.arch === 'arm'
? `hugo_${version}_Linux-ARM.tar.gz` :
process.platform === 'linux' && process.arch === 'arm64'
? `hugo_${version}_Linux-ARM64.tar.gz` :
process.platform === 'freebsd' && process.arch === 'x64'
? `hugo_${version}_FreeBSD-64bit.tar.gz` :
process.platform === 'freebsd' && process.arch.endsWith('32')
? `hugo_${version}_FreeBSD-32bit.tar.gz` :
process.platform === 'freebsd' && process.arch === 'arm'
? `hugo_${version}_FreeBSD-ARM.tar.gz` :
process.platform === 'freebsd' && process.arch === 'arm64'
? `hugo_${version}_FreeBSD-ARM64.tar.gz` :
process.platform === 'openbsd' && process.arch === 'x64'
? `hugo_${version}_OpenBSD-64bit.tar.gz` :
process.platform === 'openbsd' && process.arch.endsWith('32')
? `hugo_${version}_OpenBSD-32bit.tar.gz` :
process.platform === 'openbsd' && process.arch === 'arm'
? `hugo_${version}_OpenBSD-ARM.tar.gz` :
process.platform === 'openbsd' && process.arch === 'arm64'
? `hugo_${version}_OpenBSD-ARM64.tar.gz` :
null;
// stop here if there's nothing we can download
if (!downloadFile) throw "Are you sure this platform is supported?";
let downloadUrl = downloadBaseUrl + downloadFile;
let vendorDir = path.join(__dirname, 'vendor');
let archivePath = path.join(vendorDir, downloadFile);
let binName = process.platform === 'win32' ? 'hugo.exe' : 'hugo';
let binPath = path.join(vendorDir, binName);
try {
// ensure the target directory exists
await fs.promises.mkdir(vendorDir, { recursive: true });
// fetch the archive file from GitHub
await new Promise((resolve, reject) => https.get(downloadUrl, response => {
// throw an error immediately if the download failed
if (response.statusCode !== 200) {
response.resume();
reject(new Error(`Download failed: status code ${response.statusCode} from ${downloadUrl}`));
return;
}
// pipe the response directly to a file
response.pipe(
fs.createWriteStream(archivePath)
.on('finish', resolve)
.on('error', reject)
);
}).on('error', reject));
// TODO: validate the checksum of the download
// https://github.com/jakejarvis/hugo-extended/issues/1
// extract the downloaded file
await decompress(archivePath, vendorDir);
} finally {
// delete the downloaded archive when finished
if (fs.existsSync(archivePath)) {
await fs.promises.unlink(archivePath);
}
}
// return the full path to our Hugo binary
return binPath;
}

View File

@ -1,10 +0,0 @@
'use strict';
const path = require('path');
const BinWrapper = require('bin-wrapper');
const src = require('./src');
module.exports = new BinWrapper()
.src(src)
.dest(path.join(__dirname, '../vendor'))
.use(process.platform === 'win32' ? 'hugo.exe' : 'hugo');

View File

@ -1,12 +0,0 @@
'use strict';
const bin = require('.');
(async () => {
try {
await bin.run(['version']);
console.log('✔ Hugo installed successfully!');
} catch (error) {
console.error('✖ ERROR: Hugo installation failed. :( Are you sure this platform is supported?\n', error);
}
})();

View File

@ -1,50 +0,0 @@
'use strict';
const { version } = require('../package.json');
const baseUrl = `https://github.com/gohugoio/hugo/releases/download/v${version}/`;
// Platforms: https://nodejs.org/api/process.html#process_process_platform
// Architectures: https://nodejs.org/api/process.html#process_process_arch
// Hugo Extended supports: macOS x64, macOS ARM64, Linux x64, Windows x64.
// all other combos fall back to vanilla Hugo.
module.exports =
process.platform === 'darwin' && process.arch === 'x64'
? `${baseUrl}hugo_extended_${version}_macOS-64bit.tar.gz` :
process.platform === 'darwin' && process.arch === 'arm64'
? `${baseUrl}hugo_extended_${version}_macOS-ARM64.tar.gz` :
process.platform === 'win32' && process.arch === 'x64'
? `${baseUrl}hugo_extended_${version}_Windows-64bit.zip` :
process.platform === 'win32' && process.arch.endsWith('32')
? `${baseUrl}hugo_${version}_Windows-32bit.zip` :
process.platform === 'linux' && process.arch === 'x64'
? `${baseUrl}hugo_extended_${version}_Linux-64bit.tar.gz` :
process.platform === 'linux' && process.arch.endsWith('32')
? `${baseUrl}hugo_${version}_Linux-32bit.tar.gz` :
process.platform === 'linux' && process.arch === 'arm'
? `${baseUrl}hugo_${version}_Linux-ARM.tar.gz` :
process.platform === 'linux' && process.arch === 'arm64'
? `${baseUrl}hugo_${version}_Linux-ARM64.tar.gz` :
process.platform === 'freebsd' && process.arch === 'x64'
? `${baseUrl}hugo_${version}_FreeBSD-64bit.tar.gz` :
process.platform === 'freebsd' && process.arch.endsWith('32')
? `${baseUrl}hugo_${version}_FreeBSD-32bit.tar.gz` :
process.platform === 'freebsd' && process.arch === 'arm'
? `${baseUrl}hugo_${version}_FreeBSD-ARM.tar.gz` :
process.platform === 'freebsd' && process.arch === 'arm64'
? `${baseUrl}hugo_${version}_FreeBSD-ARM64.tar.gz` :
process.platform === 'openbsd' && process.arch === 'x64'
? `${baseUrl}hugo_${version}_OpenBSD-64bit.tar.gz` :
process.platform === 'openbsd' && process.arch.endsWith('32')
? `${baseUrl}hugo_${version}_OpenBSD-32bit.tar.gz` :
process.platform === 'openbsd' && process.arch === 'arm'
? `${baseUrl}hugo_${version}_OpenBSD-ARM.tar.gz` :
process.platform === 'openbsd' && process.arch === 'arm64'
? `${baseUrl}hugo_${version}_OpenBSD-ARM64.tar.gz` :
null;

1941
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,9 +13,9 @@
"url": "git+https://github.com/jakejarvis/hugo-extended.git"
},
"files": [
"lib/*.js",
"index.js",
"cli.js",
"index.js"
"install.js"
],
"bin": {
"hugo": "cli.js",
@ -23,16 +23,17 @@
},
"main": "index.js",
"dependencies": {
"bin-wrapper": "^4.1.0"
"cross-spawn": "^7.0.3",
"decompress": "^4.2.1",
"follow-redirects": "^1.14.1"
},
"devDependencies": {
"bin-check": "^4.1.0",
"eslint": "^7.27.0",
"mocha": "^8.4.0"
},
"scripts": {
"postinstall": "node lib/install.js",
"test": "eslint \"**/*.js\" && mocha && node cli.js env"
"postinstall": "node install.js",
"test": "eslint \"**/*.js\" && mocha"
},
"engines": {
"node": ">=10"
@ -63,7 +64,8 @@
"sourceType": "module"
},
"env": {
"node": true
"node": true,
"es6": true
}
}
}

View File

@ -2,10 +2,13 @@
'use strict';
const { execFile } = require('child_process');
const assert = require('assert');
const binCheck = require('bin-check');
const hugoBin = require('..');
const hugo = require('..');
it('Hugo exists and runs?', async () => {
assert(await binCheck(hugoBin, ['version']));
assert(execFile(hugo, ['env'], (error, stdout) => {
if (error) throw error;
console.log(stdout);
}));
});