-
Notifications
You must be signed in to change notification settings - Fork 905
/
Copy pathloadConfig.ts
161 lines (150 loc) · 4.43 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
import path from 'path';
import {
UserDependencyConfig,
ProjectConfig,
DependencyConfig,
UserConfig,
Config,
} 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,
isPlatform: boolean,
): 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
isPlatform || !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';
}
/**
* Loads CLI configuration
*/
function loadConfig(projectRoot: string = findProjectRoot()): 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: [],
platforms: userConfig.platforms,
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);
const isPlatform = Object.keys(config.platforms).length > 0;
return assign({}, acc, {
dependencies: assign({}, acc.dependencies, {
get [dependencyName](): DependencyConfig {
return getDependencyConfig(
root,
dependencyName,
finalConfig,
config,
userConfig,
isPlatform,
);
},
}),
commands: [...acc.commands, ...config.commands],
platforms: {
...acc.platforms,
...config.platforms,
},
healthChecks: [...acc.healthChecks, ...config.healthChecks],
}) as Config;
} catch {
return acc;
}
}, initialConfig);
return finalConfig;
}
export default loadConfig;