Skip to content

Commit 5006406

Browse files
committed
fix: get Electron dependency path
1 parent 620a6ae commit 5006406

File tree

5 files changed

+17
-46
lines changed

5 files changed

+17
-46
lines changed

packages/api/core/src/api/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export default autoTrace(
154154
}
155155

156156
if (!electronExecPath) {
157-
electronExecPath = await locateElectronExecutable(dir, packageJSON);
157+
electronExecPath = locateElectronExecutable(dir, packageJSON);
158158
}
159159

160160
d('Electron binary path:', electronExecPath);

packages/api/core/src/util/electron-executable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import logSymbols from 'log-symbols';
55

66
type PackageJSON = Record<string, unknown>;
77

8-
export default async function locateElectronExecutable(dir: string, packageJSON: PackageJSON): Promise<string> {
9-
const electronModulePath: string | undefined = await getElectronModulePath(dir, packageJSON);
8+
export default function locateElectronExecutable(dir: string, packageJSON: PackageJSON): string {
9+
const electronModulePath: string | undefined = getElectronModulePath(dir, packageJSON);
1010

1111
// eslint-disable-next-line @typescript-eslint/no-var-requires
1212
let electronExecPath = require(electronModulePath || path.resolve(dir, 'node_modules/electron'));

packages/api/core/test/fast/make_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('make', () => {
3333
const electronPath = path.resolve(__dirname, 'node_modules/electron');
3434
stubbedMake = proxyquire.noCallThru().load('../../src/api/make', {
3535
'@electron-forge/core-utils': {
36-
getElectronModulePath: () => Promise.resolve(electronPath),
36+
getElectronModulePath: () => electronPath,
3737
getElectronVersion: () => Promise.resolve('1.0.0'),
3838
},
3939
}).default;

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

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import path from 'path';
2-
32
import debug from 'debug';
4-
import findUp from 'find-up';
53
import fs from 'fs-extra';
64
import semver from 'semver';
75

@@ -20,28 +18,6 @@ function findElectronDep(dep: string): boolean {
2018
return electronPackageNames.includes(dep);
2119
}
2220

23-
async function findAncestorNodeModulesPath(dir: string, packageName: string): Promise<string | undefined> {
24-
d('Looking for a lock file to indicate the root of the repo');
25-
const lockPath = await findUp(['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'], { cwd: dir, type: 'file' });
26-
if (lockPath) {
27-
d(`Found lock file: ${lockPath}`);
28-
const nodeModulesPath = path.join(path.dirname(lockPath), 'node_modules', packageName);
29-
if (await fs.pathExists(nodeModulesPath)) {
30-
return nodeModulesPath;
31-
}
32-
}
33-
34-
return Promise.resolve(undefined);
35-
}
36-
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-
4521
export class PackageNotFoundError extends Error {
4622
constructor(packageName: string, dir: string) {
4723
super(`Cannot find the package "${packageName}". Perhaps you need to run "${safeYarnOrNpm()} install" in "${dir}"?`);
@@ -63,23 +39,18 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
6339
return packageName;
6440
}
6541

66-
async function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string | undefined> {
67-
const nodeModulesPath = await determineNodeModulesPath(dir, packageName);
68-
if (!nodeModulesPath) {
42+
function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string> {
43+
try {
44+
// eslint-disable-next-line node/no-missing-require
45+
return require.resolve(`${packageName}/package.json`, { paths: [dir] });
46+
} catch {
6947
throw new PackageNotFoundError(packageName, dir);
7048
}
71-
72-
const electronPackageJSONPath = path.join(nodeModulesPath, 'package.json');
73-
if (await fs.pathExists(electronPackageJSONPath)) {
74-
return electronPackageJSONPath;
75-
}
76-
77-
return undefined;
7849
}
7950

80-
export async function getElectronModulePath(dir: string, packageJSON: PackageJSONWithDeps): Promise<string | undefined> {
51+
export function getElectronModulePath(dir: string, packageJSON: PackageJSONWithDeps): string | undefined {
8152
const moduleName = getElectronModuleName(packageJSON);
82-
const packageJSONPath = await getElectronPackageJSONPath(dir, moduleName);
53+
const packageJSONPath = getElectronPackageJSONPath(dir, moduleName);
8354
if (packageJSONPath) {
8455
return path.dirname(packageJSONPath);
8556
}
@@ -95,7 +66,7 @@ export async function getElectronVersion(dir: string, packageJSON: PackageJSONWi
9566
let version = packageJSON.devDependencies![packageName];
9667
if (!semver.valid(version)) {
9768
// It's not an exact version, find it in the actual module
98-
const electronPackageJSONPath = await getElectronPackageJSONPath(dir, packageName);
69+
const electronPackageJSONPath = getElectronPackageJSONPath(dir, packageName);
9970
if (electronPackageJSONPath) {
10071
const electronPackageJSON = await fs.readJson(electronPackageJSONPath);
10172
version = electronPackageJSON.version;

packages/utils/core-utils/test/electron-version_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('getElectronModulePath', () => {
130130
devDependencies: { electron: '^4.0.4' },
131131
};
132132

133-
expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
133+
expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
134134
});
135135

136136
after(() => {
@@ -150,7 +150,7 @@ describe('getElectronModulePath', () => {
150150
devDependencies: { electron: '^4.0.4' },
151151
};
152152

153-
expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
153+
expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
154154
});
155155

156156
it('finds the top-level electron module despite the additional node_modules folder inside the package', async () => {
@@ -160,7 +160,7 @@ describe('getElectronModulePath', () => {
160160
devDependencies: { electron: '^4.0.4' },
161161
};
162162

163-
expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
163+
expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
164164
});
165165

166166
it('finds the correct electron module in nohoist mode', async () => {
@@ -170,8 +170,8 @@ describe('getElectronModulePath', () => {
170170
devDependencies: { electron: '^13.0.0' },
171171
};
172172

173-
expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(fixtureDir, 'node_modules', 'electron'));
174-
expect(await getElectronModulePath(fixtureDir, packageJSON)).not.to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
173+
expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(fixtureDir, 'node_modules', 'electron'));
174+
expect(getElectronModulePath(fixtureDir, packageJSON)).not.to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
175175
});
176176

177177
after(() => {

0 commit comments

Comments
 (0)