-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
111 lines (97 loc) · 2.95 KB
/
postinstall.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
const { promisify } = require('util');
const { spawn } = require('child_process');
const path = require('path');
const http = require('http');
const tar = require('tar');
const { mkdtemp, createWriteStream, lstat } = require('fs');
const { tmpdir, platform } = require('os');
const rimraf = require('rimraf');
const packageJson = require('./package.json');
const $mkdtemp = promisify(mkdtemp);
const $rimraf = promisify(rimraf);
const $lstat = promisify(lstat);
function $spawn(...args) {
return new Promise((resolve, reject) => {
const proc = spawn(...args);
proc.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`child process exited with code ${code}`));
}
});
});
}
async function main() {
const tmp = await $mkdtemp(path.join(tmpdir(), 'c-wasm-postinstall-'));
const emsdkTar = path.join(tmp, 'emsdk-portable.tar.gz');
const cwd = path.join(__dirname, 'emsdk-portable');
try {
const stats = await $lstat(path.join(cwd, 'emscripten', packageJson.emsdk, 'em++'));
if (stats.isFile()) {
process.stdout.write('emsdk found\n');
process.exit(0);
}
} catch (error) {
process.stdout.write(`${error}\n`);
process.stdout.write('emsdk not found, installing\n');
}
try {
const file = createWriteStream(emsdkTar);
await new Promise((resolve, reject) => {
http.get('http://s3.amazonaws.com/mozilla-games/emscripten/releases/emsdk-portable.tar.gz', (response) => {
if (response.statusCode !== 200) {
reject(new Error(response.statusMessage));
}
response.pipe(file);
file.on('finish', () => {
file.close(() => {
resolve();
});
});
}).on('error', (error) => {
reject(error);
});
});
} catch (error) {
process.stdout.write('Unable to download emsdk\n');
process.stdout.write(`${error}\n`);
process.exit(1);
}
try {
await tar.x({
file: emsdkTar,
});
} catch (error) {
process.stdout.write('Unable to unpack emsdk\n');
process.stdout.write(`${error}\n`);
process.exit(1);
}
const emsdkCommand = (platform() === 'win32') ? 'emsdk.bat' : './emsdk';
try {
await $spawn(emsdkCommand, ['update'], { cwd, stdio: 'inherit' });
} catch (error) {
process.stdout.write('Unable to update emsdk\n');
process.stdout.write(`${error}\n`);
process.exit(1);
}
try {
await $spawn(emsdkCommand, ['install', `sdk-${packageJson.emsdk}-64bit`], { cwd, stdio: 'inherit' });
} catch (error) {
process.stdout.write('Unable to install emsdk\n');
process.stdout.write(`${error}\n`);
process.exit(1);
}
try {
await $spawn(emsdkCommand, ['activate', `sdk-${packageJson.emsdk}-64bit`], {
cwd,
stdio: 'inherit',
});
} catch (error) {
process.stdout.write('Unable to activate emsdk\n');
process.stdout.write(`${error}\n`);
process.exit(1);
}
await $rimraf(tmp);
}
main();