diff --git a/README.md b/README.md index 63f268332..b0d3ad873 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ $ openapi --help --useUnionTypes Use union types instead of enums --exportCore Write core files to disk (default: true) --exportServices Write services to disk [true, false, regexp] (default: true) - --exportModels Write models to disk (default: true) + --exportModels Write models to disk [true, false, regexp] (default: true) --exportSchemas Write schemas to disk (default: false) --indent Indentation options [4, 2, tab] (default: "4") --postfixServices Service name postfix (default: "Service") diff --git a/bin/index.js b/bin/index.js index edb7879f1..36664e351 100755 --- a/bin/index.js +++ b/bin/index.js @@ -29,13 +29,15 @@ const params = program const OpenAPI = require(path.resolve(__dirname, '../dist/index.js')); -if (OpenAPI) { - let exportServices; +const parseBooleanOrString = value => { try { - exportServices = JSON.parse(params.exportServices) === true; + return JSON.parse(value) === true; } catch (error) { - exportServices = params.exportServices; + return value; } +}; + +if (OpenAPI) { OpenAPI.generate({ input: params.input, output: params.output, @@ -44,8 +46,8 @@ if (OpenAPI) { useOptions: params.useOptions, useUnionTypes: params.useUnionTypes, exportCore: JSON.parse(params.exportCore) === true, - exportServices, - exportModels: JSON.parse(params.exportModels) === true, + exportServices: parseBooleanOrString(params.exportServices), + exportModels: parseBooleanOrString(params.exportModels), exportSchemas: JSON.parse(params.exportSchemas) === true, indent: params.indent, postfixServices: params.postfixServices, diff --git a/bin/index.spec.js b/bin/index.spec.js index e9dadabef..ff1cff75a 100755 --- a/bin/index.spec.js +++ b/bin/index.spec.js @@ -43,7 +43,7 @@ describe('bin', () => { expect(result.stderr.toString()).toBe(''); }); - it('it should support regexp in exportSchemas', async () => { + it('it should support regexp params', async () => { const result = crossSpawn.sync('node', [ './bin/index.js', '--input', @@ -52,6 +52,8 @@ describe('bin', () => { './test/generated/bin', '--exportServices', '^(Simple|Types)', + '--exportModels', + '^(Simple|Types)', ]); expect(result.stdout.toString()).toBe(''); expect(result.stderr.toString()).toBe(''); diff --git a/src/index.ts b/src/index.ts index 3763ed67d..749a2cfb2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,7 @@ export type Options = { useUnionTypes?: boolean; exportCore?: boolean; exportServices?: boolean | string; - exportModels?: boolean; + exportModels?: boolean | string; exportSchemas?: boolean; indent?: Indent; postfixServices?: string; diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index 2f55b0e05..6cc491ecd 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -42,7 +42,7 @@ export const writeClient = async ( useUnionTypes: boolean, exportCore: boolean, exportServices: boolean | string, - exportModels: boolean, + exportModels: boolean | string, exportSchemas: boolean, indent: Indent, postfixServices: string, @@ -65,6 +65,11 @@ export const writeClient = async ( client.services = client.services.filter(service => regexp.test(service.name)); } + if (typeof exportModels === 'string') { + const regexp = new RegExp(exportModels); + client.models = client.models.filter(model => regexp.test(model.name)); + } + if (exportCore) { await rmdir(outputPathCore); await mkdir(outputPathCore); diff --git a/src/utils/writeClientIndex.ts b/src/utils/writeClientIndex.ts index 72723c0f1..5f0ced589 100644 --- a/src/utils/writeClientIndex.ts +++ b/src/utils/writeClientIndex.ts @@ -30,7 +30,7 @@ export const writeClientIndex = async ( useUnionTypes: boolean, exportCore: boolean, exportServices: boolean | string, - exportModels: boolean, + exportModels: boolean | string, exportSchemas: boolean, postfixServices: string, postfixModels: string, diff --git a/types/index.d.ts b/types/index.d.ts index f347d74bc..127ec7af9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -21,7 +21,7 @@ export type Options = { useUnionTypes?: boolean; exportCore?: boolean; exportServices?: boolean | string; - exportModels?: boolean; + exportModels?: boolean | string; exportSchemas?: boolean; indent?: Indent | '4' | '2' | 'tab'; postfixServices?: string;