Repository navigation
Expand file tree
/
Copy pathpreinstall.js
More file actions
93 lines (80 loc) · 2.85 KB
/
Copy pathpreinstall.js
File metadata and controls
93 lines (80 loc) · 2.85 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
#!/usr/bin/env node
import { existsSync, readdirSync, rmSync } from 'fs';
import { spawnSync } from 'child_process';
import installPath from './path.js';
import path from 'path';
const isWin = process.platform === 'win32';
// Check if running as root on Linux/macOS and abort if so.
// See: https://github.com/electron/build-tools/issues/789.
// This check will not run in CI since CI sometimes needs
// to run as root
if (!process.env.CI && !isWin && process.getuid?.() === 0) {
console.error(
'Do not install @electron/build-tools with sudo, as this causes issues for standard users.\n\n' +
'Please install Node.js via a version manager (e.g., nvm, fnm, or Homebrew) to\n' +
'avoid needing sudo for global npm installs.\n',
);
process.exit(1);
}
function throwForBadSpawn(basicInfo, spawnSyncResult) {
if (spawnSyncResult.status !== 0) {
throw new Error(`Command "${basicInfo}" failed with exit code ${spawnSyncResult.status}`);
}
}
function install() {
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.
const yarnReleasesDir = path.resolve(installPath, '.yarn', 'releases');
const yarnToUse = readdirSync(yarnReleasesDir).find((f) => /^yarn-.+js/.test(f));
const yarnPath = path.resolve(yarnReleasesDir, yarnToUse);
throwForBadSpawn(
'yarn install',
spawnSync(process.execPath, [yarnPath, 'install'], {
stdio: 'inherit',
cwd: installPath,
}),
);
} 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();