Skip to content

Commit

Permalink
feat(client): support regexp to select services to export
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Jan 29, 2024
1 parent e11a373 commit 69fbc94
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $ openapi --help
--useOptions Use options instead of arguments
--useUnionTypes Use union types instead of enums
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk (default: true)
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
Expand Down
8 changes: 7 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const params = program
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));

if (OpenAPI) {
let exportServices;
try {
exportServices = JSON.parse(params.exportServices) === true;
} catch (error) {
exportServices = params.exportServices;
}
OpenAPI.generate({
input: params.input,
output: params.output,
Expand All @@ -38,7 +44,7 @@ if (OpenAPI) {
useOptions: params.useOptions,
useUnionTypes: params.useUnionTypes,
exportCore: JSON.parse(params.exportCore) === true,
exportServices: JSON.parse(params.exportServices) === true,
exportServices,
exportModels: JSON.parse(params.exportModels) === true,
exportSchemas: JSON.parse(params.exportSchemas) === true,
indent: params.indent,
Expand Down
14 changes: 14 additions & 0 deletions bin/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ describe('bin', () => {
expect(result.stderr.toString()).toBe('');
});

it('it should support regexp in exportSchemas', async () => {
const result = crossSpawn.sync('node', [
'./bin/index.js',
'--input',
'./test/spec/v3.json',
'--output',
'./test/generated/bin',
'--exportServices',
'^(Simple|Types)',
]);
expect(result.stdout.toString()).toBe('');
expect(result.stderr.toString()).toBe('');
});

it('it should throw error without params', async () => {
const result = crossSpawn.sync('node', ['./bin/index.js']);
expect(result.stdout.toString()).toBe('');
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type Options = {
useOptions?: boolean;
useUnionTypes?: boolean;
exportCore?: boolean;
exportServices?: boolean;
exportServices?: boolean | string;
exportModels?: boolean;
exportSchemas?: boolean;
indent?: Indent;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/writeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const writeClient = async (
useOptions: boolean,
useUnionTypes: boolean,
exportCore: boolean,
exportServices: boolean,
exportServices: boolean | string,
exportModels: boolean,
exportSchemas: boolean,
indent: Indent,
Expand All @@ -60,6 +60,11 @@ export const writeClient = async (
throw new Error(`Output folder is not a subdirectory of the current working directory`);
}

if (typeof exportServices === 'string') {
const regexp = new RegExp(exportServices);
client.services = client.services.filter(service => regexp.test(service.name));
}

if (exportCore) {
await rmdir(outputPathCore);
await mkdir(outputPathCore);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClientIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const writeClientIndex = async (
outputPath: string,
useUnionTypes: boolean,
exportCore: boolean,
exportServices: boolean,
exportServices: boolean | string,
exportModels: boolean,
exportSchemas: boolean,
postfixServices: string,
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type Options = {
useOptions?: boolean;
useUnionTypes?: boolean;
exportCore?: boolean;
exportServices?: boolean;
exportServices?: boolean | string;
exportModels?: boolean;
exportSchemas?: boolean;
indent?: Indent | '4' | '2' | 'tab';
Expand Down

0 comments on commit 69fbc94

Please sign in to comment.