-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathinstaller.js
More file actions
231 lines (195 loc) · 6.57 KB
/
installer.js
File metadata and controls
231 lines (195 loc) · 6.57 KB
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
'use strict';
/* eslint-disable no-await-in-loop */
const fs = require('fs');
const os = require('os');
const crypto = require('crypto');
const stream = require('stream');
const path = require('path');
const glob = require('glob');
const fetch = require('node-fetch');
const { ESVU_PATH, ensureDirectory, symlink, platform, rmdir } = require('./common');
const Logger = require('./logger');
const DL_FILE_PREFIX = 'esvu-';
function hash(string) {
const h = crypto.createHash('md5');
h.update(string);
return h.digest('hex');
}
class EngineInstaller {
constructor(version, isLatest) {
this.version = version;
this.isLatest = isLatest;
this.downloadPath = undefined;
this.extractedPath = undefined;
this.installPath = path.join(ESVU_PATH, 'engines', this.constructor.config.id);
if (!isLatest) {
this.installPath += `-${version}`;
}
this.binEntries = [];
}
static async install(requestedVersion, status) {
const logger = new Logger(this.config.name);
if (requestedVersion === 'latest' && !status.selectedEngines.includes(this.config.id)) {
status.selectedEngines.push(this.config.id);
}
logger.info('Checking version...');
const version = await this.resolveVersion(requestedVersion);
const INSTALLED_ID = requestedVersion === 'latest' ? this.config.id : `${this.config.id}-${version}`;
if (status.installed[INSTALLED_ID]
&& version === status.installed[INSTALLED_ID].version) {
logger.succeed('Up to date');
return;
}
const installer = new this(version, requestedVersion === 'latest');
if (this.config.externalRequirements) {
logger.warn('There are external requirements that may need to be installed:');
this.config.externalRequirements.forEach((e) => {
logger.warn(` * ${e.name} - ${e.url}`);
});
}
logger.info(`Installing version ${version}`);
const url = await installer.getDownloadURL(version);
logger.info(`Downloading ${url}`);
installer.downloadPath = await fetch(url)
.then(async (r) => {
if (!r.ok) {
throw new Error(`Got ${r.status}`);
}
const rURL = new URL(r.url);
const l = path.join(os.tmpdir(), DL_FILE_PREFIX + hash(url) + path.extname(rURL.pathname));
const sink = fs.createWriteStream(l);
const progress = logger.progress(+r.headers.get('content-length'));
await new Promise((resolve, reject) => {
r.body
.pipe(new (class extends stream.Transform {
constructor(...args) {
super(...args);
this.count = 0;
}
_transform(chunk, encoding, cb) {
this.count += chunk.length;
progress.update(this.count);
this.push(chunk);
cb(null);
}
_flush(cb) {
cb(null);
}
})())
.pipe(sink)
.once('error', reject)
.once('finish', resolve);
});
progress.stop();
return l;
});
installer.extractedPath = `${installer.downloadPath}-extracted`;
logger.info(`Extracting ${installer.downloadPath}`);
await ensureDirectory(installer.installPath);
await ensureDirectory(path.join(ESVU_PATH, 'bin'));
await installer.extract();
logger.info(`Installing ${installer.extractedPath}`);
await installer.install();
logger.info('Testing...');
await installer.test();
await installer.cleanup();
status.installed[INSTALLED_ID] = {
version,
binEntries: installer.binEntries,
};
logger.succeed(`Installed with bin entries: ${installer.binEntries.join(', ')}`);
}
static async uninstall(version, status) {
const logger = new Logger(this.config.name);
logger.info('Uninstalling...');
if (version !== 'latest') {
version = await this.resolveVersion(version);
}
const INSTALLED_ID = version === 'latest' ? this.config.id : `${this.config.id}-${version}`;
// Delete bin entries and engine assets
await Promise.all([
status.installed[INSTALLED_ID]
&& status.installed[INSTALLED_ID].binEntries
&& status.installed[INSTALLED_ID].binEntries.map((b) =>
fs.promises.unlink(path.join(ESVU_PATH, 'bin', b))),
rmdir(path.join(ESVU_PATH, 'engines', INSTALLED_ID)),
]);
// Remove from status.selectedEngines
if (version === 'latest') {
status.selectedEngines.splice(status.selectedEngines.indexOf(this.config.id), 1);
}
delete status.installed[INSTALLED_ID];
logger.succeed('Removed assets and bin entries');
}
static isSupported() {
if (this.config.supported) {
return this.config.supported.includes(platform);
}
return true;
}
static shouldInstallByDefault() {
return this.isSupported();
}
cleanup() {
return Promise.all([
fs.promises.unlink(this.downloadPath),
rmdir(this.extractedPath),
]);
}
async registerAssets(pattern) {
const full = path.join(this.extractedPath, pattern);
const files = await new Promise((resolve, reject) => {
glob(full, { nodir: true }, (err, list) => {
if (err) {
reject(err);
} else {
resolve(list);
}
});
});
if (files.length === 0) {
throw new Error(`No files matched ${pattern}`);
}
await Promise.all(files.map((file) =>
this.registerAsset(path.relative(this.extractedPath, file))));
}
async registerAsset(name) {
const full = path.join(this.extractedPath, name);
const out = path.join(this.installPath, name);
await ensureDirectory(path.dirname(out));
await fs.promises.copyFile(full, out);
return out;
}
async registerBinary(name, alias = name) {
const full = await this.registerAsset(name);
return this.registerBinarySymlink(full, path.basename(alias));
}
async registerBinarySymlink(from, name) {
if (!this.isLatest) {
name += `-${this.version}`;
}
const bin = path.join(ESVU_PATH, 'bin', name);
await symlink(from, bin);
this.binEntries.push(name);
return bin;
}
async registerScript(name, body) {
if (!this.isLatest) {
name += `-${this.version}`;
}
let source;
if (platform.startsWith('win')) {
name += '.cmd';
source = `@echo off\r\n${body} %*\r\n`;
} else {
source = `#!/usr/bin/env bash\nexec ${body} "$@"\n`;
}
const full = path.join(ESVU_PATH, 'bin', name);
await fs.promises.writeFile(full, source, {
mode: 0o755,
});
this.binEntries.push(name);
return full;
}
}
module.exports = EngineInstaller;