diff --git a/README.md b/README.md index 83f0ae233..63f268332 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ $ openapi --help --useOptions Use options instead of arguments --useUnionTypes Use union types instead of enums --exportCore Write core files to disk (default: true) - --exportServices Write services to disk (default: true) + --exportServices Write services to disk [true, false, regexp] (default: true) --exportModels Write models to disk (default: true) --exportSchemas Write schemas to disk (default: false) --indent Indentation options [4, 2, tab] (default: "4") diff --git a/bin/index.js b/bin/index.js index 32f2fecbc..edb7879f1 100755 --- a/bin/index.js +++ b/bin/index.js @@ -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, @@ -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, diff --git a/bin/index.spec.js b/bin/index.spec.js index 6030c07c8..e9dadabef 100755 --- a/bin/index.spec.js +++ b/bin/index.spec.js @@ -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(''); diff --git a/src/index.ts b/src/index.ts index d75c30177..3763ed67d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ export type Options = { useOptions?: boolean; useUnionTypes?: boolean; exportCore?: boolean; - exportServices?: boolean; + exportServices?: boolean | string; exportModels?: boolean; exportSchemas?: boolean; indent?: Indent; diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index cea2f3d88..2f55b0e05 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -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, @@ -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); diff --git a/src/utils/writeClientIndex.ts b/src/utils/writeClientIndex.ts index 5044294d5..72723c0f1 100644 --- a/src/utils/writeClientIndex.ts +++ b/src/utils/writeClientIndex.ts @@ -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, diff --git a/types/index.d.ts b/types/index.d.ts index e2b5247e0..f347d74bc 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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';