Skip to content

Commit

Permalink
chore: setup dev tools
Browse files Browse the repository at this point in the history
* add husky with hooks for pre-commit (lint-staged) & commit-msg
* add lint-staged for eslint & prettier
* add commitlint
* add typescript with @warungpintar/warpin-script (migration to typescript)
  • Loading branch information
R Adysurya A committed Feb 18, 2021
1 parent a2cccc2 commit 87f3bfe
Show file tree
Hide file tree
Showing 40 changed files with 3,989 additions and 452 deletions.
1 change: 1 addition & 0 deletions .config/husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .config/husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1
4 changes: 4 additions & 0 deletions .config/husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
.*.js
*.config.js
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@warungpintar/warpin-scripts/config/eslint.config');
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules
node_modules/
/lib

dist/

8 changes: 8 additions & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
'src/**/*.{js?(x),ts?(x)}': [
'eslint --fix',
'prettier --write',
// @TODO: {jest} 'yarn test --findRelatedTests',
],
'**/*.ts?(x)': () => 'tsc -p tsconfig.json --noEmit',
};
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package.json
/lib
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@warungpintar/warpin-scripts/config/prettier.config.js");
20 changes: 20 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"editor.tabSize": 2,
"editor.rulers": [80],
"editor.wordWrapColumn": 80,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"editor.formatOnSave": true,
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "typescript",
"autoFix": true
},
{
"language": "typescriptreact",
"autoFix": true
}
]
}
28 changes: 28 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const BABEL_ENV = process.env.BABEL_ENV;
const isCommonJS = BABEL_ENV !== undefined && BABEL_ENV === "cjs";
const isESM = BABEL_ENV !== undefined && BABEL_ENV === "esm";
const BASE = require("@warungpintar/warpin-scripts/config/babel.config");

module.exports = function(api) {
const base = BASE(api);
const overrides = [
{
presets: [
[
"@babel/env",
{
loose: true,
modules: isCommonJS ? "commonjs" : false,
targets: {
esmodules: isESM ? true : undefined,
},
},
],
],
},
];
return {
...base,
overrides,
};
};
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {extends: ['@commitlint/config-conventional']}
50 changes: 45 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
"name": "warmock",
"version": "0.0.1",
"description": "Warung Pintar Mock + Foward Proxy API",
"main": "index.js",
"scripts": {
"dev": "CONFIG_PATH=example/.warlock.yaml nodemon src/index.js",
"test": "mocha src/**/*.test.js"
},
"keywords": [
"mock",
"api",
Expand All @@ -15,6 +10,32 @@
],
"author": "wartech",
"license": "ISC",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"typings": "dist/types/index.d.ts",
"files": [
"dist"
],
"scripts": {
"start": "nodemon --watch src --exec yarn build -e ts",
"start:example": "CONFIG_PATH=example/.warlock.yaml nodemon src/index.js",
"format": "prettier",
"lint": "eslint",
"prebuild": "rimraf dist",
"build": "concurrently yarn:build:*",
"build:esm": "cross-env BABEL_ENV=esm babel src --extensions .ts -d dist/esm --source-maps",
"build:cjs": "cross-env BABEL_ENV=cjs babel src --extensions .ts -d dist/cjs --source-maps",
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
"test": "concurrently yarn:test:*",
"test:unit": "mocha src/**/*.test.js",
"test:type": "tsc --noEmit",
"test:lint": "yarn lint src/**/*.ts",
"test:format": "yarn format src/**/*.ts",
"postinstall": "is-ci || husky install .config/husky",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable"
},
"dependencies": {
"@graphql-mesh/cli": "^0.11.12",
"@graphql-mesh/graphql": "^0.13.13",
Expand All @@ -34,8 +55,27 @@
"ramda": "^0.27.1"
},
"devDependencies": {
"@commitlint/cli": "11.0.0",
"@commitlint/config-conventional": "11.0.0",
"@types/faker": "5.1.6",
"@types/jest": "^26.0.20",
"@types/js-yaml": "4.0.0",
"@types/koa": "2.11.8",
"@types/koa-bodyparser": "4.3.0",
"@types/koa-router": "7.4.1",
"@types/node": "^14.14.28",
"@types/ramda": "0.27.38",
"@warungpintar/warpin-scripts": "*",
"cross-env": "7.0.3",
"husky": "5.0.9",
"is-ci": "3.0.0",
"lint-staged": "10.5.4",
"mocha": "^8.3.0",
"nodemon": "^2.0.7",
"pinst": "2.1.4",
"should": "^13.2.3"
},
"resolutions": {
"typescript": "4.1.5"
}
}
4 changes: 0 additions & 4 deletions src/config.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const CONFIG = {};

