Skip to content

Commit

Permalink
fix: get Electron dependency path
Browse files Browse the repository at this point in the history
  • Loading branch information
rtritto committed Jun 8, 2024
1 parent 38f64e4 commit e92088f
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions packages/utils/core-utils/src/electron-version.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { createRequire } from 'node:module';
import path from 'path';

import debug from 'debug';
import findUp from 'find-up';
import fs from 'fs-extra';
import semver from 'semver';

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

const require = createRequire(import.meta.url);

const d = debug('electron-forge:electron-version');

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

async function determineNodeModulesPath(dir: string, packageName: string): Promise<string | undefined> {
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}"?`);
Expand All @@ -63,23 +57,17 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
return packageName;
}

async function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string | undefined> {
const nodeModulesPath = await determineNodeModulesPath(dir, packageName);
if (!nodeModulesPath) {
function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string> {
const electronPath = require.resolve(`${packageName}/package.json`, { paths: [dir] });
if (!electronPath) {
throw new PackageNotFoundError(packageName, dir);
}

const electronPackageJSONPath = path.join(nodeModulesPath, 'package.json');
if (await fs.pathExists(electronPackageJSONPath)) {
return electronPackageJSONPath;
}

return undefined;
return electronPath;
}

export async function getElectronModulePath(dir: string, packageJSON: PackageJSONWithDeps): Promise<string | undefined> {
const moduleName = getElectronModuleName(packageJSON);
const packageJSONPath = await getElectronPackageJSONPath(dir, moduleName);
const packageJSONPath = getElectronPackageJSONPath(dir, moduleName);
if (packageJSONPath) {
return path.dirname(packageJSONPath);
}
Expand All @@ -95,7 +83,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;
Expand Down

0 comments on commit e92088f

Please sign in to comment.