This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 477
/
Modules.ts
82 lines (73 loc) · 2.2 KB
/
Modules.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { stat, Stats, statSync } from 'fs-extra';
import { join, resolve } from 'path';
import resolveFrom from 'resolve-from';
import { ExpoConfig } from './Config.types';
import { ConfigError } from './Errors';
export function resolveModule(
request: string,
projectRoot: string,
exp: Pick<ExpoConfig, 'nodeModulesPath'>
): string {
const fromDir = exp.nodeModulesPath ? exp.nodeModulesPath : projectRoot;
return resolveFrom(fromDir, request);
}
export function projectHasModule(
modulePath: string,
projectRoot: string,
exp: Pick<ExpoConfig, 'nodeModulesPath'>
): string | undefined {
const fromDir = exp.nodeModulesPath ? exp.nodeModulesPath : projectRoot;
return resolveFrom.silent(fromDir, modulePath);
}
export function moduleNameFromPath(modulePath: string): string {
if (modulePath.startsWith('@')) {
const [org, packageName] = modulePath.split('/');
if (org && packageName) {
return [org, packageName].join('/');
}
return modulePath;
}
const [packageName] = modulePath.split('/');
return packageName ? packageName : modulePath;
}
/**
* A non-failing version of async FS stat.
*
* @param file
*/
async function statAsync(file: string): Promise<Stats | null> {
try {
return await stat(file);
} catch {
return null;
}
}
export async function fileExistsAsync(file: string): Promise<boolean> {
return (await statAsync(file))?.isFile() ?? false;
}
export async function directoryExistsAsync(file: string): Promise<boolean> {
return (await statAsync(file))?.isDirectory() ?? false;
}
export function fileExists(file: string): boolean {
try {
return statSync(file).isFile();
} catch (e) {
return false;
}
}
export function getRootPackageJsonPath(
projectRoot: string,
exp: Partial<Pick<ExpoConfig, 'nodeModulesPath'>>
): string {
const packageJsonPath =
'nodeModulesPath' in exp && typeof exp.nodeModulesPath === 'string'
? join(resolve(projectRoot, exp.nodeModulesPath), 'package.json')
: join(projectRoot, 'package.json');
if (!fileExists(packageJsonPath)) {
throw new ConfigError(
`The expected package.json path: ${packageJsonPath} does not exist`,
'MODULE_NOT_FOUND'
);
}
return packageJsonPath;
}