Skip to content

Commit 990d7c9

Browse files
committed
cache it instead
1 parent 7cf0639 commit 990d7c9

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/utils/core-utils/src/package-manager.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import logSymbols from 'log-symbols';
66

77
const d = debug('electron-forge:package-manager');
88

9-
let hasEmittedWarning = false;
10-
119
export type SupportedPackageManager = 'yarn' | 'npm' | 'pnpm';
1210
export type PMDetails = { executable: SupportedPackageManager; version?: string; install: string; dev: string; exact: string };
1311

12+
let cachedPM: PMDetails | undefined = undefined;
13+
1414
const MANAGERS: Record<SupportedPackageManager, PMDetails> = {
1515
yarn: {
1616
executable: 'yarn',
@@ -71,15 +71,18 @@ function pmFromUserAgent() {
7171
*
7272
*/
7373
export const resolvePackageManager: () => Promise<PMDetails> = async () => {
74+
if (cachedPM) {
75+
return cachedPM;
76+
}
77+
7478
const executingPM = pmFromUserAgent();
7579
const lockfile = await findUp(['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'pnpm-workspace.yaml'], { type: 'file' });
7680
const lockfilePM = (lockfile && PM_FROM_LOCKFILE[lockfile]) ?? undefined;
7781
const installer = process.env.NODE_INSTALLER || executingPM?.name || lockfilePM;
7882

7983
// TODO(erickzhao): Remove NODE_INSTALLER environment variable for Forge 8
80-
if (typeof process.env.NODE_INSTALLER === 'string' && !hasEmittedWarning) {
84+
if (typeof process.env.NODE_INSTALLER === 'string') {
8185
console.warn(logSymbols.warning, chalk.yellow(`The NODE_INSTALLER environment variable is deprecated and will be removed in Electron Forge v8`));
82-
hasEmittedWarning = true;
8386
}
8487

8588
switch (installer) {
@@ -89,7 +92,8 @@ export const resolvePackageManager: () => Promise<PMDetails> = async () => {
8992
d(
9093
`Resolved package manager to ${installer}. (Derived from NODE_INSTALLER: ${process.env.NODE_INSTALLER}, npm_config_user_agent: ${executingPM}, lockfile: ${lockfilePM}.)`
9194
);
92-
return { ...MANAGERS[installer], version: executingPM?.version };
95+
cachedPM = { ...MANAGERS[installer], version: executingPM?.version };
96+
break;
9397
default:
9498
if (installer !== undefined) {
9599
console.warn(
@@ -99,8 +103,9 @@ export const resolvePackageManager: () => Promise<PMDetails> = async () => {
99103
} else {
100104
d(`No package manager detected. Falling back to npm.`);
101105
}
102-
return MANAGERS['npm'];
106+
cachedPM = MANAGERS['npm'];
103107
}
108+
return cachedPM;
104109
};
105110

106111
export const spawnPackageManager = async (args?: CrossSpawnArgs, opts?: CrossSpawnOptions): Promise<string> =>

0 commit comments

Comments
 (0)