From aec9136b6dcc2f438beafb87f883069d45ac9afb Mon Sep 17 00:00:00 2001 From: Andrew Tatomyr Date: Thu, 2 Jan 2025 18:12:51 +0200 Subject: [PATCH] chore: fix output formatting --- .../miscellaneous/apply-per-api-decorators/snapshot.js | 8 ++++---- packages/cli/src/commands/bundle.ts | 9 +++++---- packages/cli/src/commands/lint.ts | 5 +++-- packages/cli/src/utils/miscellaneous.ts | 9 ++++++++- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/__tests__/miscellaneous/apply-per-api-decorators/snapshot.js b/__tests__/miscellaneous/apply-per-api-decorators/snapshot.js index 348b30e4ff..d89fa83f46 100644 --- a/__tests__/miscellaneous/apply-per-api-decorators/snapshot.js +++ b/__tests__/miscellaneous/apply-per-api-decorators/snapshot.js @@ -37,14 +37,14 @@ components: bundling nested/openapi/main.yaml... 📦 Created a bundle for nested/openapi/main.yaml at stdout ms. -bundling https:/raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml... -📦 Created a bundle for https:/raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml at stdout ms. +bundling https://raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml... +📦 Created a bundle for https://raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml at stdout ms. `; exports[`E2E miscellaneous lint a specific api (when the api is specified as an alias and it points to an external URL) 1`] = ` -validating https:/raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml... +validating https://raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml... [1] https://raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml:2:1 at #/info/contact Info object should contain \`contact\` field. @@ -58,7 +58,7 @@ Info object should contain \`contact\` field. Error was generated by the info-contact rule. -https:/raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml: validated in ms +https://raw.githubusercontent.com/Redocly/redocly-cli/refs/heads/main/__tests__/miscellaneous/apply-per-api-decorators/nested/openapi/main.yaml: validated in ms ❌ Validation failed with 1 error. run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file. diff --git a/packages/cli/src/commands/bundle.ts b/packages/cli/src/commands/bundle.ts index 2432429acb..5e9314d028 100644 --- a/packages/cli/src/commands/bundle.ts +++ b/packages/cli/src/commands/bundle.ts @@ -13,6 +13,7 @@ import { saveBundle, sortTopLevelKeysForOas, checkForDeprecatedOptions, + formatPath, } from '../utils/miscellaneous'; import type { OutputExtensions, Skips, Totals, VerifyConfigOptions } from '../types'; @@ -55,7 +56,7 @@ export async function handleBundle({ styleguide.skipPreprocessors(argv['skip-preprocessor']); styleguide.skipDecorators(argv['skip-decorator']); - process.stderr.write(gray(`bundling ${relative(process.cwd(), path)}...\n`)); + process.stderr.write(gray(`bundling ${formatPath(path)}...\n`)); const { bundle: result, @@ -118,20 +119,20 @@ export async function handleBundle({ if (fileTotals.errors > 0) { if (argv.force) { process.stderr.write( - `❓ Created a bundle for ${blue(relative(process.cwd(), path))} at ${blue( + `❓ Created a bundle for ${blue(formatPath(path))} at ${blue( outputFile || 'stdout' )} with errors ${green(elapsed)}.\n${yellow('Errors ignored because of --force')}.\n` ); } else { process.stderr.write( `❌ Errors encountered while bundling ${blue( - relative(process.cwd(), path) + formatPath(path) )}: bundle not created (use --force to ignore errors).\n` ); } } else { process.stderr.write( - `📦 Created a bundle for ${blue(relative(process.cwd(), path))} at ${blue( + `📦 Created a bundle for ${blue(formatPath(path))} at ${blue( outputFile || 'stdout' )} ${green(elapsed)}.\n` ); diff --git a/packages/cli/src/commands/lint.ts b/packages/cli/src/commands/lint.ts index fa33428a17..b0229c384c 100644 --- a/packages/cli/src/commands/lint.ts +++ b/packages/cli/src/commands/lint.ts @@ -13,6 +13,7 @@ import { pluralize } from '@redocly/openapi-core/lib/utils'; import { checkIfRulesetExist, exitWithError, + formatPath, getExecutionTime, getFallbackApisOrExit, handleError, @@ -75,7 +76,7 @@ export async function handleLint({ )} configuration by default.\n\n` ); } - process.stderr.write(gray(`validating ${relative(process.cwd(), path)}...\n`)); + process.stderr.write(gray(`validating ${formatPath(path)}...\n`)); const results = await lint({ ref: path, config: resolvedConfig, @@ -102,7 +103,7 @@ export async function handleLint({ } const elapsed = getExecutionTime(startedAt); - process.stderr.write(gray(`${relative(process.cwd(), path)}: validated in ${elapsed}\n\n`)); + process.stderr.write(gray(`${formatPath(path)}: validated in ${elapsed}\n\n`)); } catch (e) { handleError(e, path); } diff --git a/packages/cli/src/utils/miscellaneous.ts b/packages/cli/src/utils/miscellaneous.ts index 2d9a5fcf52..a551a55cad 100644 --- a/packages/cli/src/utils/miscellaneous.ts +++ b/packages/cli/src/utils/miscellaneous.ts @@ -57,7 +57,7 @@ export async function getFallbackApisOrExit( if (isNotEmptyArray(filteredInvalidEntrypoints)) { for (const { path } of filteredInvalidEntrypoints) { process.stderr.write( - yellow(`\n${relative(process.cwd(), path)} ${red(`does not exist or is invalid.\n\n`)}`) + yellow(`\n${formatPath(path)} ${red(`does not exist or is invalid.\n\n`)}`) ); } exitWithError('Please provide a valid path.'); @@ -702,3 +702,10 @@ export function notifyAboutIncompatibleConfigOptions( } } } + +export function formatPath(path: string) { + if (isAbsoluteUrl(path)) { + return path; + } + return relative(process.cwd(), path); +}