(
options: ConfigOptions = {},
)
| 126 | >; |
| 127 | |
| 128 | export async function getConfig( |
| 129 | options: ConfigOptions = {}, |
| 130 | ): Promise<Config | undefined> { |
| 131 | const { configPath, preset } = options; |
| 132 | |
| 133 | if (configPath != null) { |
| 134 | // Resolve the config file path relative to where cli was called. |
| 135 | return logger.task( |
| 136 | `Loading lighthouse config from ${configPath}`, |
| 137 | async () => { |
| 138 | const message = `Loaded lighthouse config from ${configPath}`; |
| 139 | if (configPath.endsWith('.json')) { |
| 140 | return { message, result: await readJsonFile<Config>(configPath) }; |
| 141 | } |
| 142 | if (/\.(ts|js|mjs)$/.test(configPath)) { |
| 143 | return { |
| 144 | message, |
| 145 | result: await importModule<Config>({ |
| 146 | filepath: configPath, |
| 147 | }), |
| 148 | }; |
| 149 | } |
| 150 | throw new Error( |
| 151 | `Unknown Lighthouse config file extension in ${configPath}`, |
| 152 | ); |
| 153 | }, |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | if (preset != null) { |
| 158 | const supportedPresets: Record< |
| 159 | NonNullable<LighthouseCliFlags['preset']>, |
| 160 | Config |
| 161 | > = { |
| 162 | desktop: desktopConfig, |
| 163 | perf: perfConfig, |
| 164 | experimental: experimentalConfig, |
| 165 | }; |
| 166 | // in reality, the preset could be a string not included in the type definition |
| 167 | const config: Config | undefined = supportedPresets[preset]; |
| 168 | if (config) { |
| 169 | logger.info(`Loaded config from ${ansis.bold(preset)} preset`); |
| 170 | return config; |
| 171 | } else { |
| 172 | logger.warn(`Preset "${preset}" is not supported`); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return undefined; |
| 177 | } |
| 178 | |
| 179 | export function enrichFlags( |
| 180 | flags: LighthouseCliFlags, |
no test coverage detected