From ac880e0ee59fa2999fb23b35e1608a3b2926021e Mon Sep 17 00:00:00 2001 From: Tim de Wolf Date: Thu, 11 Apr 2019 10:40:39 +0200 Subject: [PATCH 1/4] add export keyword to output before namespace --- src/swagger-2.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swagger-2.ts b/src/swagger-2.ts index f52e2e571..6d664c891 100644 --- a/src/swagger-2.ts +++ b/src/swagger-2.ts @@ -45,7 +45,7 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string { const shouldCamelCase = options.camelcase || false; const queue: [string, Swagger2Definition][] = []; - const output: string[] = [`namespace ${namespace} {`]; + const output: string[] = [`export namespace ${namespace} {`]; const { definitions } = spec; From b838de036e63e92b9b96f3e7cc53e60b61d014bf Mon Sep 17 00:00:00 2001 From: Tim de Wolf Date: Thu, 11 Apr 2019 10:41:41 +0200 Subject: [PATCH 2/4] run test --- example/output.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/output.ts b/example/output.ts index 0f49030b4..0598743bc 100644 --- a/example/output.ts +++ b/example/output.ts @@ -1,4 +1,4 @@ -namespace OpenAPI2 { +export namespace OpenAPI2 { export interface ValueProp { // Heading of a value proposition. header: string; From 48e1f896ceebc43c845c60e58de4279ad56ef870 Mon Sep 17 00:00:00 2001 From: Tim de Wolf Date: Thu, 11 Apr 2019 10:54:52 +0200 Subject: [PATCH 3/4] added option to export --- README.md | 2 ++ bin/cli.js | 6 ++++++ package.json | 2 +- src/swagger-2.ts | 8 +++++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8237ecc81..b1342af44 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ also use the Node API (below). | `--namespace [name]` | `-n` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) | | `--swagger [version]` | `-s` | `2` | Which Swagger version to use. Currently only supports `2`. | | `--camelcase` | `-c` | `false` | Convert `snake_case` properties to `camelCase`? | +| `--export` | `-e` | `false` | Exports the namespace | ### Node @@ -107,6 +108,7 @@ in handy. | `namespace` | `string` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) | | `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. | | `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` | +| `export` | `boolean` | `false` | Exports the namespace | [glob]: https://www.npmjs.com/package/glob [js-yaml]: https://www.npmjs.com/package/js-yaml diff --git a/bin/cli.js b/bin/cli.js index 21ab874f1..c7b09135c 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -19,6 +19,7 @@ Options --output, -o specify output file --camelcase, -c convert snake_case properties to camelCase (default: off) --swagger, -s specify Swagger version (default: 2) + --export, -e exports the namespace (default: false) `, { flags: { @@ -39,6 +40,11 @@ Options type: 'number', alias: 's', }, + export: { + type: 'boolean', + default: false, + alias: 'e', + }, }, } ); diff --git a/package.json b/package.json index 2ba8c0fcb..9a7d97492 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "scripts": { "build": "pack build", "pregenerate": "npm run build", - "generate": "node pkg/bin/cli example/input.yaml -o example/output.ts", + "generate": "node pkg/bin/cli example/input.yaml -e -o example/output.ts", "lint": "eslint --ignore-path .gitignore --ext .js,.ts src", "prepublish": "npm run build", "publish": "pack publish", diff --git a/src/swagger-2.ts b/src/swagger-2.ts index 6d664c891..434417115 100644 --- a/src/swagger-2.ts +++ b/src/swagger-2.ts @@ -23,6 +23,7 @@ export interface Swagger2 { export interface Swagger2Options { camelcase?: boolean; namespace?: string; + export?: boolean; } // Primitives only! @@ -43,9 +44,14 @@ function camelCase(name: string): string { function parse(spec: Swagger2, options: Swagger2Options = {}): string { const namespace = options.namespace || 'OpenAPI2'; const shouldCamelCase = options.camelcase || false; + const shouldExport = options.export || false; + + console.log('shouldExport', shouldExport); const queue: [string, Swagger2Definition][] = []; - const output: string[] = [`export namespace ${namespace} {`]; + + const output: string[] = shouldExport ? ['export '] : []; + output.push(`namespace ${namespace} {`); const { definitions } = spec; From d68cb66462be7bacf6c970c5307c9750516f335f Mon Sep 17 00:00:00 2001 From: Tim de Wolf Date: Thu, 11 Apr 2019 11:03:44 +0200 Subject: [PATCH 4/4] remove logs and make tests pass --- example/output.ts | 2 +- package.json | 2 +- src/swagger-2.ts | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/example/output.ts b/example/output.ts index 0598743bc..0f49030b4 100644 --- a/example/output.ts +++ b/example/output.ts @@ -1,4 +1,4 @@ -export namespace OpenAPI2 { +namespace OpenAPI2 { export interface ValueProp { // Heading of a value proposition. header: string; diff --git a/package.json b/package.json index 9a7d97492..2ba8c0fcb 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "scripts": { "build": "pack build", "pregenerate": "npm run build", - "generate": "node pkg/bin/cli example/input.yaml -e -o example/output.ts", + "generate": "node pkg/bin/cli example/input.yaml -o example/output.ts", "lint": "eslint --ignore-path .gitignore --ext .js,.ts src", "prepublish": "npm run build", "publish": "pack publish", diff --git a/src/swagger-2.ts b/src/swagger-2.ts index 434417115..2cc6dd4d9 100644 --- a/src/swagger-2.ts +++ b/src/swagger-2.ts @@ -46,8 +46,6 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string { const shouldCamelCase = options.camelcase || false; const shouldExport = options.export || false; - console.log('shouldExport', shouldExport); - const queue: [string, Swagger2Definition][] = []; const output: string[] = shouldExport ? ['export '] : [];