Skip to content

Commit

Permalink
- Modernized setup and using typescript for test files
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdikoomen committed Jan 6, 2022
1 parent cfdbe48 commit 07c98cf
Show file tree
Hide file tree
Showing 50 changed files with 767 additions and 775 deletions.
14 changes: 0 additions & 14 deletions .babelrc.js

This file was deleted.

4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
test/generated
test/e2e/generated
node_modules
10 changes: 2 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"prettier"
],
"extends": ["plugin:@typescript-eslint/recommended", "plugin:prettier/recommended", "prettier"],
"env": {
"es6": true,
"node": true,
"jest": true
},
"plugins": [
"simple-import-sort"
],
"plugins": ["simple-import-sort"],
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-inferrable-types": 0,
Expand Down
18 changes: 18 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": true
}
}
],
[
"@babel/preset-typescript",
{
"onlyRemoveTypeImports": true
}
]
]
}
40 changes: 0 additions & 40 deletions jest.config.js

This file was deleted.

34 changes: 34 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
projects: [
{
displayName: 'UNIT',
testEnvironment: 'node',
testMatch: ['<rootDir>/src/**/*.spec.ts', '<rootDir>/test/index.spec.ts'],
moduleFileExtensions: ['js', 'ts', 'd.ts'],
moduleNameMapper: {
'\\.hbs$': '<rootDir>/src/templates/__mocks__/index.ts',
},
},
{
displayName: 'E2E',
testEnvironment: 'node',
testMatch: [
'<rootDir>/test/e2e/v2.fetch.spec.ts',
'<rootDir>/test/e2e/v2.xhr.spec.ts',
'<rootDir>/test/e2e/v2.node.spec.ts',
'<rootDir>/test/e2e/v2.axios.spec.ts',
'<rootDir>/test/e2e/v2.babel.spec.ts',
'<rootDir>/test/e2e/v3.fetch.spec.ts',
'<rootDir>/test/e2e/v3.xhr.spec.ts',
'<rootDir>/test/e2e/v3.node.spec.ts',
'<rootDir>/test/e2e/v3.axios.spec.ts',
'<rootDir>/test/e2e/v3.babel.spec.ts',
],
},
],
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!<rootDir>/src/**/*.d.ts', '!<rootDir>/bin', '!<rootDir>/dist'],
};

export default config;
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
"keywords": [
"openapi",
"swagger",
"codegen",
"generator",
"client",
"typescript",
"yaml",
"json",
"fetch",
"xhr",
"axios",
"node"
],
"maintainers": [
Expand All @@ -46,16 +45,15 @@
"build": "rollup --config --environment NODE_ENV:development",
"build:watch": "rollup --config --environment NODE_ENV:development --watch",
"release": "rollup --config --environment NODE_ENV:production",
"run": "NODE_ENV=production node ./test/index.js",
"validate": "tsc --project tsconfig.json --noEmit",
"run": "node ./test/index.js",
"test": "jest --selectProjects UNIT",
"test:update": "jest --selectProjects UNIT --updateSnapshot",
"test:watch": "jest --selectProjects UNIT --watch",
"test:coverage": "jest --selectProjects UNIT --coverage",
"test:e2e": "jest --selectProjects E2E --runInBand",
"eslint": "eslint \"./src/**/*.ts\" \"./bin/index.js\" \"./types/index.d.ts\"",
"eslint:fix": "eslint \"./src/**/*.ts\" \"./bin/index.js\" \"./types/index.d.ts\" --fix",
"prettier": "prettier \"./src/**/*.ts\" \"./bin/index.js\" \"./types/index.d.ts\" --check",
"prettier:fix": "prettier \"./src/**/*.ts\" \"./bin/index.js\" \"./types/index.d.ts\" --write",
"eslint": "eslint .",
"eslint:fix": "eslint . --fix",
"prepublishOnly": "yarn run clean && yarn run release",
"codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b"
},
Expand All @@ -68,17 +66,17 @@
"form-data": "^4.0.0",
"handlebars": "^4.7.6",
"json-schema-ref-parser": "^9.0.7",
"mkdirp": "^1.0.4",
"node-fetch": "^2.6.5",
"rimraf": "^3.0.2"
"node-fetch": "^2.6.6"
},
"devDependencies": {
"@babel/cli": "7.16.7",
"@babel/core": "7.16.7",
"@babel/preset-env": "7.16.7",
"@babel/preset-typescript": "7.16.7",
"@rollup/plugin-alias": "3.1.9",
"@rollup/plugin-commonjs": "21.0.1",
"@rollup/plugin-node-resolve": "13.1.3",
"@rollup/plugin-typescript": "8.3.0",
"@types/express": "4.17.13",
"@types/glob": "7.2.0",
"@types/jest": "27.4.0",
Expand All @@ -98,9 +96,11 @@
"prettier": "2.5.1",
"puppeteer": "13.0.1",
"qs": "6.10.2",
"rimraf": "^3.0.2",
"rollup": "2.63.0",
"rollup-plugin-node-externals": "3.1.2",
"rollup-plugin-terser": "7.0.2",
"rollup-plugin-typescript2": "0.31.1",
"ts-node": "10.4.0",
"tslib": "2.3.1",
"typescript": "4.5.4"
}
Expand Down
69 changes: 34 additions & 35 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict';

