-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpreinstall.js
77 lines (67 loc) · 2.19 KB
/
preinstall.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
#!/usr/bin/env node
const { existsSync, rmSync } = require('fs');
const { spawnSync } = require('child_process');
const { homedir } = require('os');
const path = require('path');
const isWin = process.platform === 'win32';
function throwForBadSpawn(basicInfo, spawnSyncResult) {
if (spawnSyncResult.status !== 0) {
throw new Error(`Command "${basicInfo}" failed with exit code ${spawnSyncResult.status}`);
}
}
function install() {
const installPath = path.resolve(homedir(), '.electron_build_tools');
const BUILD_TOOLS_URL = 'https://github.com/electron/build-tools';
try {
// Clone build-tools into user homedir
const alreadyCloned = existsSync(installPath);
if (alreadyCloned) {
throwForBadSpawn(
'git fetch',
spawnSync('git', ['fetch'], { stdio: 'inherit', cwd: installPath }),
);
} else {
throwForBadSpawn(
'git clone',
spawnSync('git', ['clone', '-q', BUILD_TOOLS_URL, installPath], { stdio: 'inherit' }),
);
}
const shouldCheckout = !alreadyCloned || !!process.env.BUILD_TOOLS_SHA;
if (shouldCheckout) {
const checkoutSha = process.env.BUILD_TOOLS_SHA ?? 'main';
throwForBadSpawn(
`git checkout ${checkoutSha}`,
spawnSync('git', ['checkout', checkoutSha, '-f'], { stdio: 'inherit', cwd: installPath }),
);
throwForBadSpawn(
'git reset',
spawnSync(
'git',
['reset', '--hard', `${checkoutSha === 'main' ? 'origin/main' : checkoutSha}`],
{
stdio: 'inherit',
cwd: installPath,
},
),
);
}
// Install build-tools deps.
throwForBadSpawn(
'yarn install',
spawnSync(`npx${isWin ? '.cmd' : ''}`, ['yarn', 'install'], {
stdio: 'inherit',
cwd: installPath,
shell: isWin,
}),
);
} catch (err) {
console.error('Failed to install build-tools: ', err);
// Delete cloned repo to prevent retry failure.
if (existsSync(installPath)) {
rmSync(installPath, { recursive: true, force: true });
}
// Set the exit code to an error so npm will also show the error instead of failing silently.
process.exitCode = 1;
}
}
install();