|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 |
| -import * as objectPath from 'object-path'; |
| 3 | +import objectPath from 'object-path'; |
| 4 | +import * as os from 'os'; |
4 | 5 | import * as path from 'path';
|
5 | 6 | import * as yargs from 'yargs';
|
6 | 7 |
|
7 | 8 | import { installHugo } from '../index';
|
8 | 9 |
|
9 | 10 | // Read CLI parameters
|
10 |
| -const argv: { [param: string]: any } = yargs |
| 11 | +const argv = yargs |
11 | 12 | .version(false) // Disable default version flag (we're using our own in the next line)
|
12 |
| - .option('version', { |
13 |
| - describe: 'Hugo version to install, or path to package.json value with the version', |
14 |
| - type: 'string', |
15 |
| - required: true, |
| 13 | + .option('arch', { |
| 14 | + choices: ['arm', 'arm64', 'x64', 'x86'], |
| 15 | + default: os.arch(), |
| 16 | + describe: 'System architecture that the binary should run on. It is recommended to use auto-detect by not using this option.', |
16 | 17 | })
|
17 | 18 | .option('destination', {
|
18 |
| - describe: 'Destination to download the Hugo binary into', |
19 |
| - type: 'string', |
20 | 19 | default: 'bin/hugo',
|
| 20 | + describe: 'Destination to download the Hugo binary into.', |
| 21 | + type: 'string', |
| 22 | + }) |
| 23 | + .option('downloadUrl', { |
| 24 | + default: 'https://github.com/gohugoio/hugo/releases/download/', |
| 25 | + describe: |
| 26 | + '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.', |
| 27 | + type: 'string', |
21 | 28 | })
|
22 | 29 | .option('extended', {
|
23 |
| - describe: 'Download Hugo extended', |
| 30 | + default: false, |
| 31 | + describe: 'Download Hugo extended version.', |
| 32 | + type: 'boolean', |
| 33 | + }) |
| 34 | + .option('os', { |
| 35 | + choices: ['darwin', 'freebsd', 'linux', 'openbsd', 'win32'], |
| 36 | + default: os.platform(), |
| 37 | + describe: 'Operating system that the binary should run on. It is recommended to use auto-detect by not using this option.', |
| 38 | + }) |
| 39 | + .option('skipChecksumCheck', { |
| 40 | + default: false, |
| 41 | + describe: 'Skip checksum checks for downloaded binaries. It is recommended to leave this option enabled.', |
24 | 42 | type: 'boolean',
|
| 43 | + }) |
| 44 | + .option('skipHealthCheck', { |
25 | 45 | default: false,
|
| 46 | + describe: 'Skip health checks for downloaded binaries. It is recommended to leave this option enabled.', |
| 47 | + type: 'boolean', |
| 48 | + }) |
| 49 | + .option('version', { |
| 50 | + describe: |
| 51 | + 'Hugo version to install, or path to package.json value with the version. Make sure to use the exact version number defined in Hugo releases.', |
| 52 | + type: 'string', |
| 53 | + required: true, |
26 | 54 | })
|
27 | 55 | .strict().argv;
|
28 | 56 |
|
29 | 57 | // If the version does not have the format of a version number, assume it's an object path
|
30 | 58 | if (isNaN(parseFloat(argv.version))) {
|
31 |
| - // Note: No is no other way in NodeJS to dynamically resolve and import the project's package.json file |
| 59 | + // Note: There is no other way in NodeJS to dynamically resolve and import the project's package.json file |
32 | 60 | // eslint-disable-next-line @typescript-eslint/no-var-requires
|
33 | 61 | const packageJson: any = require(path.resolve(process.cwd(), 'package.json'));
|
34 | 62 | const packageJsonHugoVersion: string | null = objectPath.get(packageJson, argv.version, null);
|
35 | 63 | if (!packageJsonHugoVersion) {
|
36 |
| - console.error(`Cannot find a hugo version in the package.json file at "${argv.version}"`); |
| 64 | + console.error(`The version "${argv.version}" is either invalid or not part of the "package.json" file.`); |
37 | 65 | process.exit(1);
|
38 | 66 | }
|
39 | 67 | argv.version = packageJsonHugoVersion;
|
40 | 68 | }
|
41 | 69 |
|
42 | 70 | // Run
|
43 |
| -const versionString = `${argv.version}${argv.extended ? '_extended' : ''}`; |
44 |
| -console.log(`Download hugo binary (version "${versionString}") into "${argv.destination}" ...`); |
45 |
| -installHugo(argv.version, argv.destination) |
| 71 | +installHugo({ |
| 72 | + arch: argv.arch, |
| 73 | + downloadUrl: argv.downloadUrl, |
| 74 | + destination: argv.destination, |
| 75 | + extended: argv.extended, |
| 76 | + os: argv.os, |
| 77 | + skipChecksumCheck: argv.skipChecksumCheck, |
| 78 | + skipHealthCheck: argv.skipHealthCheck, |
| 79 | + version: argv.version, |
| 80 | +}) |
46 | 81 | .then(() => {
|
47 | 82 | console.log('Success!');
|
48 | 83 | process.exit();
|
49 | 84 | })
|
50 | 85 | .catch((error: any) => {
|
51 |
| - console.error('Error!'); |
52 |
| - if (error.toString().indexOf('404') !== -1) { |
53 |
| - console.error(` -> It seems like the hugo version "${versionString}" does not exist.`); |
54 |
| - } |
55 |
| - console.log(''); |
56 |
| - console.dir(error); |
| 86 | + console.log(error); |
57 | 87 | process.exit(1);
|
58 | 88 | });
|
0 commit comments