-
Notifications
You must be signed in to change notification settings - Fork 934
Expand file tree
/
Copy pathreadConfigFromDisk.ts
More file actions
78 lines (66 loc) · 2.01 KB
/
readConfigFromDisk.ts
File metadata and controls
78 lines (66 loc) · 2.01 KB
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
import cosmiconfig from 'cosmiconfig';
import {JoiError} from './errors';
import * as schema from './schema';
import {
UserConfig,
UserDependencyConfig,
} from '@react-native-community/cli-types';
import {logger, inlineString} from '@react-native-community/cli-tools';
import chalk from 'chalk';
/**
* Places to look for the configuration file.
*/
const searchPlaces = ['react-native.config.js'];
/**
* Reads a project configuration as defined by the user in the current
* workspace.
*/
export function readConfigFromDisk(rootFolder: string): UserConfig {
const explorer = cosmiconfig('react-native', {
searchPlaces,
stopDir: rootFolder,
});
const searchResult = explorer.searchSync(rootFolder);
const config = searchResult ? searchResult.config : undefined;
const result = schema.projectConfig.validate(config);
if (result.error) {
throw new JoiError(result.error);
}
return result.value as UserConfig;
}
/**
* Reads a dependency configuration as defined by the developer
* inside `node_modules`.
*/
export function readDependencyConfigFromDisk(
rootFolder: string,
dependencyName: string,
): UserDependencyConfig {
const explorer = cosmiconfig('react-native', {
stopDir: rootFolder,
searchPlaces,
});
const searchResult = explorer.searchSync(rootFolder);
const config = searchResult ? searchResult.config : emptyDependencyConfig;
const result = schema.dependencyConfig.validate(config, {abortEarly: false});
if (result.error) {
const validationError = new JoiError(result.error);
logger.warn(
inlineString(`
Package ${chalk.bold(
dependencyName,
)} contains invalid configuration: ${chalk.bold(
validationError.message,
)}.
Please verify it's properly linked using "npx react-native config" command and contact the package maintainers about this.`),
);
}
return result.value as UserDependencyConfig;
}
const emptyDependencyConfig = {
dependency: {
platforms: {},
},
commands: [],
platforms: {},
};