* Parse raw content into a JavaScript object based on file type.
( content: string, filename: string, filetype: string | undefined = undefined )
| 11 | * Parse raw content into a JavaScript object based on file type. |
| 12 | */ |
| 13 | function parseRawConfig( |
| 14 | content: string, |
| 15 | filename: string, |
| 16 | filetype: string | undefined = undefined |
| 17 | ): unknown { |
| 18 | if (filetype === undefined) { |
| 19 | filetype = path.extname(filename.toLowerCase()); |
| 20 | } |
| 21 | try { |
| 22 | if (filetype === '.json') { |
| 23 | return JSON.parse(content); |
| 24 | } else if (filetype === '.toml') { |
| 25 | return toml.parse(content); |
| 26 | } else if (filetype === '.yaml' || filetype === '.yml') { |
| 27 | return yaml.load(content, { filename }); |
| 28 | } else { |
| 29 | throw new PythonAnalysisError({ |
| 30 | message: `Could not parse config file "${filename}": unrecognized config format`, |
| 31 | code: 'PYTHON_CONFIG_UNKNOWN_FORMAT', |
| 32 | path: filename, |
| 33 | }); |
| 34 | } |
| 35 | } catch (error: unknown) { |
| 36 | if (error instanceof PythonAnalysisError) { |
| 37 | throw error; |
| 38 | } |
| 39 | if (error instanceof Error) { |
| 40 | throw new PythonAnalysisError({ |
| 41 | message: `Could not parse config file "${filename}": ${error.message}`, |
| 42 | code: 'PYTHON_CONFIG_PARSE_ERROR', |
| 43 | path: filename, |
| 44 | fileContent: content, |
| 45 | }); |
| 46 | } |
| 47 | throw error; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Parse config content and validate it against a zod schema. |
no test coverage detected