From 7e345d91558f9332a64656f7b75145fd1428e15b Mon Sep 17 00:00:00 2001 From: rtritto Date: Mon, 10 Jun 2024 02:05:03 +0200 Subject: [PATCH] fix: get Electron dependency path --- packages/api/core/src/api/start.ts | 2 +- .../api/core/src/util/electron-executable.ts | 4 +- packages/api/core/test/fast/make_spec.ts | 2 +- .../utils/core-utils/src/electron-version.ts | 44 ++++--------------- .../core-utils/test/electron-version_spec.ts | 10 ++--- 5 files changed, 17 insertions(+), 45 deletions(-) diff --git a/packages/api/core/src/api/start.ts b/packages/api/core/src/api/start.ts index 3c30718fa9..faf0692154 100644 --- a/packages/api/core/src/api/start.ts +++ b/packages/api/core/src/api/start.ts @@ -154,7 +154,7 @@ export default autoTrace( } if (!electronExecPath) { - electronExecPath = await locateElectronExecutable(dir, packageJSON); + electronExecPath = locateElectronExecutable(dir, packageJSON); } d('Electron binary path:', electronExecPath); diff --git a/packages/api/core/src/util/electron-executable.ts b/packages/api/core/src/util/electron-executable.ts index 3d37e0b2e3..79a3b34cbe 100644 --- a/packages/api/core/src/util/electron-executable.ts +++ b/packages/api/core/src/util/electron-executable.ts @@ -5,8 +5,8 @@ import logSymbols from 'log-symbols'; type PackageJSON = Record; -export default async function locateElectronExecutable(dir: string, packageJSON: PackageJSON): Promise { - const electronModulePath: string | undefined = await getElectronModulePath(dir, packageJSON); +export default function locateElectronExecutable(dir: string, packageJSON: PackageJSON): string { + const electronModulePath: string | undefined = getElectronModulePath(dir, packageJSON); // eslint-disable-next-line @typescript-eslint/no-var-requires let electronExecPath = require(electronModulePath || path.resolve(dir, 'node_modules/electron')); diff --git a/packages/api/core/test/fast/make_spec.ts b/packages/api/core/test/fast/make_spec.ts index 47cca20bf7..64d16a85c1 100644 --- a/packages/api/core/test/fast/make_spec.ts +++ b/packages/api/core/test/fast/make_spec.ts @@ -33,7 +33,7 @@ describe('make', () => { const electronPath = path.resolve(__dirname, 'node_modules/electron'); stubbedMake = proxyquire.noCallThru().load('../../src/api/make', { '@electron-forge/core-utils': { - getElectronModulePath: () => Promise.resolve(electronPath), + getElectronModulePath: () => electronPath, getElectronVersion: () => Promise.resolve('1.0.0'), }, }).default; diff --git a/packages/utils/core-utils/src/electron-version.ts b/packages/utils/core-utils/src/electron-version.ts index 16caf0aac6..b8e4af9d43 100644 --- a/packages/utils/core-utils/src/electron-version.ts +++ b/packages/utils/core-utils/src/electron-version.ts @@ -1,7 +1,6 @@ import path from 'path'; import debug from 'debug'; -import findUp from 'find-up'; import fs from 'fs-extra'; import semver from 'semver'; @@ -20,28 +19,6 @@ function findElectronDep(dep: string): boolean { return electronPackageNames.includes(dep); } -async function findAncestorNodeModulesPath(dir: string, packageName: string): Promise { - d('Looking for a lock file to indicate the root of the repo'); - const lockPath = await findUp(['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'], { cwd: dir, type: 'file' }); - if (lockPath) { - d(`Found lock file: ${lockPath}`); - const nodeModulesPath = path.join(path.dirname(lockPath), 'node_modules', packageName); - if (await fs.pathExists(nodeModulesPath)) { - return nodeModulesPath; - } - } - - return Promise.resolve(undefined); -} - -async function determineNodeModulesPath(dir: string, packageName: string): Promise { - const nodeModulesPath: string | undefined = path.join(dir, 'node_modules', packageName); - if (await fs.pathExists(nodeModulesPath)) { - return nodeModulesPath; - } - return findAncestorNodeModulesPath(dir, packageName); -} - export class PackageNotFoundError extends Error { constructor(packageName: string, dir: string) { super(`Cannot find the package "${packageName}". Perhaps you need to run "${safeYarnOrNpm()} install" in "${dir}"?`); @@ -63,23 +40,18 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string { return packageName; } -async function getElectronPackageJSONPath(dir: string, packageName: string): Promise { - const nodeModulesPath = await determineNodeModulesPath(dir, packageName); - if (!nodeModulesPath) { +function getElectronPackageJSONPath(dir: string, packageName: string): Promise { + try { + // eslint-disable-next-line node/no-missing-require + return require.resolve(`${packageName}/package.json`, { paths: [dir] }); + } catch { throw new PackageNotFoundError(packageName, dir); } - - const electronPackageJSONPath = path.join(nodeModulesPath, 'package.json'); - if (await fs.pathExists(electronPackageJSONPath)) { - return electronPackageJSONPath; - } - - return undefined; } -export async function getElectronModulePath(dir: string, packageJSON: PackageJSONWithDeps): Promise { +export function getElectronModulePath(dir: string, packageJSON: PackageJSONWithDeps): string | undefined { const moduleName = getElectronModuleName(packageJSON); - const packageJSONPath = await getElectronPackageJSONPath(dir, moduleName); + const packageJSONPath = getElectronPackageJSONPath(dir, moduleName); if (packageJSONPath) { return path.dirname(packageJSONPath); } @@ -95,7 +67,7 @@ export async function getElectronVersion(dir: string, packageJSON: PackageJSONWi let version = packageJSON.devDependencies![packageName]; if (!semver.valid(version)) { // It's not an exact version, find it in the actual module - const electronPackageJSONPath = await getElectronPackageJSONPath(dir, packageName); + const electronPackageJSONPath = getElectronPackageJSONPath(dir, packageName); if (electronPackageJSONPath) { const electronPackageJSON = await fs.readJson(electronPackageJSONPath); version = electronPackageJSON.version; diff --git a/packages/utils/core-utils/test/electron-version_spec.ts b/packages/utils/core-utils/test/electron-version_spec.ts index 931715d580..dc3dd198d7 100644 --- a/packages/utils/core-utils/test/electron-version_spec.ts +++ b/packages/utils/core-utils/test/electron-version_spec.ts @@ -130,7 +130,7 @@ describe('getElectronModulePath', () => { devDependencies: { electron: '^4.0.4' }, }; - expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); + expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); }); after(() => { @@ -150,7 +150,7 @@ describe('getElectronModulePath', () => { devDependencies: { electron: '^4.0.4' }, }; - expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); + expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); }); it('finds the top-level electron module despite the additional node_modules folder inside the package', async () => { @@ -160,7 +160,7 @@ describe('getElectronModulePath', () => { devDependencies: { electron: '^4.0.4' }, }; - expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); + expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); }); it('finds the correct electron module in nohoist mode', async () => { @@ -170,8 +170,8 @@ describe('getElectronModulePath', () => { devDependencies: { electron: '^13.0.0' }, }; - expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(fixtureDir, 'node_modules', 'electron')); - expect(await getElectronModulePath(fixtureDir, packageJSON)).not.to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); + expect(getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(fixtureDir, 'node_modules', 'electron')); + expect(getElectronModulePath(fixtureDir, packageJSON)).not.to.be.equal(path.join(workspaceDir, 'node_modules', 'electron')); }); after(() => {