(
file: ConfigFileLike,
options: ImportConfigFileOptions = {}
)
| 30 | } |
| 31 | |
| 32 | export async function importConfigFile( |
| 33 | file: ConfigFileLike, |
| 34 | options: ImportConfigFileOptions = {} |
| 35 | ): Promise<ImportConfigFileResult> { |
| 36 | const parse = options.parse ?? parseConfigYaml; |
| 37 | const apply = options.apply ?? applyConfigurationToStores; |
| 38 | const logger = options.logger ?? console; |
| 39 | |
| 40 | try { |
| 41 | const content = await file.text(); |
| 42 | const result = parse(content); |
| 43 | |
| 44 | if (result.valid && result.config) { |
| 45 | apply(result.config); |
| 46 | logger.log(`[Config] Applied settings from ${file.name}`); |
| 47 | return { applied: true }; |
| 48 | } |
| 49 | |
| 50 | const errorMessages = formatConfigValidationErrors(result.errors); |
| 51 | if (options.logErrors) { |
| 52 | logger.error(`[Config] Invalid configuration: ${errorMessages}`); |
| 53 | } |
| 54 | return { applied: false, errorMessage: `Config error: ${errorMessages}` }; |
| 55 | } catch (err) { |
| 56 | const message = err instanceof Error ? err.message : 'Unknown error'; |
| 57 | if (options.logErrors) { |
| 58 | logger.error('[Config] Failed to load config file:', err); |
| 59 | } |
| 60 | return { applied: false, errorMessage: `Config error: ${message}` }; |
| 61 | } |
| 62 | } |
no test coverage detected