@redocly/cli
Version:
[@Redocly](https://redocly.com) CLI is your all-in-one OpenAPI utility. It builds, manages, improves, and quality-checks your OpenAPI descriptions, all of which comes in handy for various phases of the API Lifecycle. Create your own rulesets to make API g
90 lines โข 4.27 kB
JavaScript
import { performance } from 'perf_hooks';
import { blue, gray, green, yellow } from 'colorette';
import { writeFileSync } from 'fs';
import { formatProblems, getTotals, bundle, logger, } from '@redocly/openapi-core';
import { dumpBundle, getExecutionTime, getFallbackApisOrExit, getOutputFileName, handleError, printUnusedWarnings, saveBundle, sortTopLevelKeysForOas, formatPath, } from '../utils/miscellaneous.js';
import { AbortFlowError } from '../utils/error.js';
export async function handleBundle({ argv, config, version, collectSpecData, }) {
const apis = await getFallbackApisOrExit(argv.apis, config);
const totals = { errors: 0, warnings: 0, ignored: 0 };
for (const { path, alias, output } of apis) {
try {
const startedAt = performance.now();
const aliasConfig = config.forAlias(alias);
aliasConfig.skipPreprocessors(argv['skip-preprocessor']);
aliasConfig.skipDecorators(argv['skip-decorator']);
if (alias === undefined) {
logger.info(gray(`bundling ${formatPath(path)}...\n`));
}
else {
logger.info(gray(`bundling ${formatPath(path)} using configuration for api '${alias}'...\n`));
}
const { bundle: result, problems, ...meta } = await bundle({
ref: path,
config: aliasConfig,
dereference: argv.dereferenced,
removeUnusedComponents: argv['remove-unused-components'],
keepUrlRefs: argv['keep-url-references'],
collectSpecData,
});
const fileTotals = getTotals(problems);
const { outputFile, ext } = getOutputFileName({
entrypoint: path,
output,
argvOutput: argv.output,
ext: argv.ext,
entries: argv?.apis?.length || 0,
});
if (fileTotals.errors === 0 || argv.force) {
if (!outputFile) {
const bundled = dumpBundle(sortTopLevelKeysForOas(result.parsed), argv.ext || 'yaml', argv.dereferenced);
logger.output(bundled);
}
else {
const bundled = dumpBundle(sortTopLevelKeysForOas(result.parsed), ext, argv.dereferenced);
saveBundle(outputFile, bundled);
}
}
totals.errors += fileTotals.errors;
totals.warnings += fileTotals.warnings;
totals.ignored += fileTotals.ignored;
formatProblems(problems, {
format: 'codeframe',
totals: fileTotals,
version,
});
if (argv.metafile) {
if (apis.length > 1) {
logger.warn(`[WARNING] "--metafile" cannot be used with multiple apis. Skipping...`);
}
{
writeFileSync(argv.metafile, JSON.stringify(meta), 'utf-8');
}
}
const elapsed = getExecutionTime(startedAt);
if (fileTotals.errors > 0) {
if (argv.force) {
logger.info(`โ Created a bundle for ${blue(formatPath(path))} at ${blue(outputFile || 'stdout')} with errors ${green(elapsed)}.\n${yellow('Errors ignored because of --force')}.\n`);
}
else {
logger.info(`โ Errors encountered while bundling ${blue(formatPath(path))}: bundle not created (use --force to ignore errors).\n`);
}
}
else {
logger.info(`๐ฆ Created a bundle for ${blue(formatPath(path))} at ${blue(outputFile || 'stdout')} ${green(elapsed)}.\n`);
}
const removedCount = meta.visitorsData?.['remove-unused-components']?.removedCount;
if (removedCount) {
logger.info(gray(`๐งน Removed ${removedCount} unused components.\n`));
}
}
catch (e) {
handleError(e, path);
}
}
printUnusedWarnings(config);
if (!(totals.errors === 0 || argv.force)) {
throw new AbortFlowError('Bundle failed.');
}
}
//# sourceMappingURL=bundle.js.map