diff --git a/.changeset/tiny-plums-change.md b/.changeset/tiny-plums-change.md new file mode 100644 index 0000000000..ce813c16e3 --- /dev/null +++ b/.changeset/tiny-plums-change.md @@ -0,0 +1,5 @@ +--- +"@redocly/cli": patch +--- + +Fixed hard crash that happened when no API was provided either via the command argument or in the config. diff --git a/packages/cli/src/__tests__/commands/lint.test.ts b/packages/cli/src/__tests__/commands/lint.test.ts index 1106f38f0a..3a2c03e39a 100644 --- a/packages/cli/src/__tests__/commands/lint.test.ts +++ b/packages/cli/src/__tests__/commands/lint.test.ts @@ -124,12 +124,6 @@ describe('handleLint', () => { await commandWrapper(handleLint)(argvMock); expect(checkIfRulesetExist).toHaveBeenCalledTimes(1); }); - - it('should fail if apis not provided', async () => { - await commandWrapper(handleLint)({ ...argvMock, apis: [] }); - expect(getFallbackApisOrExit).toHaveBeenCalledTimes(1); - expect(exitWithError).toHaveBeenCalledWith('No APIs were provided.'); - }); }); describe('loop through entrypoints and lint stage', () => { diff --git a/packages/cli/src/__tests__/utils.test.ts b/packages/cli/src/__tests__/utils.test.ts index 06b4385999..1a036aa0fa 100644 --- a/packages/cli/src/__tests__/utils.test.ts +++ b/packages/cli/src/__tests__/utils.test.ts @@ -142,6 +142,18 @@ describe('getFallbackApisOrExit', async () => { } }); + it('should exit with error when no APIs are provided and config has no apis', async () => { + const apisConfig = await openapiCore.createConfig({ apis: {} }); + expect.assertions(1); + try { + await getFallbackApisOrExit([], apisConfig); + } catch (e) { + expect(e.message).toEqual( + 'No APIs were provided. Specify an API via the command argument or define one in your config.' + ); + } + }); + it('should error if file from config do not exist', async () => { vi.spyOn(errorHandling, 'exitWithError'); vi.mocked(fs.existsSync).mockImplementationOnce(() => false); diff --git a/packages/cli/src/commands/lint.ts b/packages/cli/src/commands/lint.ts index d0dc4a5c63..0f8250faba 100644 --- a/packages/cli/src/commands/lint.ts +++ b/packages/cli/src/commands/lint.ts @@ -15,7 +15,7 @@ import { performance } from 'perf_hooks'; import type { Arguments } from 'yargs'; import type { CommandArgv, Totals, VerifyConfigOptions } from '../types.js'; -import { AbortFlowError, exitWithError } from '../utils/error.js'; +import { AbortFlowError } from '../utils/error.js'; import { getCommandNameFromArgs } from '../utils/get-command-name-from-args.js'; import { checkIfRulesetExist, @@ -47,10 +47,6 @@ export async function handleLint({ }: CommandArgs) { const apis = await getFallbackApisOrExit(argv.apis, config); - if (!apis.length) { - exitWithError('No APIs were provided.'); - } - const totals: Totals = { errors: 0, warnings: 0, ignored: 0 }; let totalIgnored = 0; diff --git a/packages/cli/src/commands/scorecard-classic/index.ts b/packages/cli/src/commands/scorecard-classic/index.ts index 742d371583..8a6499ce8e 100644 --- a/packages/cli/src/commands/scorecard-classic/index.ts +++ b/packages/cli/src/commands/scorecard-classic/index.ts @@ -34,9 +34,6 @@ export async function handleScorecardClassic({ }: CommandArgs) { const startedAt = performance.now(); const apis = await getFallbackApisOrExit(argv.api ? [argv.api] : [], config); - if (!apis.length) { - exitWithError('No APIs were provided.'); - } const projectUrl = argv['project-url'] || diff --git a/packages/cli/src/utils/miscellaneous.ts b/packages/cli/src/utils/miscellaneous.ts index 55cc135dfd..1061bd02fc 100644 --- a/packages/cli/src/utils/miscellaneous.ts +++ b/packages/cli/src/utils/miscellaneous.ts @@ -57,6 +57,11 @@ export async function getFallbackApisOrExit( } exitWithError('Please provide a valid path.'); } + if (res.length === 0) { + exitWithError( + 'No APIs were provided. Specify an API via the command argument or define one in your config.' + ); + } return res; }