-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
install.ts
235 lines (210 loc) · 8 KB
/
install.ts
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
234
235
import fs = require('fs');
import os = require('os');
import path = require('path');
import zlib = require('zlib');
import https = require('https');
import child_process = require('child_process');
const version = require('./package.json').version;
const binPath = path.join(__dirname, 'bin', 'esbuild');
const stampPath = path.join(__dirname, 'stamp.txt');
async function installBinaryFromPackage(name: string, fromPath: string, toPath: string): Promise<void> {
// It turns out that some package managers (e.g. yarn) sometimes re-run the
// postinstall script for this package after we have already been installed.
// That means this script must be idempotent. Let's skip the install if it's
// already happened.
if (fs.existsSync(stampPath)) {
return;
}
// Try to install from the cache if possible
const cachePath = getCachePath(name);
try {
// Copy from the cache
fs.copyFileSync(cachePath, toPath);
fs.chmodSync(toPath, 0o755);
// Mark the cache entry as used for LRU
const now = new Date;
fs.utimesSync(cachePath, now, now);
// Mark the operation as successful so this script is idempotent
fs.writeFileSync(stampPath, '');
return;
} catch {
}
// Next, try to install using npm. This should handle various tricky cases
// such as environments where requests to npmjs.org will hang (in which case
// there is probably a proxy and/or a custom registry configured instead).
let buffer: Buffer | undefined;
let didFail = false;
try {
buffer = installUsingNPM(name, fromPath);
} catch (err) {
didFail = true;
console.error(`Trying to install "${name}" using npm`);
console.error(`Failed to install "${name}" using npm: ${err && err.message || err}`);
}
// If that fails, the user could have npm configured incorrectly or could not
// have npm installed. Try downloading directly from npm as a last resort.
if (!buffer) {
const url = `https://registry.npmjs.org/${name}/-/${name}-${version}.tgz`;
console.error(`Trying to download ${JSON.stringify(url)}`);
try {
buffer = extractFileFromTarGzip(await fetch(url), fromPath);
} catch (err) {
console.error(`Failed to download ${JSON.stringify(url)}: ${err && err.message || err}`);
}
}
// Give up if none of that worked
if (!buffer) {
console.error(`Install unsuccessful`);
process.exit(1);
}
// Write out the binary executable that was extracted from the package
fs.writeFileSync(toPath, buffer, { mode: 0o755 });
// Mark the operation as successful so this script is idempotent
fs.writeFileSync(stampPath, '');
// Also try to cache the file to speed up future installs
try {
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
fs.copyFileSync(toPath, cachePath);
cleanCacheLRU(cachePath);
} catch {
}
if (didFail) console.error(`Install successful`);
}
function getCachePath(name: string): string {
const home = os.homedir();
const common = ['esbuild', 'bin', `${name}@${version}`];
if (process.platform === 'darwin') return path.join(home, 'Library', 'Caches', ...common);
if (process.platform === 'win32') return path.join(home, 'AppData', 'Local', 'Cache', ...common);
return path.join(home, '.cache', ...common);
}
function cleanCacheLRU(fileToKeep: string): void {
// Gather all entries in the cache
const dir = path.dirname(fileToKeep);
const entries: { path: string, mtime: Date }[] = [];
for (const entry of fs.readdirSync(dir)) {
const entryPath = path.join(dir, entry);
try {
const stats = fs.statSync(entryPath);
entries.push({ path: entryPath, mtime: stats.mtime });
} catch {
}
}
// Only keep the most recent entries
entries.sort((a, b) => +b.mtime - +a.mtime);
for (const entry of entries.slice(5)) {
try {
fs.unlinkSync(entry.path);
} catch {
}
}
}
function fetch(url: string): Promise<Buffer> {
return new Promise((resolve, reject) => {
https.get(url, res => {
if ((res.statusCode === 301 || res.statusCode === 302) && res.headers.location)
return fetch(res.headers.location).then(resolve, reject);
if (res.statusCode !== 200)
return reject(new Error(`Server responded with ${res.statusCode}`));
let chunks: Buffer[] = [];
res.on('data', chunk => chunks.push(chunk));
res.on('end', () => resolve(Buffer.concat(chunks)));
}).on('error', reject);
});
}
function extractFileFromTarGzip(buffer: Buffer, file: string): Buffer {
try {
buffer = zlib.unzipSync(buffer);
} catch (err) {
throw new Error(`Invalid gzip data in archive: ${err && err.message || err}`);
}
let str = (i: number, n: number) => String.fromCharCode(...buffer.subarray(i, i + n)).replace(/\0.*$/, '');
let offset = 0;
file = `package/${file}`;
while (offset < buffer.length) {
let name = str(offset, 100);
let size = parseInt(str(offset + 124, 12), 8);
offset += 512;
if (!isNaN(size)) {
if (name === file) return buffer.subarray(offset, offset + size);
offset += (size + 511) & ~511;
}
}
throw new Error(`Could not find ${JSON.stringify(file)} in archive`);
}
function installUsingNPM(name: string, file: string): Buffer {
const installDir = path.join(__dirname, '.install');
fs.mkdirSync(installDir);
fs.writeFileSync(path.join(installDir, 'package.json'), '{}');
// Erase "npm_config_global" so that "npm install --global esbuild" works.
// Otherwise this nested "npm install" will also be global, and the install
// will deadlock waiting for the global installation lock.
const env = { ...process.env, npm_config_global: undefined };
child_process.execSync(`npm install --loglevel=error --prefer-offline --no-audit --progress=false ${name}@${version}`,
{ cwd: installDir, stdio: 'pipe', env });
const buffer = fs.readFileSync(path.join(installDir, 'node_modules', name, file));
removeRecursive(installDir);
return buffer;
}
function removeRecursive(dir: string): void {
for (const entry of fs.readdirSync(dir)) {
const entryPath = path.join(dir, entry);
let stats;
try {
stats = fs.lstatSync(entryPath);
} catch (e) {
continue; // Guard against https://github.com/nodejs/node/issues/4760
}
if (stats.isDirectory()) removeRecursive(entryPath);
else fs.unlinkSync(entryPath);
}
fs.rmdirSync(dir);
}
function installOnUnix(name: string): void {
if (process.env.ESBUILD_BIN_PATH_FOR_TESTS) {
fs.unlinkSync(binPath);
fs.symlinkSync(process.env.ESBUILD_BIN_PATH_FOR_TESTS, binPath);
} else {
installBinaryFromPackage(name, 'bin/esbuild', binPath)
.catch(e => setImmediate(() => { throw e; }));
}
}
function installOnWindows(name: string): void {
fs.writeFileSync(
binPath,
`#!/usr/bin/env node
const path = require('path');
const esbuild_exe = path.join(__dirname, '..', 'esbuild.exe');
const child_process = require('child_process');
child_process.spawnSync(esbuild_exe, process.argv.slice(2), { stdio: 'inherit' });
`);
const exePath = path.join(__dirname, 'esbuild.exe');
if (process.env.ESBUILD_BIN_PATH_FOR_TESTS) {
fs.copyFileSync(process.env.ESBUILD_BIN_PATH_FOR_TESTS, exePath);
} else {
installBinaryFromPackage(name, 'esbuild.exe', exePath)
.catch(e => setImmediate(() => { throw e; }));
}
}
const key = `${process.platform} ${os.arch()} ${os.endianness()}`;
const knownWindowsPackages: Record<string, string> = {
'win32 ia32 LE': 'esbuild-windows-32',
'win32 x64 LE': 'esbuild-windows-64',
};
const knownUnixlikePackages: Record<string, string> = {
'darwin x64 LE': 'esbuild-darwin-64',
'freebsd arm64 LE': 'esbuild-freebsd-arm64',
'freebsd x64 LE': 'esbuild-freebsd-64',
'linux arm64 LE': 'esbuild-linux-arm64',
'linux ia32 LE': 'esbuild-linux-32',
'linux ppc64 LE': 'esbuild-linux-ppc64le',
'linux x64 LE': 'esbuild-linux-64',
};
// Pick a package to install
if (key in knownWindowsPackages) {
installOnWindows(knownWindowsPackages[key]);
} else if (key in knownUnixlikePackages) {
installOnUnix(knownUnixlikePackages[key]);
} else {
console.error(`Unsupported platform: ${key}`);
process.exit(1);
}