1
1
import path from 'path' ;
2
-
3
2
import debug from 'debug' ;
4
- import findUp from 'find-up' ;
5
3
import fs from 'fs-extra' ;
6
4
import semver from 'semver' ;
7
5
@@ -20,28 +18,6 @@ function findElectronDep(dep: string): boolean {
20
18
return electronPackageNames . includes ( dep ) ;
21
19
}
22
20
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
-
45
21
export class PackageNotFoundError extends Error {
46
22
constructor ( packageName : string , dir : string ) {
47
23
super ( `Cannot find the package "${ packageName } ". Perhaps you need to run "${ safeYarnOrNpm ( ) } install" in "${ dir } "?` ) ;
@@ -63,23 +39,18 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
63
39
return packageName ;
64
40
}
65
41
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 {
69
47
throw new PackageNotFoundError ( packageName , dir ) ;
70
48
}
71
-
72
- const electronPackageJSONPath = path . join ( nodeModulesPath , 'package.json' ) ;
73
- if ( await fs . pathExists ( electronPackageJSONPath ) ) {
74
- return electronPackageJSONPath ;
75
- }
76
-
77
- return undefined ;
78
49
}
79
50
80
- export async function getElectronModulePath ( dir : string , packageJSON : PackageJSONWithDeps ) : Promise < string | undefined > {
51
+ export function getElectronModulePath ( dir : string , packageJSON : PackageJSONWithDeps ) : string | undefined {
81
52
const moduleName = getElectronModuleName ( packageJSON ) ;
82
- const packageJSONPath = await getElectronPackageJSONPath ( dir , moduleName ) ;
53
+ const packageJSONPath = getElectronPackageJSONPath ( dir , moduleName ) ;
83
54
if ( packageJSONPath ) {
84
55
return path . dirname ( packageJSONPath ) ;
85
56
}
@@ -95,7 +66,7 @@ export async function getElectronVersion(dir: string, packageJSON: PackageJSONWi
95
66
let version = packageJSON . devDependencies ! [ packageName ] ;
96
67
if ( ! semver . valid ( version ) ) {
97
68
// 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 ) ;
99
70
if ( electronPackageJSONPath ) {
100
71
const electronPackageJSON = await fs . readJson ( electronPackageJSONPath ) ;
101
72
version = electronPackageJSON . version ;
0 commit comments