UNPKG

@redocly/openapi-core

Version:

See https://github.com/Redocly/redocly-cli

69 lines 2.47 kB
import * as fs from 'node:fs'; import * as path from 'node:path'; import { resolveConfig } from './config-resolvers.js'; import { Config } from './config.js'; import { BaseResolver, makeDocumentFromString, } from '../resolve.js'; import { CONFIG_FILE_NAME } from './constants.js'; export async function loadConfig(options = {}) { const { configPath = findConfig(), customExtends, externalRefResolver } = options; const resolver = externalRefResolver ?? new BaseResolver(); const rawConfigDocument = configPath ? await resolver.resolveDocument(null, configPath, true) : undefined; if (rawConfigDocument instanceof Error) { throw rawConfigDocument; } const { resolvedConfig, resolvedRefMap, plugins } = await resolveConfig({ rawConfigDocument: rawConfigDocument ? cloneConfigDocument(rawConfigDocument) : undefined, customExtends, configPath, externalRefResolver, }); const config = new Config(resolvedConfig, { configPath, document: rawConfigDocument, resolvedRefMap: resolvedRefMap, plugins, }); return config; } export function findConfig(dir) { if (!fs?.existsSync) return; const configPath = dir ? path.resolve(dir, CONFIG_FILE_NAME) : CONFIG_FILE_NAME; return fs.existsSync(configPath) ? configPath : undefined; } export async function createConfig(config, { configPath, externalRefResolver } = {}) { const rawConfigSource = typeof config === 'string' ? config : ''; const rawConfigDocument = makeDocumentFromString(rawConfigSource, configPath ?? ''); if (typeof config !== 'string' && config) { rawConfigDocument.parsed = config; } const { resolvedConfig, resolvedRefMap, plugins } = await resolveConfig({ rawConfigDocument: cloneConfigDocument(rawConfigDocument), configPath, externalRefResolver, }); return new Config(resolvedConfig, { configPath, document: rawConfigDocument, resolvedRefMap, plugins, }); } function cloneConfigDocument(document) { if (!document.parsed) { return document; } const { plugins, resolve, ...rest } = document.parsed; const cloned = { ...structuredClone(rest), plugins: plugins?.slice(), ...(resolve && { resolve: { ...resolve } }), }; return { ...document, parsed: cloned, }; } //# sourceMappingURL=load.js.map