-
Notifications
You must be signed in to change notification settings - Fork 906
/
loadConfig.ts
180 lines (167 loc) · 4.94 KB
/
loadConfig.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import path from 'path';
import {
UserDependencyConfig,
ProjectConfig,
DependencyConfig,
UserConfig,
Config,
Command,
} from '@react-native-community/cli-types';
import {
findProjectRoot,
version,
resolveNodeModuleDir,
UnknownProjectError,
} from '@react-native-community/cli-tools';
import findDependencies from './findDependencies';
import resolveReactNativePath from './resolveReactNativePath';
import {
readConfigFromDisk,
readDependencyConfigFromDisk,
} from './readConfigFromDisk';
import assign from './assign';
import merge from './merge';
function getDependencyConfig(
root: string,
dependencyName: string,
finalConfig: Config,
config: UserDependencyConfig,
userConfig: UserConfig,
): DependencyConfig {
return merge(
{
root,
name: dependencyName,
platforms: Object.keys(finalConfig.platforms).reduce(
(dependency, platform) => {
const platformConfig = finalConfig.platforms[platform];
dependency[platform] =
// Linking platforms is not supported
Object.keys(config.platforms).length > 0 || !platformConfig
? null
: platformConfig.dependencyConfig(
root,
config.dependency.platforms?.[platform],
);
return dependency;
},
{} as Config['platforms'],
),
},
userConfig.dependencies[dependencyName] || {},
) as DependencyConfig;
}
// Try our best to figure out what version of React Native we're running. This is
// currently being used to get our deeplinks working, so it's only worried with
// the major and minor version.
function getReactNativeVersion(reactNativePath: string) {
try {
let semver = version.current(reactNativePath);
if (semver) {
// Retain only these version, since they correspond with our documentation.
return `${semver.major}.${semver.minor}`;
}
} catch (e) {
// If we don't seem to be in a well formed project, give up quietly.
if (!(e instanceof UnknownProjectError)) {
throw e;
}
}
return 'unknown';
}
const removeDuplicateCommands = <T extends boolean>(commands: Command<T>[]) => {
const uniqueCommandsMap = new Map();
commands.forEach((command) => {
uniqueCommandsMap.set(command.name, command);
});
return Array.from(uniqueCommandsMap.values());
};
/**
* Loads CLI configuration
*/
function loadConfig({
projectRoot = findProjectRoot(),
selectedPlatform,
}: {
projectRoot?: string;
selectedPlatform?: string;
}): Config {
let lazyProject: ProjectConfig;
const userConfig = readConfigFromDisk(projectRoot);
const initialConfig: Config = {
root: projectRoot,
get reactNativePath() {
return userConfig.reactNativePath
? path.resolve(projectRoot, userConfig.reactNativePath)
: resolveReactNativePath(projectRoot);
},
get reactNativeVersion() {
return getReactNativeVersion(initialConfig.reactNativePath);
},
dependencies: userConfig.dependencies,
commands: userConfig.commands,
healthChecks: userConfig.healthChecks || [],
platforms: userConfig.platforms,
assets: userConfig.assets,
get project() {
if (lazyProject) {
return lazyProject;
}
lazyProject = {};
for (const platform in finalConfig.platforms) {
const platformConfig = finalConfig.platforms[platform];
if (platformConfig) {
lazyProject[platform] = platformConfig.projectConfig(
projectRoot,
userConfig.project[platform] || {},
);
}
}
return lazyProject;
},
};
const finalConfig = Array.from(
new Set([
...Object.keys(userConfig.dependencies),
...findDependencies(projectRoot),
]),
).reduce((acc: Config, dependencyName) => {
const localDependencyRoot =
userConfig.dependencies[dependencyName] &&
userConfig.dependencies[dependencyName].root;
try {
let root =
localDependencyRoot ||
resolveNodeModuleDir(projectRoot, dependencyName);
let config = readDependencyConfigFromDisk(root, dependencyName);
return assign({}, acc, {
dependencies: assign({}, acc.dependencies, {
get [dependencyName](): DependencyConfig {
return getDependencyConfig(
root,
dependencyName,
finalConfig,
config,
userConfig,
);
},
}),
commands: removeDuplicateCommands([
...config.commands,
...acc.commands,
]),
platforms: {
...acc.platforms,
...(selectedPlatform && config.platforms[selectedPlatform]
? {[selectedPlatform]: config.platforms[selectedPlatform]}
: config.platforms),
},
healthChecks: [...acc.healthChecks, ...config.healthChecks],
}) as Config;
} catch {
return acc;
}
}, initialConfig);
return finalConfig;
}
export default loadConfig;