-
-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathcheck-system.ts
More file actions
201 lines (186 loc) · 5.68 KB
/
check-system.ts
File metadata and controls
201 lines (186 loc) · 5.68 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
import { exec } from 'node:child_process';
import os from 'node:os';
import path from 'node:path';
import {
PACKAGE_MANAGERS,
resolvePackageManager,
spawnPackageManager,
SupportedPackageManager,
} from '@electron-forge/core-utils';
import { ForgeListrTask } from '@electron-forge/shared-types';
import debug from 'debug';
import fs from 'fs-extra';
import semver from 'semver';
const d = debug('electron-forge:check-system');
async function getGitVersion(): Promise<string | null> {
return new Promise<string | null>((resolve) => {
exec('git --version', (err, output) =>
err
? resolve(null)
: resolve(output.toString().trim().split(' ').reverse()[0]),
);
});
}
/**
* Packaging an app with Electron Forge requires `node_modules` to be on disk.
* With `pnpm`, this can be done in a few different ways.
*
* `node-linker=hoisted` replicates the behaviour of npm and Yarn Classic, while
* users may choose to set `public-hoist-pattern` or `hoist-pattern` for advanced
* configuration purposes.
*/
async function checkPnpmConfig() {
const { pnpm } = PACKAGE_MANAGERS;
const hoistPattern = await spawnPackageManager(pnpm, [
'config',
'get',
'hoist-pattern',
]);
const publicHoistPattern = await spawnPackageManager(pnpm, [
'config',
'get',
'public-hoist-pattern',
]);
if (hoistPattern !== 'undefined' || publicHoistPattern !== 'undefined') {
d(
`Custom hoist pattern detected ${JSON.stringify({
hoistPattern,
publicHoistPattern,
})}, assuming that the user has configured pnpm to package dependencies.`,
);
return;
}
const nodeLinker = await spawnPackageManager(pnpm, [
'config',
'get',
'node-linker',
]);
if (nodeLinker !== 'hoisted') {
throw new Error(
'When using pnpm, `node-linker` must be set to "hoisted" (or a custom `hoist-pattern` or `public-hoist-pattern` must be defined). Run `pnpm config set node-linker hoisted` to set this config value, or add it to your project\'s `.npmrc` file.',
);
}
}
async function checkYarnConfig() {
const { yarn } = PACKAGE_MANAGERS;
const yarnVersion = await spawnPackageManager(yarn, ['--version']);
const nodeLinker = await spawnPackageManager(yarn, [
'config',
'get',
'nodeLinker',
]);
if (
yarnVersion &&
semver.gte(yarnVersion, '2.0.0') &&
nodeLinker !== 'node-modules'
) {
throw new Error(
'When using Yarn 2+, `nodeLinker` must be set to "node-modules". Run `yarn config set nodeLinker node-modules` to set this config value, or add it to your project\'s `.yarnrc.yml` file.',
);
}
}
// TODO(erickzhao): Drop antiquated versions of npm for Forge v8
const ALLOWLISTED_VERSIONS: Record<
SupportedPackageManager,
Record<string, string>
> = {
npm: {
all: '^3.0.0 || ^4.0.0 || ~5.1.0 || ~5.2.0 || >= 5.4.2',
darwin: '>= 5.4.0',
linux: '>= 5.4.0',
},
yarn: {
all: '>= 1.0.0',
},
pnpm: {
all: '>= 8.0.0',
},
bun: {
all: '>= 1.2.0',
},
};
export async function checkPackageManager() {
const pm = await resolvePackageManager();
const version = pm.version ?? (await spawnPackageManager(pm, ['--version']));
const versionString = version.toString().trim();
const range =
ALLOWLISTED_VERSIONS[pm.executable][process.platform] ??
ALLOWLISTED_VERSIONS[pm.executable].all;
if (!semver.valid(version)) {
d(`Invalid semver-string while checking version: ${version}`);
throw new Error(
`Could not check ${pm.executable} version "${version}", assuming incompatible`,
);
}
if (!semver.satisfies(version, range)) {
throw new Error(
`Incompatible version of ${pm.executable} detected: "${version}" must be in range ${range}`,
);
}
if (pm.executable === 'pnpm') {
await checkPnpmConfig();
} else if (pm.executable === 'yarn') {
await checkYarnConfig();
}
return `${pm.executable}@${versionString}`;
}
/**
* Some people know their system is OK and don't appreciate the 800ms lag in
* start up that these checks (in particular the package manager check) costs.
*
* Simply creating this flag file in your home directory will skip these checks
* and shave ~800ms off your forge start time.
*
* This is specifically not documented or everyone would make it.
*/
const SKIP_SYSTEM_CHECK = path.resolve(
os.homedir(),
'.skip-forge-system-check',
);
export type SystemCheckContext = {
command: string;
git: boolean;
node: boolean;
packageManager: boolean;
};
export async function checkSystem(
callerTask: ForgeListrTask<SystemCheckContext>,
) {
if (!(await fs.pathExists(SKIP_SYSTEM_CHECK))) {
d('checking system, create ~/.skip-forge-system-check to stop doing this');
return callerTask.newListr<SystemCheckContext>(
[
{
title: 'Checking git exists',
// We only call the `initGit` helper in the `init` and `import` commands
enabled: (ctx): boolean =>
(ctx.command === 'init' || ctx.command === 'import') && ctx.git,
task: async (_, task) => {
const gitVersion = await getGitVersion();
if (gitVersion) {
task.title = `Found git@${gitVersion}`;
} else {
throw new Error('Could not find git in environment');
}
},
},
{
title: 'Checking package manager version',
task: async (_, task) => {
const packageManager = await checkPackageManager();
task.title = `Found ${packageManager}`;
},
},
],
{
concurrent: true,
exitOnError: true,
rendererOptions: {
collapseSubtasks: true,
},
},
);
}
d('skipping system check');
return true;
}