-
Notifications
You must be signed in to change notification settings - Fork 798
/
Copy pathconfig-utils.ts
75 lines (67 loc) · 2.43 KB
/
config-utils.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
import type * as d from '../../declarations';
import { isAbsolute, join } from 'path';
import { isBoolean } from '@utils';
export const getAbsolutePath = (config: d.Config | d.UnvalidatedConfig, dir: string) => {
if (!isAbsolute(dir)) {
dir = join(config.rootDir, dir);
}
return dir;
};
/**
* This function does two things:
*
* 1. If you pass a `flagName`, it will hoist that `flagName` out of the
* `ConfigFlags` object and onto the 'root' level (if you will) of the
* `config` under the `configName` (`keyof d.Config`) that you pass.
* 2. If you _don't_ pass a `flagName` it will just set the value you supply
* on the config.
*
* @param config the config that we want to update
* @param configName the key we're setting on the config
* @param flagName either the name of a ConfigFlag prop we want to hoist up or null
* @param defaultValue the default value we should set!
*/
export const setBooleanConfig = <K extends keyof d.Config>(
config: d.UnvalidatedConfig,
configName: (K & keyof d.ConfigFlags) | K,
flagName: keyof d.ConfigFlags | null,
defaultValue: d.Config[K]
) => {
if (flagName) {
let flagValue = config.flags?.[flagName];
if (isBoolean(flagValue)) {
config[configName] = flagValue;
}
}
const userConfigName = getUserConfigName(config, configName);
if (typeof config[userConfigName] === 'function') {
config[userConfigName] = !!config[userConfigName]();
}
if (isBoolean(config[userConfigName])) {
config[configName] = config[userConfigName];
} else {
config[configName] = defaultValue;
}
};
/**
* Find any possibly mis-capitalized configuration names on the config, logging
* and warning if one is found.
*
* @param config the user-supplied config that we're dealing with
* @param correctConfigName the configuration name that we're checking for right now
* @returns a string container a mis-capitalized config name found on the
* config object, if any.
*/
const getUserConfigName = (config: d.UnvalidatedConfig, correctConfigName: keyof d.Config): string => {
const userConfigNames = Object.keys(config);
for (const userConfigName of userConfigNames) {
if (userConfigName.toLowerCase() === correctConfigName.toLowerCase()) {
if (userConfigName !== correctConfigName) {
config.logger?.warn(`config "${userConfigName}" should be "${correctConfigName}"`);
return userConfigName;
}
break;
}
}
return correctConfigName;
};