forked from electron/rebuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectron-locator.ts
37 lines (30 loc) · 1.13 KB
/
electron-locator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import * as fs from 'fs-extra';
import * as path from 'path';
import { searchForModule } from './search-module';
const electronModuleNames = ['electron', 'electron-prebuilt-compile'];
async function locateModuleByRequire(): Promise<string | null> {
for (const moduleName of electronModuleNames) {
try {
const modulePath = path.resolve(require.resolve(path.join(moduleName, 'package.json')), '..');
if (await fs.pathExists(path.join(modulePath, 'package.json'))) {
return modulePath;
}
} catch { // eslint-disable-line no-empty
}
}
return null
}
export async function locateElectronModule(
projectRootPath: string | undefined = undefined,
startDir: string | undefined = undefined,
): Promise<string | null> {
startDir ??= process.cwd();
for (const moduleName of electronModuleNames) {
const electronPaths = await searchForModule(startDir, moduleName, projectRootPath);
const electronPath = electronPaths.find(async (ePath: string) => await fs.pathExists(path.join(ePath, 'package.json')));
if (electronPath) {
return electronPath;
}
}
return locateModuleByRequire();
}