forked from nikku/hugo-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
216 lines (153 loc) · 5.33 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
'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 BERTY_BASE_URL = 'https://github.com/berty/berty/releases/download';
var BERTY_MIN_VERSION = '2.364.5';
var BERTY_DEFAULT_VERSION = process.env.BERTY_VERSION || '2.364.5';
var TARGET = {
platform: process.platform,
arch: process.arch
};
var PLATFORM_LOOKUP = {
darwin: 'darwin',
linux: 'linux',
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 = 'berty' + 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 berty version.
*
* @param {String} version
* @return {Object}
*/
function getDetails(version, target) {
var arch_dl = '_i386',
platform = target.platform,
archiveExtension = '.tar.gz',
executableExtension = '';
if (/x64/.test(target.arch)) {
arch_dl = '_amd64';
} else if (/arm/.test(target.arch)) {
arch_dl = '_arm64';
}
if (/win32/.test(platform)) {
platform = 'windows';
executableExtension = '.exe';
archiveExtension = '.zip';
}
var baseName = 'berty_${version}'.replace(/\$\{version\}/g, version);
var executableName = 'berty${executableExtension}'.replace(/\$\{executableExtension\}/g, executableExtension);
var archiveName =
'berty_${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${version}/${archiveName}'
.replace(/\$\{baseUrl\}/g, BERTY_BASE_URL)
.replace(/\$\{version\}/g, version)
.replace(/\$\{archiveName\}/g, archiveName);
return {
archiveName: archiveName,
executableName: executableName,
downloadLink: downloadLink,
executableExtension: executableExtension
};
}
/**
* Ensure the given version of berty is installed before
* passing (err, executablePath) to the callback.
*
* @param {Object} options
* @param {Function} callback
*/
function withBerty(options, callback) {
if (typeof options === 'function') {
callback = options;
options = '';
}
var version = options.version || BERTY_DEFAULT_VERSION;
var verbose = options.verbose;
verbose && logDebug('target=%o, berty=%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, BERTY_MIN_VERSION)) {
logError('berty-cli@%s is compatible with berty >= %s only.', cliVersion, BERTY_MIN_VERSION);
logError('you requested berty@%s', version);
return callback(new Error(`incompatible with berty@${version}`));
}
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('berty found\n');
return callback(null, executablePath);
}
log('berty 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 berty: ' + err);
return callback(err);
}
log('fetched berty %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('berty 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.withBerty = withBerty;