Skip to content

Commit d274ac9

Browse files
committed
chore: remove duplications
1 parent 5f98a93 commit d274ac9

File tree

8 files changed

+62
-53
lines changed

8 files changed

+62
-53
lines changed

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
detectSpec,
55
getTotals,
66
loadConfig,
7-
BaseResolver,
87
type SpecVersion,
98
type Document,
109
} from '@redocly/openapi-core';
@@ -35,20 +34,35 @@ describe('handleJoin', () => {
3534
vi.mock('colorette');
3635
vi.mocked(yellow).mockImplementation((text) => text as string);
3736

38-
vi.mock('@redocly/openapi-core');
37+
vi.mock('@redocly/openapi-core', async () => {
38+
const actual = await vi.importActual<typeof import('@redocly/openapi-core')>(
39+
'@redocly/openapi-core'
40+
);
41+
class MockedBaseResolver extends actual.BaseResolver {
42+
resolveDocument = vi
43+
.fn()
44+
.mockImplementationOnce(() =>
45+
Promise.resolve({ source: { absoluteRef: 'ref' }, parsed: firstDocument } as Document)
46+
)
47+
.mockImplementationOnce(() =>
48+
Promise.resolve({ source: { absoluteRef: 'ref' }, parsed: secondDocument } as Document)
49+
)
50+
.mockImplementationOnce(() =>
51+
Promise.resolve({ source: { absoluteRef: 'ref' }, parsed: thirdDocument } as Document)
52+
);
53+
}
54+
return {
55+
...actual,
56+
bundleDocument: vi.fn(),
57+
detectSpec: vi.fn(),
58+
getTotals: vi.fn(),
59+
loadConfig: vi.fn(),
60+
BaseResolver: MockedBaseResolver,
61+
};
62+
});
3963
vi.mocked(bundleDocument).mockResolvedValue({ problems: [] } as any);
4064
vi.mocked(getTotals).mockReturnValue({ errors: 0, warnings: 0, ignored: 0 });
4165
vi.mocked(loadConfig).mockResolvedValue(configFixture);
42-
vi.mocked(BaseResolver.prototype.resolveDocument)
43-
.mockImplementationOnce(() =>
44-
Promise.resolve({ source: { absoluteRef: 'ref' }, parsed: firstDocument } as Document)
45-
)
46-
.mockImplementationOnce(() =>
47-
Promise.resolve({ source: { absoluteRef: 'ref' }, parsed: secondDocument } as Document)
48-
)
49-
.mockImplementationOnce(() =>
50-
Promise.resolve({ source: { absoluteRef: 'ref' }, parsed: thirdDocument } as Document)
51-
);
5266
});
5367

