* Step 2: reads the JSON config file and runs it through yargs to reject * unknown keys and apply coercions, without applying defaults.
(
configPath: string,
rawContent?: string,
)
| 88 | * unknown keys and apply coercions, without applying defaults. |
| 89 | */ |
| 90 | static parseConfigFile( |
| 91 | configPath: string, |
| 92 | rawContent?: string, |
| 93 | ): Partial<ParsedArguments> { |
| 94 | try { |
| 95 | const content = rawContent ?? readFileSync(configPath, 'utf-8'); |
| 96 | // Strip a leading UTF-8 byte order mark (U+FEFF). Windows PowerShell 5.1 |
| 97 | // writes one by default, and JSON.parse rejects it. |
| 98 | const fileContent: unknown = JSON.parse(content.replace(/^\uFEFF/, '')); |
| 99 | if (!isPlainObject(fileContent)) { |
| 100 | throw new Error('Config must be a JSON object'); |
| 101 | } |
| 102 | const configOptions = withoutDefaults(mcpOptions); |
| 103 | delete configOptions.config; |
| 104 | const parsed = yargs([]) |
| 105 | .parserConfiguration({ |
| 106 | 'strip-aliased': true, |
| 107 | 'camel-case-expansion': false, |
| 108 | }) |
| 109 | .options(configOptions) |
| 110 | .help(false) |
| 111 | .version(false) |
| 112 | .config(fileContent) |
| 113 | .strict() |
| 114 | .fail(false) |
| 115 | .exitProcess(false) |
| 116 | .parseSync([]); |
| 117 | return stripYargsPositionalArgs(parsed); |
| 118 | } catch (err) { |
| 119 | throw new Error(`Invalid JSON config file: ${getErrorMessage(err)}`); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | parseConfigFile( |
| 124 | configPath: string, |
no test coverage detected