(filePath: string, required: boolean)
| 61 | } |
| 62 | |
| 63 | function loadSingleConfigFile(filePath: string, required: boolean): Partial<CliFlags> { |
| 64 | if (!fs.existsSync(filePath)) { |
| 65 | if (required) { |
| 66 | throw new AppError('INVALID_ARGS', `Config file not found: ${filePath}`); |
| 67 | } |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | let raw: string; |
| 72 | try { |
| 73 | raw = fs.readFileSync(filePath, 'utf8'); |
| 74 | } catch (error) { |
| 75 | throw new AppError('INVALID_ARGS', `Failed to read config file: ${filePath}`, { |
| 76 | cause: error instanceof Error ? error.message : String(error), |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | let parsed: unknown; |
| 81 | try { |
| 82 | parsed = JSON.parse(raw); |
| 83 | } catch (error) { |
| 84 | throw new AppError('INVALID_ARGS', `Invalid JSON in config file: ${filePath}`, { |
| 85 | cause: error instanceof Error ? error.message : String(error), |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { |
| 90 | throw new AppError('INVALID_ARGS', `Config file must contain a JSON object: ${filePath}`); |
| 91 | } |
| 92 | |
| 93 | return parseConfigObject(parsed as Record<string, unknown>, `config file ${filePath}`); |
| 94 | } |
| 95 | |
| 96 | function parseConfigObject( |
| 97 | source: Record<string, unknown>, |
no test coverage detected