forked from nikku/hugo-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (168 loc) · 5.87 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
var path = require('path');
var fs = require('fs');
var got = require('got');
var decompress = require('decompress');
var semver = require('semver');
var cliVersion = require('./package').version;
var chalk = require('chalk');
var HUGO_BASE_URL = 'https://github.com/gohugoio/hugo/releases/download';
var HUGO_MIN_VERSION = '0.20.0';
var HUGO_DEFAULT_VERSION = process.env.HUGO_VERSION || '0.52.0';
var HUGO_MIN_VERSION_NEW_URL_SCHEMA = '0.54.0';
var TARGET = {
platform: process.platform,
arch: process.arch
};
var PLATFORM_LOOKUP = {
darwin: 'macOS',
freebsd: 'FreeBSD',
linux: 'Linux',
openbsd: 'OpenBSD',
win32: 'Windows'
};
function download(url, target, callback) {
var fileStream = fs.createWriteStream(target);
got.stream(url)
.on('error', callback)
.on('end', callback)
.pipe(fileStream);
}
function extract(archivePath, destPath, installDetails) {
var executableName = 'hugo' + installDetails.executableExtension;
return decompress(archivePath, destPath,
{
strip: 1,
map: file => {
if (path.basename(file.path) == executableName) {
file.path = installDetails.executableName;
}
return file;
}
});
}
/**
* Return the installation / download details for the given hugo version.
*
* @param {String} version
* @return {Object}
*/
function getDetails(version, target) {
var arch_exec = '386',
arch_dl = '-32bit',
platform = target.platform,
archiveExtension = '.tar.gz',
executableExtension = '';
if (/x64/.test(target.arch)) {
arch_exec = 'amd64';
arch_dl = '-64bit';
} else if (/arm/.test(target.arch)) {
arch_exec = 'arm';
arch_dl = '_ARM';
}
if (/win32/.test(platform)) {
platform = 'windows';
executableExtension = '.exe';
archiveExtension = '.zip';
}
var baseName = 'hugo_${version}'.replace(/\$\{version\}/g, version);
var baseVersion = version.replace(/^extended_/, '');
var executableName =
'${baseName}_${platform}_${arch}${executableExtension}'
.replace(/\$\{baseName\}/g, baseName)
.replace(/\$\{platform\}/g, platform)
.replace(/\$\{arch\}/g, arch_exec)
.replace(/\$\{executableExtension\}/g, executableExtension);
var archiveName =
'${baseName}_${platform}${arch}${archiveExtension}'
.replace(/\$\{baseName\}/g, baseName)
.replace(/\$\{platform\}/g, PLATFORM_LOOKUP[target.platform])
.replace(/\$\{arch\}/g, arch_dl)
.replace(/\$\{archiveExtension\}/g, archiveExtension);
var downloadLink =
'${baseUrl}/v${baseVersion}/${archiveName}'
.replace(/\$\{baseUrl\}/g, HUGO_BASE_URL)
.replace(/\$\{baseVersion\}/g, baseVersion)
.replace(/\$\{archiveName\}/g, archiveName);
return {
archiveName: archiveName,
executableName: executableName,
downloadLink: downloadLink,
executableExtension: executableExtension
};
}
/**
* Ensure the given version of hugo is installed before
* passing (err, executablePath) to the callback.
*
* @param {Object} options
* @param {Function} callback
*/
function withHugo(options, callback) {
if (typeof options === 'function') {
callback = options;
options = '';
}
var version = options.version || HUGO_DEFAULT_VERSION;
var verbose = options.verbose;
verbose && logDebug('target=%o, hugo=%o', TARGET, { version });
// strip of _extended prefix for semver check to work
var extended = /^extended_|\/extended$/.test(version);
var compatVersion = version.replace(/^extended_|\/extended$/, '');
if (semver.lt(compatVersion, HUGO_MIN_VERSION)) {
logError('hugo-cli@%s is compatible with hugo >= %s only.', cliVersion, HUGO_MIN_VERSION);
logError('you requested hugo@%s', version);
return callback(new Error(`incompatible with hugo@${version}`));
}
if (semver.lt(compatVersion, HUGO_MIN_VERSION_NEW_URL_SCHEMA)) {
compatVersion = (compatVersion.endsWith('.0')) ? compatVersion.slice(0, -2) : compatVersion;
}
var pwd = __dirname;
var installDetails = getDetails((extended ? 'extended_' : '') + compatVersion, TARGET);
var installDirectory = path.join(pwd, 'tmp');
var archivePath = path.join(installDirectory, installDetails.archiveName),
executablePath = path.join(installDirectory, installDetails.executableName);
verbose && logDebug('searching executable at <%s>', executablePath);
if (fs.existsSync(executablePath)) {
verbose && logDebug('hugo found\n');
return callback(null, executablePath);
}
log('hugo not found. Attempting to fetch it...');
var mkdirp = require('mkdirp');
// ensure directory exists
mkdirp.sync(installDirectory);
verbose && logDebug('downloading archive from <%s>', installDetails.downloadLink);
download(installDetails.downloadLink, archivePath, function(err) {
var extractPath = path.dirname(archivePath);
if (err) {
logError('failed to download hugo: ' + err);
return callback(err);
}
log('fetched hugo %s', version);
log('extracting archive...');
extract(archivePath, extractPath, installDetails).then(function() {
verbose && logDebug('extracted archive to <%s>', extractPath);
if (!fs.existsSync(executablePath)) {
logError('executable <%s> not found', executablePath);
logError('please report this as a bug');
throw new Error('executable not found');
}
log('hugo available, let\'s go!\n');
callback(null, executablePath);
}, function(err) {
logError('failed to extract: ' + err);
callback(err);
});
});
}
function logDebug(fmt, ...args) {
console.debug(`${chalk.black.bgWhite('DEBUG')} ${fmt}`, ...args);
}
function log(fmt, ...args) {
console.log(`${chalk.black.bgCyan('INFO')} ${fmt}`, ...args);
}
function logError(fmt, ...args) {
console.error(`${chalk.black.bgRed('ERROR')} ${fmt}`, ...args);
}
module.exports.getDetails = getDetails;
module.exports.withHugo = withHugo;