export default CONFIG;
11 changes: 0 additions & 11 deletions src/defaultResolvers/faker.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/defaultResolvers/faker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import faker from 'faker';
import R from 'ramda';

const resolver = (_: any, args: { props: string }) => {
const fakerProps = args.props?.split('.') ?? [];
const fakerFn = R.path<() => string>(fakerProps)(faker);
return fakerFn?.();
};

export default resolver;
5 changes: 0 additions & 5 deletions src/defaultResolvers/index.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/defaultResolvers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fakerResolver from './faker';

export default {
faker: fakerResolver,
};
36 changes: 0 additions & 36 deletions src/index.js

This file was deleted.

39 changes: 39 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-disable */
// @ts-nocheck
// @TODO: use express
import fs from 'fs';
import Koa from 'koa';
import Router from 'koa-router';
import bodyParser from 'koa-bodyparser';
// import yaml from 'js-yaml';
// import configContext from "./middleware/configContext";
// import errorHandler from "./middleware/errorHandler";
// import proxy from "./proxy.js";

try {
// @TODO: {parser-config}
// @TODO: change passing config path by envar to cli arg
// const configFile = process.env.CONFIG_PATH ?? '.warmock.yaml';
// const configContent = fs.readFileSync(configFile, 'utf8');
// const config = yaml.load(configContent);
const router = new Router();
router.use(bodyParser());
// @TODO {proxy} need following @types/koa-bodyparser
// router.use(proxy);
const app = new Koa();
// app.use(errorHandler);
// app.use(configContext(config));
app.use(router.routes());
app.listen(3001);
} catch (error) {
if (
error.message &&
error.message.includes('ENOENT: no such file or directory')
) {
console.error('Config file not found');
} else {
console.error(error);
}

process.exit(1);
}
42 changes: 19 additions & 23 deletions src/libs/HttpClient.js → src/libs/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
const axios = require('axios');
const R = require('ramda');

const defaultResolver = require('../defaultResolvers');
// @ts-nocheck
// @TODO {httpClient}
import axios from 'axios';
import R from 'ramda';
import defaultResolver from '../defaultResolvers';

const createHttpClient = (config, opts, ctx) => {
const httpClient = axios.create();
const { rest } = config;

const hasMockSetup = R.propEq('source', opts.baseURL);
const mocker = R.find(hasMockSetup)(rest)
const mocker = R.find(hasMockSetup)(rest);

const handleOkResponse = (response) => {
const transformResolvers = R.path(
[
'transforms',
opts.url,
ctx.request.method.toLowerCase()
]
)(mocker);
const transformResolvers = R.path([
'transforms',
opts.url,
ctx.request.method.toLowerCase(),
])(mocker);

if (!transformResolvers) {
return Promise.resolve(response)
return Promise.resolve(response);
}

// @TODO: RESOLVER MAPPING LOGIC HERE!
Expand All @@ -42,26 +41,23 @@ const createHttpClient = (config, opts, ctx) => {
...response,
data: {
...response.data,
results: response.data.results.map(result => ({
results: response.data.results.map((result) => ({
...result,
name: defaultResolver['faker'](result, { props: 'name.firstName' }, ctx)
name: defaultResolver.faker(result, { props: 'name.firstName' }, ctx),
})),
},
})
});
};

const handleErrorResponse = error => {
const handleErrorResponse = (error) => {
return Promise.reject(error);
};

httpClient.interceptors.response.use(
handleOkResponse,
handleErrorResponse,
);
httpClient.interceptors.response.use(handleOkResponse, handleErrorResponse);

return httpClient;
}
};

module.exports = {
export default {
createHttpClient,
};
8 changes: 0 additions & 8 deletions src/libs/ServerError.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/libs/ServerError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type statusCode = number;

class ServerError extends Error {
statusCode: statusCode;
constructor(statusCode: statusCode, message: string) {
super(message);
this.statusCode = statusCode;
}
}

export default ServerError;
16 changes: 0 additions & 16 deletions src/middleware/configContext.js

This file was deleted.

Loading

0 comments on commit 87f3bfe

Please sign in to comment.