Skip to content

Commit

Permalink
feat(core): support multiple files input for oas converter
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 committed Jan 23, 2025
1 parent acd97b7 commit 324ecff
Showing 1 changed file with 64 additions and 38 deletions.
102 changes: 64 additions & 38 deletions apps/cli/src/command/convert.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { OpenAPIConverter } from '@api7/adc-converter-openapi';
import * as ADCSDK from '@api7/adc-sdk';
import OpenAPIParser from '@readme/openapi-parser';
import { Listr } from 'listr2';
import { cloneDeep } from 'lodash';
import { existsSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { OpenAPIV3 } from 'openapi-types';
Expand All @@ -11,74 +13,96 @@ import { TaskContext } from './diff.command';
import { BaseCommand } from './helper';

interface ConvertOptions {
file: string;
file: Array<string>;
output: string;
verbose: number;
}

type ConvertContext = TaskContext & {
oas?: OpenAPIV3.Document;
buffer?: Array<ADCSDK.Configuration>;
};

class BaseConvertCommand extends BaseCommand {
constructor(name: string) {
super(name);
this.option(
'-f, --file <openapi-file-path>',
'OpenAPI specification file path',
(filePath, files: Array<string> = []) => files.concat(filePath),
).option('-o, --output <output-path>', 'output file path', 'adc.yaml');
}
}

const OpenAPICommand = new BaseConvertCommand('openapi')
.description('Convert an OpenAPI specification to equivalent ADC configuration.\n\nLearn more at: https://docs.api7.ai/enterprise/reference/openapi-adc')
.description(
'Convert an OpenAPI specification to equivalent ADC configuration.\n\nLearn more at: https://docs.api7.ai/enterprise/reference/openapi-adc',
)
.summary('convert OpenAPI spec to ADC configuration')
.addExamples([
{
title: 'Convert OpenAPI specification in YAML format to ADC configuration and write to the default adc.yaml file',
title:
'Convert OpenAPI specification in YAML format to ADC configuration and write to the default adc.yaml file',
command: 'adc convert openapi -f openapi.yaml',
},
{
title: 'Convert OpenAPI specification in JSON format to ADC configuration and write to the specified file',
title:
'Convert OpenAPI specification in JSON format to ADC configuration and write to the specified file',
command: 'adc convert openapi -f openapi.json -o converted-adc.yaml',
}
},
{
title:
'Convert multiple OpenAPI specifications to single ADC configuration',
command: 'adc convert openapi -f openapi.yaml -f openapi.json',
},
])
.action(async () => {
const opts = OpenAPICommand.optsWithGlobals<ConvertOptions>();

const tasks = new Listr<
TaskContext & { oas?: OpenAPIV3.Document },
typeof SignaleRenderer
>(
const tasks = new Listr<ConvertContext, typeof SignaleRenderer>(
[
{
title: 'Load OpenAPI document',
task: async (ctx) => {
const filePath = opts.file;
...opts.file.map((filePath) => {
return {
title: `Convert OpenAPI document "${resolve(filePath)}"`,
task: async (ctx: ConvertContext) => {
// check existance
if (!existsSync(filePath)) {
const error = new Error(
`File "${resolve(filePath)}" does not exist`,
);
error.stack = '';
throw error;
}

// check existance
if (!existsSync(filePath)) {
const error = new Error(
`File "${resolve(filePath)}" does not exist`,
);
error.stack = '';
throw error;
}

try {
ctx.oas = (await OpenAPIParser.dereference(
filePath,
)) as OpenAPIV3.Document;
} catch (error) {
error.message = error.message.replace('\n', '');
error.stack = '';
throw error;
}
},
},
{
title: 'Convert OpenAPI document',
task: (ctx) => new OpenAPIConverter().toADC(ctx.oas),
},
try {
const oas = (await OpenAPIParser.dereference(
filePath,
)) as OpenAPIV3.Document;
const task = new OpenAPIConverter().toADC(oas);
task.add([
{
task: (subCtx) => {
if (!ctx.buffer) {
ctx.buffer = [cloneDeep(subCtx.local)];
} else {
ctx.buffer.push(cloneDeep(subCtx.local));
}
},
},
]);
return task;
} catch (error) {
error.message = error.message.replace('\n', '');
error.stack = '';
throw error;
}
},
};
}),
{
title: 'Write converted OpenAPI file',
task: (ctx, task) => {
ctx.local.services = ctx.buffer.flatMap((item) => item.services);
const yamlStr = stringify(ctx.local, {});
writeFileSync(opts.output, yamlStr);
task.title = `Converted OpenAPI file to "${resolve(
Expand All @@ -102,6 +126,8 @@ const OpenAPICommand = new BaseConvertCommand('openapi')
});

export const ConvertCommand = new BaseCommand('convert')
.description('Convert API definitions in other formats to equivalent ADC configuration.')
.description(
'Convert API definitions in other formats to equivalent ADC configuration.',
)
.summary('convert API definitions in other formats to ADC configuration')
.addCommand(OpenAPICommand);

0 comments on commit 324ecff

Please sign in to comment.