Skip to content

Commit 463e5ef

Browse files
committed
chore: changes after review
1 parent eb76f7a commit 463e5ef

File tree

7 files changed

+50
-43
lines changed

7 files changed

+50
-43
lines changed

packages/cli/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { handleTranslations } from './commands/translations';
2525
import { handleEject } from './commands/eject';
2626
import { PRODUCT_PLANS } from './commands/preview-project/constants';
2727
import { commonPushHandler } from './commands/push';
28-
import { handleRun, handleGenerate } from '@redocly/respect-core';
28+
import { handleRun } from '@redocly/respect-core';
2929

3030
import type { Arguments } from 'yargs';
3131
import type { OutputFormat, RuleSeverity } from '@redocly/openapi-core';
@@ -961,8 +961,9 @@ yargs
961961
},
962962
});
963963
},
964-
(argv) => {
964+
async (argv) => {
965965
process.env.REDOCLY_CLI_COMMAND = 'generate-arazzo';
966+
const { handleGenerate } = await import('@redocly/respect-core');
966967
commandWrapper(
967968
handleGenerate as (wrapperArgs: CommandArgs<CommandOptions>) => Promise<unknown>
968969
)(argv);

packages/respect-core/src/handlers/generate.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@ import { writeFileSync } from 'fs';
33
import { stringifyYaml } from '../utils/yaml';
44
import { generateTestConfig } from '../modules/test-config-generator';
55
import { DefaultLogger } from '../utils/logger/logger';
6-
import { type GenerateArazzoFileArgv } from '../types';
7-
import { type CommandArgs } from './run';
6+
import { exitWithError } from '../utils/exit-with-error';
7+
import { type CommandArgs } from '../types';
8+
9+
export type GenerateArazzoFileOptions = {
10+
descriptionPath: string;
11+
'output-file'?: string;
12+
extended?: boolean;
13+
};
814

915
const logger = DefaultLogger.getInstance();
1016

11-
export async function handleGenerate({ argv }: CommandArgs<GenerateArazzoFileArgv>) {
12-
logger.log(gray('\n Generating test configuration... \n'));
17+
export async function handleGenerate({ argv }: CommandArgs<GenerateArazzoFileOptions>) {
18+
try {
19+
logger.log(gray('\n Generating test configuration... \n'));
1320

14-
const generatedConfig = await generateTestConfig(argv);
15-
const content = stringifyYaml(generatedConfig);
21+
const generatedConfig = await generateTestConfig(argv);
22+
const content = stringifyYaml(generatedConfig);
1623

17-
const fileName = argv['output-file'] || 'auto-generated.arazzo.yaml';
18-
writeFileSync(fileName, content);
24+
const fileName = argv['output-file'] || 'auto-generated.arazzo.yaml';
25+
writeFileSync(fileName, content);
1926

20-
logger.log('\n' + blue(`Config ${yellow(fileName)} successfully generated.`) + '\n');
27+
logger.log('\n' + blue(`Config ${yellow(fileName)} successfully generated.`) + '\n');
28+
} catch (_err) {
29+
exitWithError('\n' + '❌ Auto config generation failed.');
30+
}
2131
}

packages/respect-core/src/handlers/run.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { bgRed, red } from 'colorette';
1+
import { red } from 'colorette';
2+
import { type CollectFn } from '@redocly/openapi-core/src/utils';
23
import { runTestFile } from '../modules/flow-runner';
34
import {
45
displayErrors,
@@ -7,17 +8,8 @@ import {
78
calculateTotals,
89
} from '../modules/cli-output';
910
import { DefaultLogger } from '../utils/logger/logger';
10-
11-
import type { Config } from '@redocly/openapi-core';
12-
import type { CollectFn } from '@redocly/openapi-core/src/utils';
13-
import type { RunArgv } from '../types';
14-
15-
export type CommandArgs<T> = {
16-
argv: T;
17-
config: Config;
18-
version: string;
19-
collectSpecData?: CollectFn;
20-
};
11+
import { type CommandArgs, type RunArgv } from '../types';
12+
import { exitWithError } from '../utils/exit-with-error';
2113

2214
export type RespectOptions = {
2315
files: string[];
@@ -112,9 +104,3 @@ async function runFile(
112104

113105
return { hasProblems, file: argv.file, workflows, argv };
114106
}
115-
116-
const exitWithError = (message: string) => {
117-
logger.error(bgRed(message));
118-
logger.printNewLine();
119-
throw new Error(message);
120-
};

packages/respect-core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export { handleGenerate, handleRun } from './handlers/index';
2+
export type { GenerateArazzoFileOptions } from './handlers/generate';
3+
export type { RespectOptions } from './handlers/run';

packages/respect-core/src/modules/test-config-generator/generate-test-config.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ import * as path from 'path';
22
import { sortMethods } from '../../utils/sort';
33
import { bundleOpenApi } from '../description-parser';
44

5-
import type {
6-
OperationMethod,
7-
TestDescription,
8-
GenerateArazzoFileArgv,
9-
Workflow,
10-
Step,
11-
} from '../../types';
5+
import type { OperationMethod, TestDescription, Workflow, Step } from '../../types';
6+
import type { GenerateArazzoFileOptions } from '../../handlers/generate';
127

138
type WorkflowsFromDescriptionInput = {
149
descriptionPaths: any;
@@ -104,7 +99,7 @@ export async function generateTestConfig({
10499
descriptionPath,
105100
'output-file': outputFile,
106101
extended,
107-
}: GenerateArazzoFileArgv) {
102+
}: GenerateArazzoFileOptions) {
108103
const { paths: pathsObject, info } = (await bundleOpenApi(descriptionPath, '')) || {};
109104
const sourceDescriptionName = resolveDescriptionNameFromPath(descriptionPath);
110105
const resolvedDescriptionPath = outputFile

packages/respect-core/src/types.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import type { OperationDetails } from './modules/description-parser';
1919
import type { RuleSeverity } from '@redocly/openapi-core/lib/config/types';
2020
import type { ApiFetcher } from './utils/api-fetcher';
2121
import type { RespectOptions } from './handlers/run';
22+
import type { Config } from '@redocly/openapi-core';
23+
import type { CollectFn } from '@redocly/openapi-core/src/utils';
2224

2325
export type OperationMethod = FromSchema<typeof operationMethod>;
2426
export type ResponseContext = {
@@ -82,6 +84,13 @@ export type RunArgv = Omit<RespectOptions, 'files'> & {
8284
severity?: string | string[];
8385
};
8486

87+
export type CommandArgs<T> = {
88+
argv: T;
89+
config: Config;
90+
version: string;
91+
collectSpecData?: CollectFn;
92+
};
93+
8594
export interface RequestContext {
8695
body: any;
8796
header: Record<string, string | number | boolean>;
@@ -223,12 +232,6 @@ export type Check = {
223232
additionalMessage?: string;
224233
};
225234

226-
export type GenerateArazzoFileArgv = {
227-
descriptionPath: string;
228-
'output-file'?: string;
229-
extended?: boolean;
230-
};
231-
232235
export interface ResultsOfTests {
233236
passed: number;
234237
failed: number;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { bgRed } from 'colorette';
2+
import { DefaultLogger } from '../utils/logger/logger';
3+
4+
const logger = DefaultLogger.getInstance();
5+
6+
export const exitWithError = (message: string) => {
7+
logger.error(bgRed(message));
8+
logger.printNewLine();
9+
throw new Error(message);
10+
};

0 commit comments

Comments
 (0)