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 478
/
Copy pathConfig.ts
554 lines (495 loc) · 16.9 KB
/
Config.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
import JsonFile, { JSONObject } from '@expo/json-file';
import fs from 'fs-extra';
import globby from 'globby';
import path from 'path';
import semver from 'semver';
import slug from 'slugify';
import {
AppJSONConfig,
ConfigFilePaths,
ExpRc,
ExpoConfig,
GetConfigOptions,
PackageJSONConfig,
Platform,
ProjectConfig,
ProjectTarget,
WriteConfigOptions,
} from './Config.types';
import { ConfigError } from './Errors';
import { getDynamicConfig, getStaticConfig } from './getConfig';
import { getRootPackageJsonPath, projectHasModule } from './Modules';
import { getExpoSDKVersion } from './Project';
/**
* If a config has an `expo` object then that will be used as the config.
* This method reduces out other top level values if an `expo` object exists.
*
* @param config Input config object to reduce
*/
function reduceExpoObject(config?: any): ExpoConfig | null {
if (!config) return config === undefined ? null : config;
if (typeof config.expo === 'object') {
// TODO: We should warn users in the future that if there are more values than "expo", those values outside of "expo" will be omitted in favor of the "expo" object.
return config.expo as ExpoConfig;
}
return config;
}
/**
* Get all platforms that a project is currently capable of running.
*
* @param projectRoot
* @param exp
*/
function getSupportedPlatforms(
projectRoot: string,
exp: Pick<ExpoConfig, 'nodeModulesPath'>
): Platform[] {
const platforms: Platform[] = [];
if (projectHasModule('react-native', projectRoot, exp)) {
platforms.push('ios', 'android');
}
if (projectHasModule('react-native-web', projectRoot, exp)) {
platforms.push('web');
}
return platforms;
}
/**
* Evaluate the config for an Expo project.
* If a function is exported from the `app.config.js` then a partial config will be passed as an argument.
* The partial config is composed from any existing app.json, and certain fields from the `package.json` like name and description.
*
*
* **Example**
* ```js
* module.exports = function({ config }) {
* // mutate the config before returning it.
* config.slug = 'new slug'
* return config;
* }
*
* **Supports**
* - `app.config.ts`
* - `app.config.js`
* - `app.config.json`
* - `app.json`
*
* @param projectRoot the root folder containing all of your application code
* @param options enforce criteria for a project config
*/
export function getConfig(projectRoot: string, options: GetConfigOptions = {}): ProjectConfig {
const paths = getConfigFilePaths(projectRoot);
const rawStaticConfig = paths.staticConfigPath ? getStaticConfig(paths.staticConfigPath) : null;
// For legacy reasons, always return an object.
const rootConfig = (rawStaticConfig || {}) as AppJSONConfig;
const staticConfig = reduceExpoObject(rawStaticConfig) || {};
const jsonFileWithNodeModulesPath = reduceExpoObject(rootConfig) as ExpoConfig;
// Can only change the package.json location if an app.json or app.config.json exists with nodeModulesPath
const [packageJson, packageJsonPath] = getPackageJsonAndPath(
projectRoot,
jsonFileWithNodeModulesPath
);
function fillAndReturnConfig(config: any, dynamicConfigObjectType: string | null) {
return {
...ensureConfigHasDefaultValues(
projectRoot,
config,
packageJson,
options.skipSDKVersionRequirement
),
dynamicConfigObjectType,
rootConfig,
dynamicConfigPath: paths.dynamicConfigPath,
staticConfigPath: paths.staticConfigPath,
};
}
// Fill in the static config
function getContextConfig(config: any = {}) {
return ensureConfigHasDefaultValues(projectRoot, config, packageJson, true).exp;
}
if (paths.dynamicConfigPath) {
// No app.config.json or app.json but app.config.js
const { exportedObjectType, config: rawDynamicConfig } = getDynamicConfig(
paths.dynamicConfigPath,
{
projectRoot,
staticConfigPath: paths.staticConfigPath,
packageJsonPath,
config: getContextConfig(staticConfig),
}
);
// Allow for the app.config.js to `export default null;`
// Use `dynamicConfigPath` to detect if a dynamic config exists.
const dynamicConfig = reduceExpoObject(rawDynamicConfig) || {};
return fillAndReturnConfig(dynamicConfig, exportedObjectType);
}
// No app.config.js but json or no config
return fillAndReturnConfig(staticConfig || {}, null);
}
export function getPackageJson(
projectRoot: string,
config: Pick<ExpoConfig, 'nodeModulesPath'> = {}
): PackageJSONConfig {
const [pkg] = getPackageJsonAndPath(projectRoot, config);
return pkg;
}
function getPackageJsonAndPath(
projectRoot: string,
config: Pick<ExpoConfig, 'nodeModulesPath'> = {}
): [PackageJSONConfig, string] {
const packageJsonPath = getRootPackageJsonPath(projectRoot, config);
return [JsonFile.read(packageJsonPath), packageJsonPath];
}
export function readConfigJson(
projectRoot: string,
skipValidation: boolean = false,
skipNativeValidation: boolean = false
): ProjectConfig {
const paths = getConfigFilePaths(projectRoot);
const rawStaticConfig = paths.staticConfigPath ? getStaticConfig(paths.staticConfigPath) : null;
const getConfigName = (): string => {
if (paths.staticConfigPath) ` \`${path.basename(paths.staticConfigPath)}\``;
return '';
};
let outputRootConfig: JSONObject | null = rawStaticConfig;
if (outputRootConfig === null || typeof outputRootConfig !== 'object') {
if (skipValidation) {
outputRootConfig = { expo: {} };
} else {
throw new ConfigError(
`Project at path ${path.resolve(
projectRoot
)} does not contain a valid Expo config${getConfigName()}`,
'NOT_OBJECT'
);
}
}
let exp = outputRootConfig.expo as ExpoConfig;
if (exp === null || typeof exp !== 'object') {
throw new ConfigError(
`Property 'expo' in${getConfigName()} for project at path ${path.resolve(
projectRoot
)} is not an object. Please make sure${getConfigName()} includes a managed Expo app config like this: ${APP_JSON_EXAMPLE}`,
'NO_EXPO'
);
}
exp = { ...exp };
const [pkg] = getPackageJsonAndPath(projectRoot, exp);
return {
...ensureConfigHasDefaultValues(projectRoot, exp, pkg, skipNativeValidation),
dynamicConfigPath: null,
dynamicConfigObjectType: null,
rootConfig: { ...outputRootConfig } as AppJSONConfig,
...paths,
};
}
export async function readConfigJsonAsync(
projectRoot: string,
skipValidation: boolean = false,
skipNativeValidation: boolean = false
): Promise<ProjectConfig> {
return readConfigJson(projectRoot, skipValidation, skipNativeValidation);
}
/**
* Get the static and dynamic config paths for a project. Also accounts for custom paths.
*
* @param projectRoot
*/
export function getConfigFilePaths(projectRoot: string): ConfigFilePaths {
const customPaths = getCustomConfigFilePaths(projectRoot);
if (customPaths) {
return customPaths;
}
return {
dynamicConfigPath: getDynamicConfigFilePath(projectRoot),
staticConfigPath: getStaticConfigFilePath(projectRoot),
};
}
function getCustomConfigFilePaths(projectRoot: string): ConfigFilePaths | null {
if (!customConfigPaths[projectRoot]) {
return null;
}
// If the user picks a custom config path, we will only use that and skip searching for a secondary config.
if (isDynamicFilePath(customConfigPaths[projectRoot])) {
return {
dynamicConfigPath: customConfigPaths[projectRoot],
staticConfigPath: null,
};
}
// Anything that's not js or ts will be treated as json.
return { staticConfigPath: customConfigPaths[projectRoot], dynamicConfigPath: null };
}
function getDynamicConfigFilePath(projectRoot: string): string | null {
for (const fileName of ['app.config.ts', 'app.config.js']) {
const configPath = path.join(projectRoot, fileName);
if (fs.existsSync(configPath)) {
return configPath;
}
}
return null;
}
function getStaticConfigFilePath(projectRoot: string): string | null {
for (const fileName of ['app.config.json', 'app.json']) {
const configPath = path.join(projectRoot, fileName);
if (fs.existsSync(configPath)) {
return configPath;
}
}
return null;
}
// TODO: This should account for dynamic configs
export function findConfigFile(
projectRoot: string
): { configPath: string; configName: string; configNamespace: 'expo' } {
let configPath: string;
// Check for a custom config path first.
if (customConfigPaths[projectRoot]) {
configPath = customConfigPaths[projectRoot];
// We shouldn't verify if the file exists because
// the user manually specified that this path should be used.
return {
configPath,
configName: path.basename(configPath),
configNamespace: 'expo',
};
} else {
// app.config.json takes higher priority over app.json
configPath = path.join(projectRoot, 'app.config.json');
if (!fs.existsSync(configPath)) {
configPath = path.join(projectRoot, 'app.json');
}
}
return {
configPath,
configName: path.basename(configPath),
configNamespace: 'expo',
};
}
// TODO: deprecate
export function configFilename(projectRoot: string): string {
return findConfigFile(projectRoot).configName;
}
export async function readExpRcAsync(projectRoot: string): Promise<ExpRc> {
const expRcPath = path.join(projectRoot, '.exprc');
return await JsonFile.readAsync(expRcPath, { json5: true, cantReadFileDefault: {} });
}
const customConfigPaths: { [projectRoot: string]: string } = {};
export function setCustomConfigPath(projectRoot: string, configPath: string): void {
customConfigPaths[projectRoot] = configPath;
}
/**
* Attempt to modify an Expo project config.
* This will only fully work if the project is using static configs only.
* Otherwise 'warn' | 'fail' will return with a message about why the config couldn't be updated.
* The potentially modified config object will be returned for testing purposes.
*
* @param projectRoot
* @param modifications modifications to make to an existing config
* @param readOptions options for reading the current config file
* @param writeOptions If true, the static config file will not be rewritten
*/
export async function modifyConfigAsync(
projectRoot: string,
modifications: Partial<ExpoConfig>,
readOptions: GetConfigOptions = {},
writeOptions: WriteConfigOptions = {}
): Promise<{ type: 'success' | 'warn' | 'fail'; message?: string; config: ExpoConfig | null }> {
const config = getConfig(projectRoot, readOptions);
if (config.dynamicConfigPath) {
// We cannot automatically write to a dynamic config.
/* Currently we should just use the safest approach possible, informing the user that they'll need to manually modify their dynamic config.
if (config.staticConfigPath) {
// Both a dynamic and a static config exist.
if (config.dynamicConfigObjectType === 'function') {
// The dynamic config exports a function, this means it possibly extends the static config.
} else {
// Dynamic config ignores the static config, there isn't a reason to automatically write to it.
// Instead we should warn the user to add values to their dynamic config.
}
}
*/
return {
type: 'warn',
message: `Cannot automatically write to dynamic config at: ${path.relative(
projectRoot,
config.dynamicConfigPath
)}`,
config: null,
};
} else if (config.staticConfigPath) {
// Static with no dynamic config, this means we can append to the config automatically.
let outputConfig: AppJSONConfig;
// If the config has an expo object (app.json) then append the options to that object.
if (config.rootConfig.expo) {
outputConfig = {
...config.rootConfig,
expo: { ...config.rootConfig.expo, ...modifications },
};
} else {
// Otherwise (app.config.json) just add the config modification to the top most level.
outputConfig = { ...config.rootConfig, ...modifications };
}
if (!writeOptions.dryRun) {
await JsonFile.writeAsync(config.staticConfigPath, outputConfig, { json5: false });
}
return { type: 'success', config: outputConfig };
}
return { type: 'fail', message: 'No config exists', config: null };
}
const APP_JSON_EXAMPLE = JSON.stringify({
expo: {
name: 'My app',
slug: 'my-app',
sdkVersion: '...',
},
});
function ensureConfigHasDefaultValues(
projectRoot: string,
exp: ExpoConfig,
pkg: JSONObject,
skipSDKVersionRequirement: boolean = false
): { exp: ExpoConfig; pkg: PackageJSONConfig } {
if (!exp) exp = {};
if (!exp.name) {
if (typeof pkg.name !== 'string') {
pkg.name = path.basename(projectRoot);
}
exp.name = pkg.name;
}
if (!exp.description && typeof pkg.description === 'string') {
exp.description = pkg.description;
}
if (!exp.slug && typeof exp.name === 'string') {
exp.slug = slug(exp.name.toLowerCase());
}
if (!exp.version) {
if (typeof pkg.version === 'string') {
exp.version = pkg.version;
} else {
pkg.version = '1.0.0';
}
exp.version = pkg.version;
}
if (exp.nodeModulesPath) {
exp.nodeModulesPath = path.resolve(projectRoot, exp.nodeModulesPath);
}
try {
exp.sdkVersion = getExpoSDKVersion(projectRoot, exp);
} catch (error) {
if (!skipSDKVersionRequirement) throw error;
}
if (!exp.platforms) {
exp.platforms = getSupportedPlatforms(projectRoot, exp);
}
return { exp, pkg };
}
export async function writeConfigJsonAsync(
projectRoot: string,
options: Object
): Promise<ProjectConfig> {
const paths = getConfigFilePaths(projectRoot);
let {
exp,
pkg,
rootConfig,
dynamicConfigObjectType,
staticConfigPath,
} = await readConfigJsonAsync(projectRoot);
exp = { ...rootConfig.expo, ...options };
rootConfig = { ...rootConfig, expo: exp };
if (paths.staticConfigPath) {
await JsonFile.writeAsync(paths.staticConfigPath, rootConfig, { json5: false });
} else {
console.log('Failed to write to config: ', options);
}
return {
exp,
pkg,
rootConfig,
staticConfigPath,
dynamicConfigObjectType,
...paths,
};
}
const DEFAULT_BUILD_PATH = `web-build`;
export function getWebOutputPath(config: { [key: string]: any } = {}): string {
if (process.env.WEBPACK_BUILD_OUTPUT_PATH) {
return process.env.WEBPACK_BUILD_OUTPUT_PATH;
}
const expo = config.expo || config || {};
return expo?.web?.build?.output || DEFAULT_BUILD_PATH;
}
export function getNameFromConfig(exp: ExpoConfig = {}): { appName: string; webName: string } {
// For RN CLI support
const appManifest = exp.expo || exp;
const { web = {} } = appManifest;
// rn-cli apps use a displayName value as well.
const appName = exp.displayName || appManifest.displayName || appManifest.name;
const webName = web.name || appName;
return {
appName,
webName,
};
}
export function getDefaultTarget(projectRoot: string): ProjectTarget {
const { exp } = getConfig(projectRoot, { skipSDKVersionRequirement: true });
// before SDK 37, always default to managed to preserve previous behavior
if (exp.sdkVersion && exp.sdkVersion !== 'UNVERSIONED' && semver.lt(exp.sdkVersion, '37.0.0')) {
return 'managed';
}
return isBareWorkflowProject(projectRoot) ? 'bare' : 'managed';
}
function isBareWorkflowProject(projectRoot: string): boolean {
const { pkg } = getConfig(projectRoot, {
skipSDKVersionRequirement: true,
});
if (pkg.dependencies && pkg.dependencies.expokit) {
return false;
}
if (fs.existsSync(path.resolve(projectRoot, 'ios'))) {
const xcodeprojFiles = globby.sync([path.join(projectRoot, 'ios', '/**/*.xcodeproj')]);
if (xcodeprojFiles.length) {
return true;
}
}
if (fs.existsSync(path.resolve(projectRoot, 'android'))) {
const gradleFiles = globby.sync([path.join(projectRoot, 'android', '/**/*.gradle')]);
if (gradleFiles.length) {
return true;
}
}
return false;
}
/**
* true if the file is .js or .ts
*
* @param filePath
*/
function isDynamicFilePath(filePath: string): boolean {
return !!filePath.match(/\.[j|t]s$/);
}
/**
* Returns a string describing the configurations used for the given project root.
* Will return null if no config is found.
*
* @param projectRoot
* @param projectConfig
*/
export function getProjectConfigDescription(
projectRoot: string,
projectConfig: ProjectConfig
): string | null {
if (projectConfig.dynamicConfigPath) {
const relativeDynamicConfigPath = path.relative(projectRoot, projectConfig.dynamicConfigPath);
if (projectConfig.staticConfigPath) {
return `Using dynamic config \`${relativeDynamicConfigPath}\` and static config \`${path.relative(
projectRoot,
projectConfig.staticConfigPath
)}\``;
}
return `Using dynamic config \`${relativeDynamicConfigPath}\``;
} else if (projectConfig.staticConfigPath) {
return `Using static config \`${path.relative(projectRoot, projectConfig.staticConfigPath)}\``;
}
return null;
}
export * from './Config.types';