Skip to content

Commit b333396

Browse files
committed
fix: expand glob before checking API count in join
1 parent b151753 commit b333396

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

.changeset/long-donkeys-bathe.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 an issue where join threw an error when glob was provided.

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { yellow } from 'colorette';
22
import { detectSpec } from '@redocly/openapi-core';
33
import { handleJoin } from '../../commands/join';
4-
import { exitWithError, writeToFileByExtension } from '../../utils/miscellaneous';
4+
import {
5+
exitWithError,
6+
getFallbackApisOrExit,
7+
writeToFileByExtension,
8+
} from '../../utils/miscellaneous';
59
import { loadConfig } from '../../__mocks__/@redocly/openapi-core';
610
import { ConfigFixture } from '../fixtures/config';
711

@@ -15,7 +19,35 @@ describe('handleJoin', () => {
1519

1620
it('should call exitWithError because only one entrypoint', async () => {
1721
await handleJoin({ argv: { apis: ['first.yaml'] }, config: {} as any, version: 'cli-version' });
18-
expect(exitWithError).toHaveBeenCalledWith(`At least 2 apis should be provided.`);
22+
expect(exitWithError).toHaveBeenCalledWith(`At least 2 APIs should be provided.`);
23+
});
24+
25+
it('should call exitWithError if glob expands to less than 2 APIs', async () => {
26+
(getFallbackApisOrExit as jest.Mock).mockResolvedValueOnce([{ path: 'first.yaml' }]);
27+
28+
await handleJoin({
29+
argv: { apis: ['*.yaml'] },
30+
config: {} as any,
31+
version: 'cli-version',
32+
});
33+
34+
expect(exitWithError).toHaveBeenCalledWith(`At least 2 APIs should be provided.`);
35+
});
36+
37+
it('should proceed if glob expands to 2 or more APIs', async () => {
38+
(detectSpec as jest.Mock).mockReturnValue('oas3_1');
39+
(getFallbackApisOrExit as jest.Mock).mockResolvedValueOnce([
40+
{ path: 'first.yaml' },
41+
{ path: 'second.yaml' },
42+
]);
43+
44+
await handleJoin({
45+
argv: { apis: ['*.yaml'] },
46+
config: ConfigFixture as any,
47+
version: 'cli-version',
48+
});
49+
50+
expect(exitWithError).not.toHaveBeenCalled();
1951
});
2052

2153
it('should call exitWithError because passed all 3 options for tags', async () => {
@@ -52,6 +84,7 @@ describe('handleJoin', () => {
5284
});
5385

5486
it('should call exitWithError because Only OpenAPI 3.0 and OpenAPI 3.1 are supported', async () => {
87+
(detectSpec as jest.Mock).mockReturnValueOnce('oas2_0');
5588
await handleJoin({
5689
argv: {
5790
apis: ['first.yaml', 'second.yaml'],

packages/cli/src/commands/join.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,12 @@ export async function handleJoin({
6464
}: CommandArgs<JoinOptions>) {
6565
const startedAt = performance.now();
6666

67-
if (argv.apis.length < 2) {
68-
return exitWithError(`At least 2 apis should be provided.`);
69-
}
70-
71-
const fileExtension = getAndValidateFileExtension(argv.output || argv.apis[0]);
72-
7367
const {
7468
'prefix-components-with-info-prop': prefixComponentsWithInfoProp,
7569
'prefix-tags-with-filename': prefixTagsWithFilename,
7670
'prefix-tags-with-info-prop': prefixTagsWithInfoProp,
7771
'without-x-tag-groups': withoutXTagGroups,
78-
output: specFilename = `openapi.${fileExtension}`,
72+
output,
7973
} = argv;
8074

8175
const usedTagsOptions = [
@@ -91,6 +85,13 @@ export async function handleJoin({
9185
}
9286

9387
const apis = await getFallbackApisOrExit(argv.apis, config);
88+
if (apis.length < 2) {
89+
return exitWithError(`At least 2 APIs should be provided.`);
90+
}
91+
92+
const fileExtension = getAndValidateFileExtension(output || apis[0].path);
93+
const specFilename = output || `openapi.${fileExtension}`;
94+
9495
const externalRefResolver = new BaseResolver(config.resolve);
9596
const documents = await Promise.all(
9697
apis.map(

0 commit comments

Comments
 (0)