Skip to content

Commit

Permalink
fix: lint config with refs
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKarpiuk authored and tatomyr committed Dec 1, 2023
1 parent b4d6100 commit 2b9c103
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
15 changes: 6 additions & 9 deletions packages/cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
Config,
findConfig,
formatProblems,
getMergedConfig,
getTotals,
lint,
lintConfig,
makeDocumentFromString,
stringifyYaml,
} from '@redocly/openapi-core';
import { ConfigValidationError } from '@redocly/openapi-core/lib/config';
import {
Expand All @@ -21,7 +18,7 @@ import {
printLintTotals,
printUnusedWarnings,
} from '../utils';
import type { OutputFormat, ProblemSeverity, RawConfig, RuleSeverity } from '@redocly/openapi-core';
import type { OutputFormat, ProblemSeverity, Document, RuleSeverity } from '@redocly/openapi-core';
import type { CommandOptions, Skips, Totals } from '../types';
import { blue, gray } from 'colorette';
import { performance } from 'perf_hooks';
Expand Down Expand Up @@ -130,13 +127,13 @@ export function lintConfigCallback(
return;
}

return async (config: RawConfig, resolvedRefMap?: ResolvedRefMap) => {
const configPath = findConfig(argv.config) || '';
const stringYaml = stringifyYaml(config);
const configContent = makeDocumentFromString(stringYaml, configPath);
return async (config: Document, resolvedRefMap?: ResolvedRefMap) => {
// const configPath = findConfig(argv.config) || '';
// const stringYaml = stringifyYaml(config);
// const configContent = makeDocumentFromString(stringYaml, configPath);

const problems = await lintConfig({
document: configContent,
document: config,
severity: (argv['lint-config'] || 'warn') as ProblemSeverity,
resolvedRefMap,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
stringifyYaml,
isAbsoluteUrl,
loadConfig,
RawConfig,
Document,
Region,
Config,
Oas3Definition,
Expand Down Expand Up @@ -426,7 +426,7 @@ export async function loadConfigAndHandleErrors(
options: {
configPath?: string;
customExtends?: string[];
processRawConfig?: (rawConfig: RawConfig, resolvedRefMap?: ResolvedRefMap) => void | Promise<void>;
processRawConfig?: (rawConfig: Document, resolvedRefMap?: ResolvedRefMap) => void | Promise<void>;
files?: string[];
region?: Region;
} = {}
Expand Down
34 changes: 21 additions & 13 deletions packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ export type BundleOptions = {
keepUrlRefs?: boolean;
};

export async function bundleConfig(
opts: {
ref?: string;
} & BundleOptions
) {

export async function resolveConfigFile(opts: {
ref?: string;
} & BundleOptions) {
const {
ref,
externalRefResolver = new BaseResolver(/* opts.config.resolve */),
base = null,
} = opts;

if (!(ref)) {
throw new Error('Reference to a config is required.\n');
}
Expand All @@ -68,7 +68,21 @@ export async function bundleConfig(

const types = normalizeTypes(ConfigTypes);

// console.log('TYPES', types.ConfigRoot);
const resolvedRefMap = await resolveDocument({
rootDocument: document,
rootType: types.ConfigRoot,
externalRefResolver,
});

return { document: document as Document, resolvedRefMap };
}

export async function bundleConfig(
document: Document,
resolvedRefMap: ResolvedRefMap,
) {

const types = normalizeTypes(ConfigTypes);

const ctx: BundleContext = {
problems: [],
Expand All @@ -77,12 +91,6 @@ export async function bundleConfig(
visitorsData: {},
};

const resolvedRefMap = await resolveDocument({
rootDocument: document,
rootType: types.ConfigRoot,
externalRefResolver,
});

const bundleVisitor = normalizeVisitors(
[
{
Expand All @@ -108,7 +116,7 @@ export async function bundleConfig(
ctx,
});

return { parsed: document.parsed, resolvedRefMap };
return document.parsed;
}

export async function bundle(
Expand Down
28 changes: 14 additions & 14 deletions packages/core/src/config/load.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import * as fs from 'fs';
import * as path from 'path';
import { RedoclyClient } from '../redocly';
import { isEmptyObject, loadYaml, doesYamlFileExist } from '../utils';
import { doesYamlFileExist, isEmptyObject } from '../utils';
import { parseYaml } from '../js-yaml';
import { Config, DOMAINS } from './config';
import { ConfigValidationError, transformConfig } from './utils';
import { resolveConfig } from './config-resolvers';
import type {
DeprecatedInRawConfig,
FlatRawConfig,
RawConfig,
RawUniversalConfig,
Region,
} from './types';
import type { RawConfig, RawUniversalConfig, Region } from './types';
import { RegionalTokenWithValidity } from '../redocly/redocly-client-types';
import { bundleConfig } from '../bundle';
import {bundleConfig, resolveConfigFile} from '../bundle';
import { Document } from '../resolve';
import type { ResolvedRefMap } from '../resolve';

async function addConfigMetadata({
Expand Down Expand Up @@ -78,7 +73,10 @@ export async function loadConfig(
options: {
configPath?: string;
customExtends?: string[];
processRawConfig?: (rawConfig: RawConfig, resolvedRefMap?: ResolvedRefMap) => void | Promise<void>;
processRawConfig?: (
rawConfig: Document,
resolvedRefMap?: ResolvedRefMap
) => void | Promise<void>;
files?: string[];
region?: Region;
} = {}
Expand Down Expand Up @@ -118,17 +116,19 @@ export function findConfig(dir?: string): string | undefined {

export async function getConfig(
configPath: string | undefined = findConfig(),
processRawConfig?: (rawConfig: RawConfig, resolvedRefMap?: ResolvedRefMap) => void | Promise<void>
processRawConfig?: (rawConfig: Document, resolvedRefMap?: ResolvedRefMap) => void | Promise<void>
): Promise<RawConfig> {
if (!configPath || !doesYamlFileExist(configPath)) return {};
try {
// const rawConfig =
// (await loadYaml<RawConfig & DeprecatedInRawConfig & FlatRawConfig>(configPath)) || {};
const {parsed: rawConfig, resolvedRefMap} = await bundleConfig({ref: configPath, config: {} as any});
const { document, resolvedRefMap } = await resolveConfigFile({ ref: configPath, config: {} as any });
if (typeof processRawConfig === 'function') {
await processRawConfig(rawConfig, resolvedRefMap);
await processRawConfig(document, resolvedRefMap);
}
return transformConfig(rawConfig);
const bundledConfig = await bundleConfig(document, resolvedRefMap);
return transformConfig(bundledConfig);

} catch (e) {
if (e instanceof ConfigValidationError) {
throw e;
Expand Down

0 comments on commit 2b9c103

Please sign in to comment.