1
+ import { createRequire } from 'node:module' ;
1
2
import path from 'path' ;
2
-
3
3
import debug from 'debug' ;
4
4
import findUp from 'find-up' ;
5
5
import fs from 'fs-extra' ;
6
6
import semver from 'semver' ;
7
7
8
8
import { safeYarnOrNpm } from './yarn-or-npm' ;
9
9
10
+ const require = createRequire ( import . meta. url ) ;
11
+
10
12
const d = debug ( 'electron-forge:electron-version' ) ;
11
13
12
14
const electronPackageNames = [ 'electron-nightly' , 'electron' ] ;
@@ -34,14 +36,6 @@ async function findAncestorNodeModulesPath(dir: string, packageName: string): Pr
34
36
return Promise . resolve ( undefined ) ;
35
37
}
36
38
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
-
45
39
export class PackageNotFoundError extends Error {
46
40
constructor ( packageName : string , dir : string ) {
47
41
super ( `Cannot find the package "${ packageName } ". Perhaps you need to run "${ safeYarnOrNpm ( ) } install" in "${ dir } "?` ) ;
@@ -63,23 +57,17 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
63
57
return packageName ;
64
58
}
65
59
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 ) {
69
63
throw new PackageNotFoundError ( packageName , dir ) ;
70
64
}
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 ;
78
66
}
79
67
80
68
export async function getElectronModulePath ( dir : string , packageJSON : PackageJSONWithDeps ) : Promise < string | undefined > {
81
69
const moduleName = getElectronModuleName ( packageJSON ) ;
82
- const packageJSONPath = await getElectronPackageJSONPath ( dir , moduleName ) ;
70
+ const packageJSONPath = getElectronPackageJSONPath ( dir , moduleName ) ;
83
71
if ( packageJSONPath ) {
84
72
return path . dirname ( packageJSONPath ) ;
85
73
}
@@ -95,7 +83,7 @@ export async function getElectronVersion(dir: string, packageJSON: PackageJSONWi
95
83
let version = packageJSON . devDependencies ! [ packageName ] ;
96
84
if ( ! semver . valid ( version ) ) {
97
85
// 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 ) ;
99
87
if ( electronPackageJSONPath ) {
100
88
const electronPackageJSON = await fs . readJson ( electronPackageJSONPath ) ;
101
89
version = electronPackageJSON . version ;
0 commit comments