Skip to content

Commit e92088f

Browse files
committed
fix: get Electron dependency path
1 parent 38f64e4 commit e92088f

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

packages/utils/core-utils/src/electron-version.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { createRequire } from 'node:module';
12
import path from 'path';
2-
33
import debug from 'debug';
44
import findUp from 'find-up';
55
import fs from 'fs-extra';
66
import semver from 'semver';
77

88
import { safeYarnOrNpm } from './yarn-or-npm';
99

10+
const require = createRequire(import.meta.url);
11+
1012
const d = debug('electron-forge:electron-version');
1113

1214
const electronPackageNames = ['electron-nightly', 'electron'];
@@ -34,14 +36,6 @@ async function findAncestorNodeModulesPath(dir: string, packageName: string): Pr
3436
return Promise.resolve(undefined);
3537
}
3638

37-
async function determineNodeModulesPath(dir: string, packageName: string): Promise<string | undefined> {
38-
const nodeModulesPath: string | undefined = path.join(dir, 'node_modules', packageName);
39-
if (await fs.pathExists(nodeModulesPath)) {
40-
return nodeModulesPath;
41-
}
42-
return findAncestorNodeModulesPath(dir, packageName);
43-
}
44-
4539
export class PackageNotFoundError extends Error {
4640
constructor(packageName: string, dir: string) {
4741
super(`Cannot find the package "${packageName}". Perhaps you need to run "${safeYarnOrNpm()} install" in "${dir}"?`);
@@ -63,23 +57,17 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
6357
return packageName;
6458
}
6559

66-
async function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string | undefined> {
67-
const nodeModulesPath = await determineNodeModulesPath(dir, packageName);
68-
if (!nodeModulesPath) {
60+
function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string> {
61+
const electronPath = require.resolve(`${packageName}/package.json`, { paths: [dir] });
62+
if (!electronPath) {
6963
throw new PackageNotFoundError(packageName, dir);
7064
}
71-
72-
const electronPackageJSONPath = path.join(nodeModulesPath, 'package.json');
73-
if (await fs.pathExists(electronPackageJSONPath)) {
74-
return electronPackageJSONPath;
75-
}
76-
77-
return undefined;
65+
return electronPath;
7866
}
7967

8068
export async function getElectronModulePath(dir: string, packageJSON: PackageJSONWithDeps): Promise<string | undefined> {
8169
const moduleName = getElectronModuleName(packageJSON);
82-
const packageJSONPath = await getElectronPackageJSONPath(dir, moduleName);
70+
const packageJSONPath = getElectronPackageJSONPath(dir, moduleName);
8371
if (packageJSONPath) {
8472
return path.dirname(packageJSONPath);
8573
}
@@ -95,7 +83,7 @@ export async function getElectronVersion(dir: string, packageJSON: PackageJSONWi
9583
let version = packageJSON.devDependencies![packageName];
9684
if (!semver.valid(version)) {
9785
// It's not an exact version, find it in the actual module
98-
const electronPackageJSONPath = await getElectronPackageJSONPath(dir, packageName);
86+
const electronPackageJSONPath = getElectronPackageJSONPath(dir, packageName);
9987
if (electronPackageJSONPath) {
10088
const electronPackageJSON = await fs.readJson(electronPackageJSONPath);
10189
version = electronPackageJSON.version;

0 commit comments

Comments
 (0)