Skip to content

Commit

Permalink
- Fixed build and prep for e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdikoomen committed Sep 24, 2020
1 parent c450461 commit 0b6fd22
Show file tree
Hide file tree
Showing 32 changed files with 175 additions and 116 deletions.
13 changes: 5 additions & 8 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
esmodules: true,
},
},
],
['@babel/preset-env', {
targets: {
node: 'current'
}
}],
'@babel/preset-typescript',
],
};
8 changes: 6 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module.exports = {
testRegex: '\\.spec\\.(ts|js)$',
testRegex: '.*\\.spec\\.(ts|js)$',
testPathIgnorePatterns: [
'/node_modules/',
'<rootDir>/dist/',
'<rootDir>/samples/',
],
testEnvironment: 'node',
moduleNameMapper: {
'\\.hbs$': '<rootDir>/src/templates/__mocks__/index.js',
},
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/*.d.ts',
'!src/templates/**',
'!**/node_modules/**',
],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"jest-cli": "26.4.2",
"prettier": "2.1.2",
"reflect-metadata": "0.1.13",
"rollup": "2.28.1",
"rollup": "2.28.2",
"rollup-plugin-terser": "7.0.2",
"rollup-plugin-typescript2": "0.27.2",
"swagger-ui-express": "4.1.4",
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const commonjs = require('@rollup/plugin-commonjs');
const {nodeResolve} = require('@rollup/plugin-node-resolve');
const {terser} = require('rollup-plugin-terser');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const { terser } = require('rollup-plugin-terser');
const typescript = require('rollup-plugin-typescript2');
const handlebars = require('handlebars');
const path = require('path');
Expand Down
20 changes: 10 additions & 10 deletions samples/codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ rm -rf dist
rm swagger-codegen-cli-v2.jar
rm swagger-codegen-cli-v3.jar

curl https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.15/swagger-codegen-cli-2.4.15.jar -o swagger-codegen-cli-v2.jar
curl https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.21/swagger-codegen-cli-3.0.21.jar -o swagger-codegen-cli-v3.jar
#curl https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.15/swagger-codegen-cli-2.4.15.jar -o swagger-codegen-cli-v2.jar
#curl https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.21/swagger-codegen-cli-3.0.21.jar -o swagger-codegen-cli-v3.jar

java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-aurelia -o dist/v2/typescript-aurelia/
java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-angular -o dist/v2/typescript-angular/
java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-inversify -o dist/v2/typescript-inversify/
java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-fetch -o dist/v2/typescript-fetch/
java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-jquery -o dist/v2/typescript-jquery/
java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-node -o dist/v2/typescript-node/
#java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-aurelia -o dist/v2/typescript-aurelia/
#java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-angular -o dist/v2/typescript-angular/
#java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-inversify -o dist/v2/typescript-inversify/
#java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-fetch -o dist/v2/typescript-fetch/
#java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-jquery -o dist/v2/typescript-jquery/
#java -jar ./swagger-codegen-cli-v2.jar generate -i spec/v2.json -l typescript-node -o dist/v2/typescript-node/

java -jar ./swagger-codegen-cli-v3.jar generate -i spec/v3.json -l typescript-angular -o dist/v3/typescript-angular/
java -jar ./swagger-codegen-cli-v3.jar generate -i spec/v3.json -l typescript-fetch -o dist/v3/typescript-fetch/
#java -jar ./swagger-codegen-cli-v3.jar generate -i spec/v3.json -l typescript-angular -o dist/v3/typescript-angular/
#java -jar ./swagger-codegen-cli-v3.jar generate -i spec/v3.json -l typescript-fetch -o dist/v3/typescript-fetch/

node ../bin/index.js --input spec/v2.json --output dist/v2/openapi-typescript-codegen/
node ../bin/index.js --input spec/v3.json --output dist/v3/openapi-typescript-codegen/
40 changes: 22 additions & 18 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import * as OpenAPI from './index';

describe('index', () => {
it('parses v2 without issues', async () => {
// await OpenAPI.generate({
// input: './test/mock/v2/spec.json',
// output: './test/result/v2/',
// useOptions: true
// });
await OpenAPI.generate({
input: './test/spec/v3.json',
output: './temp/v3/',
write: false,
});
});

it('parses v3 without issues', async () => {
// await OpenAPI.generate({
// input: './test/mock/v3/spec.json',
// output: './test/result/v3/',
// useOptions: true
// });
await OpenAPI.generate({
input: './test/spec/v3.json',
output: './temp/v3/',
write: false,
});
});

it('downloads and parses v2 without issues', async () => {
// await OpenAPI.generate({
// input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/mock/v2/spec.json',
// output: './test/result/v2-downloaded/'
// });
await OpenAPI.generate({
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/spec/v2.json',
output: './temp/v2-downloaded/',
write: false,
});
});

it('downloads and parses v3 without issues', async () => {
// await OpenAPI.generate({
// input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/mock/v3/spec.json',
// output: './test/result/v3-downloaded/'
// });
await OpenAPI.generate({
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/spec/v3.json',
output: './temp/v3-downloaded/',
write: false,
});
});
});
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Options {
exportServices?: boolean;
exportModels?: boolean;
exportSchemas?: boolean;
write?: boolean;
}

/**
Expand Down Expand Up @@ -50,6 +51,7 @@ export async function generate({
exportServices = true,
exportModels = true,
exportSchemas = false,
write = true,
}: Options): Promise<void> {
// Load the specification, read the OpenAPI version and load the
// handlebar templates for the given language
Expand All @@ -61,13 +63,15 @@ export async function generate({
case OpenApiVersion.V2: {
const client = parseV2(openApi);
const clientFinal = postProcessClient(client);
if (!write) break;
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
break;
}

case OpenApiVersion.V3: {
const client = parseV3(openApi);
const clientFinal = postProcessClient(client);
if (!write) break;
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getComment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EOL } from 'os';
import * as os from 'os';

import { getComment } from './getComment';

describe('getComment', () => {
it('should parse comments', () => {
const multiline = 'Testing multiline comments.' + EOL + ' * This must go to the next line.' + EOL + ' * ' + EOL + ' * This will contain a break.';
const multiline = 'Testing multiline comments.' + os.EOL + ' * This must go to the next line.' + os.EOL + ' * ' + os.EOL + ' * This will contain a break.';
expect(getComment('')).toEqual(null);
expect(getComment('Hello')).toEqual('Hello');
expect(getComment('Hello World!')).toEqual('Hello World!');
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v2/parser/getComment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EOL } from 'os';
import * as os from 'os';

/**
* Cleanup comment and prefix multiline comments with "*",
Expand All @@ -7,7 +7,7 @@ import { EOL } from 'os';
*/
export function getComment(comment?: string): string | null {
if (comment) {
return comment.replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`);
return comment.replace(/\r?\n(.*)/g, (_, w) => `${os.EOL} * ${w.trim()}`);
}
return null;
}
4 changes: 2 additions & 2 deletions src/openApi/v3/parser/getComment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EOL } from 'os';
import * as os from 'os';

import { getComment } from './getComment';

describe('getComment', () => {
it('should parse comments', () => {
const multiline = 'Testing multiline comments.' + EOL + ' * This must go to the next line.' + EOL + ' * ' + EOL + ' * This will contain a break.';
const multiline = 'Testing multiline comments.' + os.EOL + ' * This must go to the next line.' + os.EOL + ' * ' + os.EOL + ' * This will contain a break.';
expect(getComment('')).toEqual(null);
expect(getComment('Hello')).toEqual('Hello');
expect(getComment('Hello World!')).toEqual('Hello World!');
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/v3/parser/getComment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EOL } from 'os';
import * as os from 'os';

/**
* Cleanup comment and prefix multiline comments with "*",
Expand All @@ -7,7 +7,7 @@ import { EOL } from 'os';
*/
export function getComment(comment?: string): string | null {
if (comment) {
return comment.replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`);
return comment.replace(/\r?\n(.*)/g, (_, w) => `${os.EOL} * ${w.trim()}`);
}
return null;
}
6 changes: 3 additions & 3 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EOL } from 'os';
import * as os from 'os';

