-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
123 lines (106 loc) · 4.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { debuglog } from 'node:util';
import BinWrapper from '@xhmikosr/bin-wrapper';
import { packageConfig } from 'package-config';
const debug = debuglog('hugo-bin');
const pkg = new URL('../package.json', import.meta.url);
const { hugoVersion: HUGO_VERSION } = JSON.parse(await fs.readFile(pkg, 'utf8'));
const destDir = path.join(fileURLToPath(new URL('../vendor/', import.meta.url)));
const binName = process.platform === 'win32' ? 'hugo.exe' : 'hugo';
debug('[global] HUGO_VERSION:', HUGO_VERSION);
debug('[global] destDir:', destDir);
debug('[global] binName:', binName);
/**
* @param {string} baseUrl
* @param {string} hugoVersion
*/
function extendedBin(baseUrl, hugoVersion) {
debug('[extendedBin] baseUrl:', baseUrl);
debug('[extendedBin] hugoVersion:', hugoVersion);
const baseName = `hugo_${hugoVersion}`;
const baseNameExtended = `hugo_extended_${hugoVersion}`;
return new BinWrapper()
.src(`${baseUrl}${baseNameExtended}_darwin-universal.tar.gz`, 'darwin', 'arm64')
.src(`${baseUrl}${baseNameExtended}_darwin-universal.tar.gz`, 'darwin', 'x64')
.src(`${baseUrl}${baseNameExtended}_linux-amd64.tar.gz`, 'linux', 'x64')
.src(`${baseUrl}${baseNameExtended}_linux-arm64.tar.gz`, 'linux', 'arm64')
.src(`${baseUrl}${baseNameExtended}_windows-amd64.zip`, 'win32', 'x64')
// Fall back to the normal version on unsupported platforms
.src(`${baseUrl}${baseName}_dragonfly-amd64.tar.gz`, 'dragonflybsd', 'x64')
.src(`${baseUrl}${baseName}_freebsd-amd64.tar.gz`, 'freebsd', 'x64')
.src(`${baseUrl}${baseName}_linux-arm.tar.gz`, 'linux', 'arm')
.src(`${baseUrl}${baseName}_netbsd-amd64.tar.gz`, 'netbsd', 'x64')
.src(`${baseUrl}${baseName}_openbsd-amd64.tar.gz`, 'openbsd', 'x64')
.src(`${baseUrl}${baseName}_solaris-amd64.tar.gz`, 'openbsd', 'x64')
.src(`${baseUrl}${baseName}_windows-arm64.zip`, 'win32', 'arm64')
.dest(destDir)
.use(binName);
}
/**
* @param {string} baseUrl
* @param {string} hugoVersion
*/
function normalBin(baseUrl, hugoVersion) {
debug('[normalBin] baseUrl:', baseUrl);
debug('[normalBin] hugoVersion:', hugoVersion);
const baseName = `hugo_${hugoVersion}`;
return new BinWrapper()
.src(`${baseUrl}${baseName}_darwin-universal.tar.gz`, 'darwin', 'arm64')
.src(`${baseUrl}${baseName}_darwin-universal.tar.gz`, 'darwin', 'x64')
.src(`${baseUrl}${baseName}_dragonfly-amd64.tar.gz`, 'dragonflybsd', 'x64')
.src(`${baseUrl}${baseName}_freebsd-amd64.tar.gz`, 'freebsd', 'x64')
.src(`${baseUrl}${baseName}_linux-amd64.tar.gz`, 'linux', 'x64')
.src(`${baseUrl}${baseName}_linux-arm.tar.gz`, 'linux', 'arm')
.src(`${baseUrl}${baseName}_linux-arm64.tar.gz`, 'linux', 'arm64')
.src(`${baseUrl}${baseName}_netbsd-amd64.tar.gz`, 'netbsd', 'x64')
.src(`${baseUrl}${baseName}_openbsd-amd64.tar.gz`, 'openbsd', 'x64')
.src(`${baseUrl}${baseName}_solaris-amd64.tar.gz`, 'solaris', 'x64')
.src(`${baseUrl}${baseName}_windows-amd64.zip`, 'win32', 'x64')
.src(`${baseUrl}${baseName}_windows-arm64.zip`, 'win32', 'arm64')
.dest(destDir)
.use(binName);
}
/**
* @param {import('pkg-conf').Config} config
*/
function getOptions(config) {
const hugoVersion = [
process.env.HUGO_BIN_HUGO_VERSION,
process.env.npm_config_hugo_bin_hugo_version,
config.version
].find(Boolean) ?? HUGO_VERSION;
const downloadRepo = [
process.env.HUGO_BIN_DOWNLOAD_REPO,
process.env.npm_config_hugo_bin_download_repo,
config.downloadRepo
].find(Boolean) ?? 'https://github.com';
const isExtended = [
process.env.HUGO_BIN_BUILD_TAGS,
process.env.npm_config_hugo_bin_build_tags,
config.buildTags
].includes('extended');
// Strip any leading `v` from hugoVersion because otherwise we'll end up with duplicate `v`s
const version = hugoVersion[0] === 'v' ? hugoVersion.slice(1) : hugoVersion;
debug('[getOptions] hugoVersion:', version);
debug('[getOptions] downloadRepo:', downloadRepo);
debug('[getOptions] isExtended:', isExtended);
return {
hugoVersion: version,
downloadRepo,
isExtended
};
}
/**
* @param {string} cwd
*/
async function main(cwd) {
const config = await packageConfig('hugo-bin', { cwd });
const { hugoVersion, downloadRepo, isExtended } = getOptions(config);
const baseUrl = `${downloadRepo}/gohugoio/hugo/releases/download/v${hugoVersion}/`;
debug('[main] baseUrl:', baseUrl);
return isExtended ? extendedBin(baseUrl, hugoVersion) : normalBin(baseUrl, hugoVersion);
}
export default main;