5468
it('should call exitWithError because only one entrypoint', async () => {

packages/cli/src/commands/join.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
isRef,
1212
dequal,
1313
logger,
14+
isString,
15+
isPlainObject,
16+
keysOf,
1417
} from '@redocly/openapi-core';
1518
import {
1619
getFallbackApisOrExit,
@@ -20,7 +23,6 @@ import {
2023
writeToFileByExtension,
2124
} from '../utils/miscellaneous.js';
2225
import { exitWithError } from '../utils/error.js';
23-
import { isObject, isString, keysOf } from '../utils/js-utils.js';
2426
import { COMPONENTS, OPENAPI3_METHOD } from './split/types.js';
2527
import { crawl, startsWithComponents } from './split/index.js';
2628

@@ -769,10 +771,10 @@ function getInfoPrefix(info: any, prefixArg: string | undefined, type: string) {
769771

770772
function replace$Refs(obj: unknown, componentsPrefix: string) {
771773
crawl(obj, (node: Record<string, unknown>) => {
772-
if (node.$ref && typeof node.$ref === 'string' && startsWithComponents(node.$ref)) {
774+
if (isRef(node) && startsWithComponents(node.$ref)) {
773775
const name = path.basename(node.$ref);
774776
node.$ref = node.$ref.replace(name, componentsPrefix + '_' + name);
775-
} else if (isObject(node.discriminator) && isObject(node.discriminator.mapping)) {
777+
} else if (isPlainObject(node.discriminator) && isPlainObject(node.discriminator.mapping)) {
776778
const { mapping } = node.discriminator;
777779
for (const name of Object.keys(mapping)) {
778780
const mappingPointer = mapping[name];

packages/cli/src/commands/split/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { red, blue, green } from 'colorette';
22
import * as fs from 'node:fs';
3-
import { parseYaml, slash, isRef, isTruthy, dequal, logger } from '@redocly/openapi-core';
3+
import {
4+
parseYaml,
5+
slash,
6+
isRef,
7+
isTruthy,
8+
dequal,
9+
logger,
10+
isEmptyObject,
11+
isPlainObject,
12+
} from '@redocly/openapi-core';
413
import * as path from 'node:path';
514
import { performance } from 'perf_hooks';
615
import {
@@ -12,7 +21,6 @@ import {
1221
writeToFileByExtension,
1322
getAndValidateFileExtension,
1423
} from '../../utils/miscellaneous.js';
15-
import { isObject, isEmptyObject } from '../../utils/js-utils.js';
1624
import { exitWithError } from '../../utils/error.js';
1725
import {
1826
OPENAPI3_COMPONENT,
@@ -148,19 +156,23 @@ function traverseDirectoryDeepCallback(
148156
}
149157

150158
export function crawl(object: unknown, visitor: (node: Record<string, unknown>) => void) {
151-
if (!isObject(object)) return;
152-
153-
visitor(object);
154-
for (const key of Object.keys(object)) {
155-
crawl(object[key], visitor);
159+
if (isPlainObject(object)) {
160+
visitor(object);
161+
for (const key of Object.keys(object)) {
162+
crawl(object[key], visitor);
163+
}
164+
} else if (Array.isArray(object)) {
165+
for (const item of object) {
166+
crawl(item, visitor);
167+
}
156168
}
157169
}
158170

159171
function replace$Refs(obj: unknown, relativeFrom: string, componentFiles = {} as ComponentsFiles) {
160172
crawl(obj, (node: Record<string, unknown>) => {
161-
if (node.$ref && typeof node.$ref === 'string' && startsWithComponents(node.$ref)) {
173+
if (isRef(node) && startsWithComponents(node.$ref)) {
162174
replace(node as RefObject, '$ref');
163-
} else if (isObject(node.discriminator) && isObject(node.discriminator.mapping)) {
175+
} else if (isPlainObject(node.discriminator) && isPlainObject(node.discriminator.mapping)) {
164176
const { mapping } = node.discriminator;
165177
for (const name of Object.keys(mapping)) {
166178
const mappingPointer = mapping[name];

packages/cli/src/reunite/commands/push-status.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as colors from 'colorette';
22
import { logger } from '@redocly/openapi-core';
3-
import { printExecutionTime } from '../../utils/miscellaneous.js';
3+
import { printExecutionTime, capitalize } from '../../utils/miscellaneous.js';
44
import { Spinner } from '../../utils/spinner.js';
55
import { DeploymentError } from '../utils.js';
66
import { ReuniteApi, getApiKeys, getDomain } from '../api/index.js';
7-
import { capitalize } from '../../utils/js-utils.js';
87
import { handleReuniteError, retryUntilConditionMet } from './utils.js';
98

109
import type { OutputFormat } from '@redocly/openapi-core';

packages/cli/src/utils/js-utils.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/cli/src/utils/miscellaneous.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,10 @@ export function formatPath(path: string) {
556556
}
557557
return relative(process.cwd(), path);
558558
}
559+
560+
export function capitalize(s: string) {
561+
if (s?.length > 0) {
562+
return s[0].toUpperCase() + s.slice(1);
563+
}
564+
return s;
565+
}

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ export {
22
type BundleOutputFormat,
33
type CollectFn,
44
type Exact,
5+
keysOf,
56
readFileFromUrl,
67
slash,
78
doesYamlFileExist,
89
isTruthy,
910
pause,
1011
isPlainObject,
12+
isString,
1113
dequal,
1214
pluralize,
1315
isEmptyObject,

packages/core/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ export function slash(path: string): string {
168168
return path.replace(/\\/g, '/');
169169
}
170170

171-
// TODO: use it everywhere
172171
export function isString(value: unknown): value is string {
173172
return typeof value === 'string';
174173
}

0 commit comments

Comments
 (0)