Skip to content

Commit 519d4f5

Browse files
fix: hard crash in commands when no API provided (#2813)
1 parent 68e6c58 commit 519d4f5

6 files changed

Lines changed: 23 additions & 14 deletions

File tree

.changeset/tiny-plums-change.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/cli": patch
3+
---
4+
5+
Fixed hard crash that happened when no API was provided either via the command argument or in the config.

packages/cli/src/__tests__/commands/lint.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ describe('handleLint', () => {
124124
await commandWrapper(handleLint)(argvMock);
125125
expect(checkIfRulesetExist).toHaveBeenCalledTimes(1);
126126
});
127-
128-
it('should fail if apis not provided', async () => {
129-
await commandWrapper(handleLint)({ ...argvMock, apis: [] });
130-
expect(getFallbackApisOrExit).toHaveBeenCalledTimes(1);
131-
expect(exitWithError).toHaveBeenCalledWith('No APIs were provided.');
132-
});
133127
});
134128

135129
describe('loop through entrypoints and lint stage', () => {

packages/cli/src/__tests__/utils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ describe('getFallbackApisOrExit', async () => {
142142
}
143143
});
144144

145+
it('should exit with error when no APIs are provided and config has no apis', async () => {
146+
const apisConfig = await openapiCore.createConfig({ apis: {} });
147+
expect.assertions(1);
148+
try {
149+
await getFallbackApisOrExit([], apisConfig);
150+
} catch (e) {
151+
expect(e.message).toEqual(
152+
'No APIs were provided. Specify an API via the command argument or define one in your config.'
153+
);
154+
}
155+
});
156+
145157
it('should error if file from config do not exist', async () => {
146158
vi.spyOn(errorHandling, 'exitWithError');
147159
vi.mocked(fs.existsSync).mockImplementationOnce(() => false);

packages/cli/src/commands/lint.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { performance } from 'perf_hooks';
1515
import type { Arguments } from 'yargs';
1616

1717
import type { CommandArgv, Totals, VerifyConfigOptions } from '../types.js';
18-
import { AbortFlowError, exitWithError } from '../utils/error.js';
18+
import { AbortFlowError } from '../utils/error.js';
1919
import { getCommandNameFromArgs } from '../utils/get-command-name-from-args.js';
2020
import {
2121
checkIfRulesetExist,
@@ -47,10 +47,6 @@ export async function handleLint({
4747
}: CommandArgs<LintArgv>) {
4848
const apis = await getFallbackApisOrExit(argv.apis, config);
4949

50-
if (!apis.length) {
51-
exitWithError('No APIs were provided.');
52-
}
53-
5450
const totals: Totals = { errors: 0, warnings: 0, ignored: 0 };
5551
let totalIgnored = 0;
5652

packages/cli/src/commands/scorecard-classic/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ export async function handleScorecardClassic({
3434
}: CommandArgs<ScorecardClassicArgv>) {
3535
const startedAt = performance.now();
3636
const apis = await getFallbackApisOrExit(argv.api ? [argv.api] : [], config);
37-
if (!apis.length) {
38-
exitWithError('No APIs were provided.');
39-
}
4037

4138
const projectUrl =
4239
argv['project-url'] ||

packages/cli/src/utils/miscellaneous.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export async function getFallbackApisOrExit(
5757
}
5858
exitWithError('Please provide a valid path.');
5959
}
60+
if (res.length === 0) {
61+
exitWithError(
62+
'No APIs were provided. Specify an API via the command argument or define one in your config.'
63+
);
64+
}
6065
return res;
6166
}
6267

0 commit comments

Comments
 (0)