const commonjs = require('@rollup/plugin-commonjs');
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');
const fs = require('fs');

const pkg = require('./package.json');
const external = Object.keys(pkg.dependencies);
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import { readFileSync } from 'fs';
import { precompile } from 'handlebars';
import { dirname, extname, resolve } from 'path';
import externals from 'rollup-plugin-node-externals';
import { terser } from 'rollup-plugin-terser';

/**
* Custom plugin to parse handlebar imports and precompile
Expand All @@ -18,15 +15,15 @@ const external = Object.keys(pkg.dependencies);
*/
const handlebarsPlugin = () => ({
resolveId: (file, importer) => {
if (path.extname(file) === '.hbs') {
return path.resolve(path.dirname(importer), file);
if (extname(file) === '.hbs') {
return resolve(dirname(importer), file);
}
return null;
},
load: (file) => {
if (path.extname(file) === '.hbs') {
const template = fs.readFileSync(file, 'utf8').toString().trim();
const templateSpec = handlebars.precompile(template, {
load: file => {
if (extname(file) === '.hbs') {
const template = readFileSync(file, 'utf8').toString().trim();
const templateSpec = precompile(template, {
strict: true,
noEscape: true,
preventIndent: true,
Expand All @@ -38,43 +35,45 @@ const handlebarsPlugin = () => ({
union: true,
intersection: true,
enumerator: true,
escapeQuotes: true,
},
});
return `export default ${templateSpec};`;
}
return null;
}
},
});

const getPlugins = () => {
const plugins = [
handlebarsPlugin(),
typescript(),
externals({
deps: true,
}),
nodeResolve(),
commonjs(),
]
commonjs({
sourceMap: false,
}),
handlebarsPlugin(),
typescript({
module: 'esnext',
}),
alias({
entries: {
handlebars: 'handlebars/lib/handlebars.runtime',
},
}),
];
if (process.env.NODE_ENV === 'development') {
return plugins;
}
return [...plugins, terser()];
};

module.exports = {
export default {
input: './src/index.ts',
output: {
exports: 'named',
file: './dist/index.js',
format: 'cjs',
},
external: [
'fs',
'os',
'util',
'path',
'http',
'https',
'handlebars/runtime',
...external,
],
plugins: getPlugins(),
};
2 changes: 1 addition & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as OpenAPI from './index';
import OpenAPI from './index';

describe('index', () => {
it('parses v2 without issues', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,8 @@ export async function generate({
}
}
}

export default {
HttpClient,
generate,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
compiler: [8, '>= 4.3.0'],
useData: true,
main: function () {
Expand Down
1 change: 0 additions & 1 deletion src/templates/core/node/request.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{{>header}}

import { AbortController } from 'abort-controller';
import Blob from 'cross-blob'
import FormData from 'form-data';
import fetch, { BodyInit, Headers, RequestInit, Response } from 'node-fetch';

Expand Down
2 changes: 1 addition & 1 deletion src/templates/partials/schemaComposition.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
type: '{{export}}',
{{#if description}}
description: '{{{escapeQuotes description}}}',
description: `{{{description}}}`,
{{/if}}
contains: [{{#each properties}}{{>schema}}{{#unless @last}}, {{/unless}}{{/each}}],
{{#if isReadOnly}}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/partials/schemaGeneric.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
type: '{{{type}}}',
{{/if}}
{{#if description}}
description: '{{{escapeQuotes description}}}',
description: `{{{description}}}`,
{{/if}}
{{#if isReadOnly}}
isReadOnly: {{{isReadOnly}}},
Expand Down
2 changes: 1 addition & 1 deletion src/templates/partials/schemaInterface.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
{{#if description}}
description: '{{{escapeQuotes description}}}',
description: `{{{description}}}`,
{{/if}}
properties: {
{{#if properties}}
Expand Down
8 changes: 0 additions & 8 deletions src/typings/camelcase.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/typings/mkdirp.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/typings/rimraf.d.ts

This file was deleted.

Loading

0 comments on commit 07c98cf

Please sign in to comment.