mirror of
https://github.com/LukeHagar/redocly-cli.git
synced 2025-12-09 12:47:48 +00:00
chore: Setup and run Prettier (#797)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { basename, dirname, extname, join, resolve } from 'path';
|
||||
import { blue, gray, green, red, yellow } from 'colorette';
|
||||
import { performance } from "perf_hooks";
|
||||
import { performance } from 'perf_hooks';
|
||||
import * as glob from 'glob-promise';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
@@ -13,19 +13,23 @@ import {
|
||||
ResolveError,
|
||||
YamlParseError,
|
||||
parseYaml,
|
||||
stringifyYaml
|
||||
stringifyYaml,
|
||||
} from '@redocly/openapi-core';
|
||||
import { Totals, outputExtensions, Entrypoint } from './types';
|
||||
|
||||
export async function getFallbackEntryPointsOrExit(argsEntrypoints: string[] | undefined, config: Config): Promise<Entrypoint[]> {
|
||||
const { apis } = config;
|
||||
const shouldFallbackToAllDefinitions = !isNotEmptyArray(argsEntrypoints) && apis && Object.keys(apis).length > 0;
|
||||
export async function getFallbackEntryPointsOrExit(
|
||||
argsEntrypoints: string[] | undefined,
|
||||
config: Config
|
||||
): Promise<Entrypoint[]> {
|
||||
const { apis } = config;
|
||||
const shouldFallbackToAllDefinitions =
|
||||
!isNotEmptyArray(argsEntrypoints) && apis && Object.keys(apis).length > 0;
|
||||
const res = shouldFallbackToAllDefinitions
|
||||
? Object.entries(apis).map(([alias, { root }]) => ({
|
||||
path: resolve(getConfigDirectory(config), root),
|
||||
alias,
|
||||
}))
|
||||
: (await expandGlobsInEntrypoints(argsEntrypoints!, config));
|
||||
: await expandGlobsInEntrypoints(argsEntrypoints!, config);
|
||||
if (!isNotEmptyArray(res)) {
|
||||
process.stderr.write('error: missing required argument `entrypoints`.\n');
|
||||
process.exit(1);
|
||||
@@ -48,11 +52,15 @@ function getAliasOrPath(config: Config, aliasOrPath: string): Entrypoint {
|
||||
}
|
||||
|
||||
async function expandGlobsInEntrypoints(args: string[], config: Config) {
|
||||
return (await Promise.all((args as string[]).map(async aliasOrPath => {
|
||||
return glob.hasMagic(aliasOrPath)
|
||||
? (await glob(aliasOrPath)).map((g: string) => getAliasOrPath(config, g))
|
||||
: getAliasOrPath(config, aliasOrPath);
|
||||
}))).flat();
|
||||
return (
|
||||
await Promise.all(
|
||||
(args as string[]).map(async (aliasOrPath) => {
|
||||
return glob.hasMagic(aliasOrPath)
|
||||
? (await glob(aliasOrPath)).map((g: string) => getAliasOrPath(config, g))
|
||||
: getAliasOrPath(config, aliasOrPath);
|
||||
})
|
||||
)
|
||||
).flat();
|
||||
}
|
||||
|
||||
export function getExecutionTime(startedAt: number) {
|
||||
@@ -75,10 +83,7 @@ export function pathToFilename(path: string, pathSeparator: string) {
|
||||
}
|
||||
|
||||
export function escapeLanguageName(lang: string) {
|
||||
return lang
|
||||
.replace(/#/g, "_sharp")
|
||||
.replace(/\//, '_')
|
||||
.replace(/\s/g, '');
|
||||
return lang.replace(/#/g, '_sharp').replace(/\//, '_').replace(/\s/g, '');
|
||||
}
|
||||
|
||||
export class CircularJSONNotSupportedError extends Error {
|
||||
@@ -168,18 +173,19 @@ export function pluralize(label: string, num: number) {
|
||||
export function handleError(e: Error, ref: string) {
|
||||
if (e instanceof ResolveError) {
|
||||
process.stderr.write(
|
||||
`Failed to resolve entrypoint definition at ${ref}:\n\n - ${e.message}.\n\n`,
|
||||
`Failed to resolve entrypoint definition at ${ref}:\n\n - ${e.message}.\n\n`
|
||||
);
|
||||
} else if (e instanceof YamlParseError) {
|
||||
process.stderr.write(
|
||||
`Failed to parse entrypoint definition at ${ref}:\n\n - ${e.message}.\n\n`,
|
||||
`Failed to parse entrypoint definition at ${ref}:\n\n - ${e.message}.\n\n`
|
||||
);
|
||||
// TODO: codeframe
|
||||
} else { // @ts-ignore
|
||||
} else {
|
||||
// @ts-ignore
|
||||
if (e instanceof CircularJSONNotSupportedError) {
|
||||
process.stderr.write(
|
||||
red(`Detected circular reference which can't be converted to JSON.\n`) +
|
||||
`Try to use ${blue('yaml')} output or remove ${blue('--dereferenced')}.\n\n`,
|
||||
`Try to use ${blue('yaml')} output or remove ${blue('--dereferenced')}.\n\n`
|
||||
);
|
||||
} else {
|
||||
process.stderr.write(`Something went wrong when processing ${ref}:\n\n - ${e.message}.\n\n`);
|
||||
@@ -200,30 +206,27 @@ export function printLintTotals(totals: Totals, definitionsCount: number) {
|
||||
totals.warnings > 0
|
||||
? ` and ${totals.warnings} ${pluralize('warning', totals.warnings)}`
|
||||
: ''
|
||||
}.\n${ignored}`,
|
||||
),
|
||||
}.\n${ignored}`
|
||||
)
|
||||
);
|
||||
} else if (totals.warnings > 0) {
|
||||
process.stderr.write(
|
||||
green(`Woohoo! Your OpenAPI ${pluralize('definition is', definitionsCount)} valid. 🎉\n`),
|
||||
green(`Woohoo! Your OpenAPI ${pluralize('definition is', definitionsCount)} valid. 🎉\n`)
|
||||
);
|
||||
process.stderr.write(
|
||||
yellow(`You have ${totals.warnings} ${pluralize('warning', totals.warnings)}.\n${ignored}`),
|
||||
yellow(`You have ${totals.warnings} ${pluralize('warning', totals.warnings)}.\n${ignored}`)
|
||||
);
|
||||
} else {
|
||||
process.stderr.write(
|
||||
green(
|
||||
`Woohoo! Your OpenAPI ${pluralize(
|
||||
'definition is',
|
||||
definitionsCount,
|
||||
)} valid. 🎉\n${ignored}`,
|
||||
),
|
||||
`Woohoo! Your OpenAPI ${pluralize('definition is', definitionsCount)} valid. 🎉\n${ignored}`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (totals.errors > 0) {
|
||||
process.stderr.write(
|
||||
gray(`run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.\n`),
|
||||
gray(`run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.\n`)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -238,21 +241,21 @@ export function printConfigLintTotals(totals: Totals): void {
|
||||
totals.warnings > 0
|
||||
? ` and ${totals.warnings} ${pluralize('warning', totals.warnings)}`
|
||||
: ''
|
||||
}.\n`,
|
||||
),
|
||||
}.\n`
|
||||
)
|
||||
);
|
||||
} else if (totals.warnings > 0) {
|
||||
process.stderr.write(
|
||||
yellow(`You have ${totals.warnings} ${pluralize('warning', totals.warnings)}.\n`),
|
||||
yellow(`You have ${totals.warnings} ${pluralize('warning', totals.warnings)}.\n`)
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function getOutputFileName(
|
||||
entrypoint: string,
|
||||
entries: number,
|
||||
output?: string,
|
||||
ext?: BundleOutputFormat,
|
||||
ext?: BundleOutputFormat
|
||||
) {
|
||||
if (!output) {
|
||||
return { outputFile: 'stdout', ext: ext || 'yaml' };
|
||||
@@ -283,26 +286,26 @@ export function printUnusedWarnings(config: StyleguideConfig) {
|
||||
if (rules.length) {
|
||||
process.stderr.write(
|
||||
yellow(
|
||||
`[WARNING] Unused rules found in ${blue(config.configFile || '')}: ${rules.join(', ')}.\n`,
|
||||
),
|
||||
`[WARNING] Unused rules found in ${blue(config.configFile || '')}: ${rules.join(', ')}.\n`
|
||||
)
|
||||
);
|
||||
}
|
||||
if (preprocessors.length) {
|
||||
process.stderr.write(
|
||||
yellow(
|
||||
`[WARNING] Unused preprocessors found in ${blue(
|
||||
config.configFile || '',
|
||||
)}: ${preprocessors.join(', ')}.\n`,
|
||||
),
|
||||
config.configFile || ''
|
||||
)}: ${preprocessors.join(', ')}.\n`
|
||||
)
|
||||
);
|
||||
}
|
||||
if (decorators.length) {
|
||||
process.stderr.write(
|
||||
yellow(
|
||||
`[WARNING] Unused decorators found in ${blue(config.configFile || '')}: ${decorators.join(
|
||||
', ',
|
||||
)}.\n`,
|
||||
),
|
||||
', '
|
||||
)}.\n`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -312,7 +315,7 @@ export function printUnusedWarnings(config: StyleguideConfig) {
|
||||
}
|
||||
|
||||
export function exitWithError(message: string) {
|
||||
process.stderr.write(red(message)+ '\n\n');
|
||||
process.stderr.write(red(message) + '\n\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user