(configFilePath: string, configName: string, config: SWACLIConfig)
| 181 | * @param config The configuration object to save. |
| 182 | */ |
| 183 | export async function writeConfigFile(configFilePath: string, configName: string, config: SWACLIConfig) { |
| 184 | let configFile: SWACLIConfigFile = { |
| 185 | // TODO: find node_modules/ path and use local schema if found |
| 186 | $schema: swaCliConfigSchemaUrl, |
| 187 | configurations: {}, |
| 188 | }; |
| 189 | |
| 190 | if (swaCliConfigFileExists(configFilePath)) { |
| 191 | logger.silly(`Loading existing swa-cli.config.json file at ${configFilePath}`); |
| 192 | |
| 193 | try { |
| 194 | const configJson = await readFile(configFilePath, "utf-8"); |
| 195 | configFile = JSON.parse(configJson) as SWACLIConfigFile; |
| 196 | } catch (error) { |
| 197 | logger.error(`Error parsing ${configFilePath}`); |
| 198 | if (error instanceof Error) { |
| 199 | logger.error(error); |
| 200 | } |
| 201 | logger.error("Cannot update existing configuration file."); |
| 202 | logger.error("Please fix or delete your swa-cli.config.json file and try again."); |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (typeof configFile !== "object" || configFile.constructor !== Object) { |
| 208 | logger.error(`Error parsing ${configFilePath}`); |
| 209 | logger.error("Invalid configuration content found."); |
| 210 | logger.error("Please fix or delete your swa-cli.config.json file and try again."); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (configFile.configurations === undefined) { |
| 215 | logger.silly(`Creating "configurations" property in swa-cli.config.json file at ${configFilePath}`); |
| 216 | configFile.configurations = {}; |
| 217 | } |
| 218 | |
| 219 | configFile.configurations[configName] = config; |
| 220 | |
| 221 | try { |
| 222 | logger.silly(`Writing configuration "${configName}" to swa-cli.config.json`); |
| 223 | logger.silly(config); |
| 224 | |
| 225 | await writeFile(configFilePath, JSON.stringify(configFile, null, 2)); |
| 226 | } catch (error) { |
| 227 | logger.error(`Error writing configuration to ${configFilePath}`); |
| 228 | if (error instanceof Error) { |
| 229 | logger.error(error); |
| 230 | } |
| 231 | } |
| 232 | } |
no test coverage detected
searching dependent graphs…