Skip to content

Commit 859961e

Browse files
authored
Merge pull request ferdikoomen#4 from nicolas-chaulet/feat/export-services-regexp
feat(client): support regexp to select services to export
2 parents e11a373 + 69fbc94 commit 859961e

File tree

7 files changed

+31
-6
lines changed

7 files changed

+31
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $ openapi --help
4343
--useOptions Use options instead of arguments
4444
--useUnionTypes Use union types instead of enums
4545
--exportCore <value> Write core files to disk (default: true)
46-
--exportServices <value> Write services to disk (default: true)
46+
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
4747
--exportModels <value> Write models to disk (default: true)
4848
--exportSchemas <value> Write schemas to disk (default: false)
4949
--indent <value> Indentation options [4, 2, tab] (default: "4")

bin/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ const params = program
3030
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
3131

3232
if (OpenAPI) {
33+
let exportServices;
34+
try {
35+
exportServices = JSON.parse(params.exportServices) === true;
36+
} catch (error) {
37+
exportServices = params.exportServices;
38+
}
3339
OpenAPI.generate({
3440
input: params.input,
3541
output: params.output,
@@ -38,7 +44,7 @@ if (OpenAPI) {
3844
useOptions: params.useOptions,
3945
useUnionTypes: params.useUnionTypes,
4046
exportCore: JSON.parse(params.exportCore) === true,
41-
exportServices: JSON.parse(params.exportServices) === true,
47+
exportServices,
4248
exportModels: JSON.parse(params.exportModels) === true,
4349
exportSchemas: JSON.parse(params.exportSchemas) === true,
4450
indent: params.indent,

bin/index.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ describe('bin', () => {
4343
expect(result.stderr.toString()).toBe('');
4444
});
4545

46+
it('it should support regexp in exportSchemas', async () => {
47+
const result = crossSpawn.sync('node', [
48+
'./bin/index.js',
49+
'--input',
50+
'./test/spec/v3.json',
51+
'--output',
52+
'./test/generated/bin',
53+
'--exportServices',
54+
'^(Simple|Types)',
55+
]);
56+
expect(result.stdout.toString()).toBe('');
57+
expect(result.stderr.toString()).toBe('');
58+
});
59+
4660
it('it should throw error without params', async () => {
4761
const result = crossSpawn.sync('node', ['./bin/index.js']);
4862
expect(result.stdout.toString()).toBe('');

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type Options = {
2020
useOptions?: boolean;
2121
useUnionTypes?: boolean;
2222
exportCore?: boolean;
23-
exportServices?: boolean;
23+
exportServices?: boolean | string;
2424
exportModels?: boolean;
2525
exportSchemas?: boolean;
2626
indent?: Indent;

src/utils/writeClient.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const writeClient = async (
4141
useOptions: boolean,
4242
useUnionTypes: boolean,
4343
exportCore: boolean,
44-
exportServices: boolean,
44+
exportServices: boolean | string,
4545
exportModels: boolean,
4646
exportSchemas: boolean,
4747
indent: Indent,
@@ -60,6 +60,11 @@ export const writeClient = async (
6060
throw new Error(`Output folder is not a subdirectory of the current working directory`);
6161
}
6262

63+
if (typeof exportServices === 'string') {
64+
const regexp = new RegExp(exportServices);
65+
client.services = client.services.filter(service => regexp.test(service.name));
66+
}
67+
6368
if (exportCore) {
6469
await rmdir(outputPathCore);
6570
await mkdir(outputPathCore);

src/utils/writeClientIndex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const writeClientIndex = async (
2929
outputPath: string,
3030
useUnionTypes: boolean,
3131
exportCore: boolean,
32-
exportServices: boolean,
32+
exportServices: boolean | string,
3333
exportModels: boolean,
3434
exportSchemas: boolean,
3535
postfixServices: string,

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type Options = {
2020
useOptions?: boolean;
2121
useUnionTypes?: boolean;
2222
exportCore?: boolean;
23-
exportServices?: boolean;
23+
exportServices?: boolean | string;
2424
exportModels?: boolean;
2525
exportSchemas?: boolean;
2626
indent?: Indent | '4' | '2' | 'tab';

0 commit comments

Comments
 (0)