|
| 1 | +import path from "node:path"; |
| 2 | +import { cosmiconfig } from "cosmiconfig"; |
| 3 | +import { extractorConfigSchema } from "./config-schema.js"; |
| 4 | +import type { ParsedExtractorConfig } from "./config-schema.js"; |
| 5 | +import type { |
| 6 | + CliOptions, |
| 7 | + ExtractorConfig, |
| 8 | +} from "./types.js"; |
| 9 | + |
| 10 | +export async function loadConfig( |
| 11 | + configPath: string, |
| 12 | + cliOptions: CliOptions, |
| 13 | +): Promise<ExtractorConfig> { |
| 14 | + const absoluteConfigPath = path.resolve(configPath); |
| 15 | + const configDir = path.dirname(absoluteConfigPath); |
| 16 | + const explorer = cosmiconfig("typesense-api-extractor", { |
| 17 | + searchPlaces: [ |
| 18 | + "package.json", |
| 19 | + ".typesense-api-extractorrc", |
| 20 | + ".typesense-api-extractorrc.json", |
| 21 | + ".typesense-api-extractorrc.yaml", |
| 22 | + ".typesense-api-extractorrc.yml", |
| 23 | + ".typesense-api-extractorrc.js", |
| 24 | + "typesense-api-extractor.config.js", |
| 25 | + "config.json", |
| 26 | + ], |
| 27 | + }); |
| 28 | + |
| 29 | + const result = await explorer.load(absoluteConfigPath); |
| 30 | + if (result === null) { |
| 31 | + throw new Error(`Could not load config file ${absoluteConfigPath}.`); |
| 32 | + } |
| 33 | + |
| 34 | + const parsedConfig = parseConfig(result.config, absoluteConfigPath); |
| 35 | + return { |
| 36 | + rootDir: parsedConfig.root_dir, |
| 37 | + routeFile: parsedConfig.route_file, |
| 38 | + sourceBranch: parsedConfig.source_branch, |
| 39 | + outputPath: |
| 40 | + cliOptions.outputPath !== undefined |
| 41 | + ? path.resolve(cliOptions.outputPath) |
| 42 | + : path.resolve(configDir, parsedConfig.output_path), |
| 43 | + diagnosticsOutputPath: |
| 44 | + parsedConfig.diagnostics_output_path === undefined |
| 45 | + ? undefined |
| 46 | + : path.resolve(configDir, parsedConfig.diagnostics_output_path), |
| 47 | + baseUrl: cliOptions.baseUrl ?? parsedConfig.base_url, |
| 48 | + maxCallDepth: cliOptions.maxCallDepth ?? parsedConfig.max_call_depth, |
| 49 | + failOnDiagnostics: |
| 50 | + cliOptions.failOnDiagnostics ?? parsedConfig.fail_on_diagnostics, |
| 51 | + failOnUnresolved: |
| 52 | + cliOptions.failOnUnresolved ?? parsedConfig.fail_on_unresolved, |
| 53 | + blacklist: parsedConfig.blacklist, |
| 54 | + includeHelpers: parsedConfig.include_helpers, |
| 55 | + overrides: parsedConfig.overrides, |
| 56 | + debug: { |
| 57 | + enabled: cliOptions.verbose || parsedConfig.debug.enabled, |
| 58 | + routeFilter: cliOptions.debugRoute ?? parsedConfig.debug.route_filter, |
| 59 | + }, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function parseConfig( |
| 64 | + config: unknown, |
| 65 | + configPath: string, |
| 66 | +): ParsedExtractorConfig { |
| 67 | + const parsedConfig = extractorConfigSchema.safeParse(config); |
| 68 | + if (parsedConfig.success) { |
| 69 | + return parsedConfig.data; |
| 70 | + } |
| 71 | + |
| 72 | + const message = parsedConfig.error.errors |
| 73 | + .map( |
| 74 | + (error) => |
| 75 | + `Error for config field '${error.path.join(".")}': ${error.message}`, |
| 76 | + ) |
| 77 | + .join("\n"); |
| 78 | + throw new Error(`Could not parse config file ${configPath}:\n${message}`); |
| 79 | +} |
0 commit comments