import { CONFIG_BUNDLER_VISITOR_ID, PLUGINS_COLLECTOR_VISITOR_ID } from '../config/constants.js'; import type { Plugin, ResolvedConfig } from '../config/types.js'; import { configBundlerVisitor, pluginsCollectorVisitor, type ConfigBundlerVisitorData, type PluginsCollectorVisitorData, } from '../config/visitors.js'; import { detectSpec } from '../detect-spec.js'; import { getTypes } from '../oas-types.js'; import { BaseResolver, makeDocumentFromString, type Document, type ResolvedRefMap, } from '../resolve.js'; import type { NormalizedNodeType } from '../types/index.js'; import { NormalizedConfigTypes } from '../types/redocly-yaml.js'; import type { CollectSpecData } from '../utils/types.js'; import { walkDocument, type NormalizedProblem, type WalkContext } from '../walk.js'; import { bundleDocument, type CoreBundleOptions } from './bundle-document.js'; export function collectConfigPlugins( document: Document, resolvedRefMap: ResolvedRefMap, rootConfigDir: string ) { const visitorsData: PluginsCollectorVisitorData = { plugins: [], rootConfigDir }; const ctx: BundleContext = { problems: [], specVersion: 'config', refTypes: new Map(), visitorsData: { [PLUGINS_COLLECTOR_VISITOR_ID]: visitorsData, }, }; walkDocument({ document, rootType: NormalizedConfigTypes.ConfigRoot, normalizedVisitors: pluginsCollectorVisitor, resolvedRefMap, ctx, }); return visitorsData.plugins; } export function bundleConfig( document: Document, resolvedRefMap: ResolvedRefMap, plugins: Plugin[], skipPluginEval = false ): ResolvedConfig { const visitorsData: ConfigBundlerVisitorData = { plugins, skipPluginEval }; const ctx: BundleContext = { problems: [], specVersion: 'config', refTypes: new Map(), visitorsData: { [CONFIG_BUNDLER_VISITOR_ID]: visitorsData, }, }; walkDocument({ document, rootType: NormalizedConfigTypes.ConfigRoot, normalizedVisitors: configBundlerVisitor, resolvedRefMap, ctx, }); return document.parsed ?? {}; } export async function bundle( opts: { ref?: string; doc?: Document; collectSpecData?: CollectSpecData; } & CoreBundleOptions ) { const { ref, doc, externalRefResolver = new BaseResolver(opts.config.resolve), base = null, } = opts; if (!(ref || doc)) { throw new Error('Document or reference is required.\n'); } const document = doc === undefined ? await externalRefResolver.resolveDocument(base, ref!, true) : doc; if (document instanceof Error) { throw document; } opts.collectSpecData?.(document); return bundleDocument({ document, ...opts, externalRefResolver, types: getTypes(detectSpec(document.parsed)), }); } export async function bundleFromString( opts: { source: string; absoluteRef?: string; } & CoreBundleOptions ) { const { source, absoluteRef, externalRefResolver = new BaseResolver(opts.config.resolve) } = opts; const document = makeDocumentFromString(source, absoluteRef || '/'); return bundleDocument({ document, ...opts, externalRefResolver, types: getTypes(detectSpec(document.parsed)), }); } type BundleContext = WalkContext; export type BundleResult = { bundle: Document; problems: NormalizedProblem[]; fileDependencies: Set; rootType: NormalizedNodeType; refTypes?: Map; visitorsData: Record>; };