diff --git a/bin/index.js b/bin/index.js index 0e150e9f..c38cce7a 100755 --- a/bin/index.js +++ b/bin/index.js @@ -13,10 +13,10 @@ program .option('--client [value]', 'HTTP client to generate [fetch, xhr]', 'fetch') .parse(process.argv); -const SwaggerCodegen = require(path.resolve(__dirname, '../dist/index.js')); +const OpenAPI = require(path.resolve(__dirname, '../dist/index.js')); -if (SwaggerCodegen) { - SwaggerCodegen.generate( +if (OpenAPI) { + OpenAPI.generate( program.input, program.output, program.client diff --git a/src/index.spec.ts b/src/index.spec.ts new file mode 100644 index 00000000..e3e52483 --- /dev/null +++ b/src/index.spec.ts @@ -0,0 +1,11 @@ +import * as OpenAPI from '.'; + +describe('index', () => { + it('parses v2 without issues', () => { + OpenAPI.generate('./test/mock/v2/spec.json', './test/result/v2/', OpenAPI.HttpClient.FETCH, false); + }); + + it('parses v3 without issues', () => { + OpenAPI.generate('./test/mock/v3/spec.json', './test/result/v3/', OpenAPI.HttpClient.FETCH, false); + }); +}); diff --git a/src/index.ts b/src/index.ts index 297b69bf..76dc8da9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,8 +19,9 @@ export enum HttpClient { * @param input The relative location of the OpenAPI spec. * @param output The relative location of the output directory. * @param httpClient The selected httpClient (fetch or XHR). + * @param write Write the files to disk (true or false) */ -export function generate(input: string, output: string, httpClient: HttpClient = HttpClient.FETCH): void { +export function generate(input: string, output: string, httpClient: HttpClient = HttpClient.FETCH, write: boolean = false): void { const inputPath = path.resolve(process.cwd(), input); const outputPath = path.resolve(process.cwd(), output); @@ -34,12 +35,16 @@ export function generate(input: string, output: string, httpClient: HttpClient = switch (openApiVersion) { case OpenApiVersion.V2: const clientV2 = parseV2(openApi); - writeClient(clientV2, httpClient, templates, outputPath); + if (write) { + writeClient(clientV2, httpClient, templates, outputPath); + } break; case OpenApiVersion.V3: const clientV3 = parseV3(openApi); - writeClient(clientV3, httpClient, templates, outputPath); + if (write) { + writeClient(clientV3, httpClient, templates, outputPath); + } break; } } catch (e) {