| 113 | } |
| 114 | |
| 115 | export const loadConfig = async (description: CLIConfigDescription, logger: Logger): Promise<CLIConfig> => { |
| 116 | const config = await loadConfigFile(description.configFilename) |
| 117 | const managedProfiles = await loadConfigFile(description.managedConfigFilename) |
| 118 | const mergedProfiles = mergeProfiles(config, managedProfiles) |
| 119 | |
| 120 | const profile = description.profileName in mergedProfiles |
| 121 | ? mergedProfiles[description.profileName] |
| 122 | : {} |
| 123 | |
| 124 | function stringConfigValue(keyName: string): string |
| 125 | function stringConfigValue(keyName: string, defaultValue: string): string |
| 126 | function stringConfigValue(keyName: string, defaultValue?: string): string | undefined { |
| 127 | if (keyName in profile) { |
| 128 | const configValue = profile[keyName] |
| 129 | if (typeof configValue === 'string') { |
| 130 | return configValue |
| 131 | } |
| 132 | logger.warn(`expected string value for config key ${keyName} but got ${typeof profile[keyName]}`) |
| 133 | return defaultValue |
| 134 | } |
| 135 | logger.trace(`key ${keyName} not found in ${description.profileName} config`) |
| 136 | return defaultValue |
| 137 | } |
| 138 | |
| 139 | function stringArrayConfigValue(keyName: string, defaultValue: string[] = []): string[] { |
| 140 | if (keyName in profile) { |
| 141 | const configValue = profile[keyName] |
| 142 | if (typeof configValue === 'string') { |
| 143 | return [configValue] |
| 144 | } |
| 145 | if (Array.isArray(configValue) && configValue.every(dir => typeof dir === 'string')) { |
| 146 | return configValue |
| 147 | } |
| 148 | logger.warn(`expected string or array of strings for config key ${keyName} but got ${typeof configValue}`) |
| 149 | return defaultValue |
| 150 | } |
| 151 | return defaultValue |
| 152 | } |
| 153 | |
| 154 | function booleanConfigValue(keyName: string, defaultValue = false): boolean { |
| 155 | if (keyName in profile) { |
| 156 | const configValue = profile[keyName] |
| 157 | if (typeof configValue === 'boolean') { |
| 158 | return configValue |
| 159 | } |
| 160 | logger.warn(`expected boolean value for config key ${keyName} but got ${typeof profile[keyName]}`) |
| 161 | return defaultValue |
| 162 | } |
| 163 | logger.trace(`key ${keyName} not found in ${description.profileName} config`) |
| 164 | return defaultValue |
| 165 | } |
| 166 | |
| 167 | return { |
| 168 | ...description, |
| 169 | profiles: config, |
| 170 | managedProfiles, |
| 171 | mergedProfiles, |
| 172 | profile, |