export function format(s: string): string {
let indent: number = 0;
let lines = s.split(EOL);
let lines = s.split(os.EOL);
lines = lines.map(line => {
line = line.trim().replace(/^\*/g, ' *');
let i = indent;
Expand All @@ -19,5 +19,5 @@ export function format(s: string): string {
}
return result;
});
return lines.join(EOL);
return lines.join(os.EOL);
}
8 changes: 4 additions & 4 deletions src/utils/getOpenApiSpec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { safeLoad } from 'js-yaml';
import { extname } from 'path';
import * as yaml from 'js-yaml';
import * as path from 'path';

import { readSpec } from './readSpec';

Expand All @@ -10,13 +10,13 @@ import { readSpec } from './readSpec';
* @param input
*/
export async function getOpenApiSpec(input: string): Promise<any> {
const extension = extname(input).toLowerCase();
const extension = path.extname(input).toLowerCase();
const content = await readSpec(input);
switch (extension) {
case '.yml':
case '.yaml':
try {
return safeLoad(content);
return yaml.safeLoad(content);
} catch (e) {
throw new Error(`Could not parse OpenApi YAML: "${input}"`);
}
Expand Down
15 changes: 15 additions & 0 deletions src/utils/isSubdirectory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as path from 'path';

import { isSubDirectory } from './isSubdirectory';

describe('isSubDirectory', () => {
it('should return correct result', () => {
expect(isSubDirectory(path.resolve('/'), path.resolve('/'))).toBeFalsy();
expect(isSubDirectory(path.resolve('.'), path.resolve('.'))).toBeFalsy();
expect(isSubDirectory(path.resolve('./project'), path.resolve('./project'))).toBeFalsy();
expect(isSubDirectory(path.resolve('./project'), path.resolve('../'))).toBeFalsy();
expect(isSubDirectory(path.resolve('./project'), path.resolve('../../'))).toBeFalsy();
expect(isSubDirectory(path.resolve('./'), path.resolve('./output'))).toBeTruthy();
expect(isSubDirectory(path.resolve('./'), path.resolve('../output'))).toBeTruthy();
});
});
4 changes: 2 additions & 2 deletions src/utils/isSubdirectory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relative } from 'path';
import * as path from 'path';

export function isSubDirectory(parent: string, child: string) {
return relative(child, parent).startsWith('..');
return path.relative(child, parent).startsWith('..');
}
2 changes: 1 addition & 1 deletion src/utils/readSpecFromDisk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import * as path from 'path';

import { exists, readFile } from './fileSystem';

Expand Down
2 changes: 1 addition & 1 deletion src/utils/readSpecFromHttp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import http from 'http';
import * as http from 'http';

/**
* Download the spec file from a HTTP resource
Expand Down
2 changes: 1 addition & 1 deletion src/utils/readSpecFromHttps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import https from 'https';
import * as https from 'https';

/**
* Download the spec file from a HTTPS resource
Expand Down
12 changes: 12 additions & 0 deletions src/utils/registerHandlebarHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Handlebars from 'handlebars/runtime';

import { registerHandlebarHelpers } from './registerHandlebarHelpers';

describe('registerHandlebarHelpers', () => {
it('should register the helpers', () => {
registerHandlebarHelpers();
const helpers = Object.keys(Handlebars.helpers);
expect(helpers).toContain('equals');
expect(helpers).toContain('notEquals');
});
});
21 changes: 21 additions & 0 deletions src/utils/registerHandlebarTemplates.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { registerHandlebarTemplates } from './registerHandlebarTemplates';

describe('registerHandlebarTemplates', () => {
it('should return correct templates', () => {
const templates = registerHandlebarTemplates();
expect(templates.index).toBeDefined();
expect(templates.exports.model).toBeDefined();
expect(templates.exports.schema).toBeDefined();
expect(templates.exports.service).toBeDefined();
expect(templates.core.settings).toBeDefined();
expect(templates.core.apiError).toBeDefined();
expect(templates.core.getFormData).toBeDefined();
expect(templates.core.getQueryString).toBeDefined();
expect(templates.core.isSuccess).toBeDefined();
expect(templates.core.request).toBeDefined();
expect(templates.core.requestOptions).toBeDefined();
expect(templates.core.requestUsingFetch).toBeDefined();
expect(templates.core.requestUsingXHR).toBeDefined();
expect(templates.core.result).toBeDefined();
});
});
10 changes: 10 additions & 0 deletions src/utils/sort.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { sort } from './sort';

describe('sort', () => {
it('should return correct index', () => {
expect(sort('a', 'b')).toEqual(-1);
expect(sort('b', 'a')).toEqual(1);
expect(sort('a', 'a')).toEqual(0);
expect(sort('', '')).toEqual(0);
});
});
12 changes: 12 additions & 0 deletions src/utils/unique.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { unique } from './unique';

describe('unique', () => {
it('should return correct index', () => {
expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy();
expect(unique('a', 1, ['a', 'b', 'c'])).toBeFalsy();
expect(unique('a', 2, ['a', 'b', 'c'])).toBeFalsy();
expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy();
expect(unique('a', 1, ['z', 'a', 'b'])).toBeTruthy();
expect(unique('a', 2, ['y', 'z', 'a'])).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion src/utils/writeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('writeClient', () => {
},
};

await writeClient(client, templates, '/', HttpClient.FETCH, false, false, true, true, true, true);
await writeClient(client, templates, './dist', HttpClient.FETCH, false, false, true, true, true, true);

expect(rmdir).toBeCalled();
expect(mkdir).toBeCalled();
Expand Down
Loading

0 comments on commit 0b6fd22

Please sign in to comment.