Skip to content

Commit

Permalink
feat(scripts): add format & format:check using prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
phatpham9 committed Mar 25, 2020
1 parent e089f84 commit ae24521
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .huskyrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"hooks": {
"pre-commit": "yarn lint",
"pre-commit": "yarn format && yarn lint",
"pre-push": "yarn build"
}
}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"parser": "typescript",
"singleQuote": true,
"trailingComma": "all"
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"purge": "rm -rf node_modules",
"clean": "rm -rf dist",
"dev": "rollup -cw",
"format": "prettier --write **/*.ts",
"format:check": "prettier --check **/*.ts",
"lint": "tslint --project tsconfig.json --config tslint.json",
"prebuild": "yarn clean",
"build": "rollup -c",
Expand All @@ -27,6 +29,8 @@
"@types/http-status-codes": "^1.2.0",
"copyfiles": "^2.2.0",
"husky": "^2.3.0",
"prettier": "^2.0.2",
"pretty-quick": "^2.0.1",
"rollup": "^1.12.3",
"rollup-plugin-async": "^1.2.0",
"rollup-plugin-commonjs": "^10.0.0",
Expand Down
10 changes: 5 additions & 5 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class MyError extends Error {
constructor(err: any, isOperational = false) {
super();

this.name = err instanceof Error || typeof err === 'object' ? err.name : 'Error';
this.message = err instanceof Error || typeof err === 'object' ? err.message : err;
this.name =
err instanceof Error || typeof err === 'object' ? err.name : 'Error';
this.message =
err instanceof Error || typeof err === 'object' ? err.message : err;
this.isOperational = isOperational;

// Restore prototype chain
Expand Down Expand Up @@ -43,9 +45,7 @@ class HttpError extends MyError {
message: this.message,
};

return !isDev()
? content
: { ...content, error: this.stack };
return !isDev() ? content : { ...content, error: this.stack };
}
}

Expand Down
22 changes: 13 additions & 9 deletions src/express.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Request, Response, NextFunction, Router } from 'express';
import { NOT_FOUND, OK, INTERNAL_SERVER_ERROR, getStatusText } from 'http-status-codes';
import {
NOT_FOUND,
OK,
INTERNAL_SERVER_ERROR,
getStatusText,
} from 'http-status-codes';

import { MyError, HttpError } from './error';
import errorHandler from './errorHandler';
Expand All @@ -25,7 +30,12 @@ const handleNotFound = (_: Request, __: Response, next: NextFunction) => {
* @param res Express Response object
* @param __ Express Next function
*/
const handleErrors = (err: MyError | HttpError, _: Request, res: Response, __: NextFunction) => {
const handleErrors = (
err: MyError | HttpError,
_: Request,
res: Response,
__: NextFunction,
) => {
errorHandler.handle(err);

try {
Expand Down Expand Up @@ -58,10 +68,4 @@ const health = () => {
return router;
};

export {
handleNotFound,
handleErrors,
handleHealthCheck,

health,
};
export { handleNotFound, handleErrors, handleHealthCheck, health };
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/

const isDev = (): boolean => {
return !['test', 'staging', 'production'].includes(process.env.NODE_ENV as string);
return !['test', 'staging', 'production'].includes(
process.env.NODE_ENV as string,
);
};

const isTest = (): boolean => {
Expand Down
Loading

0 comments on commit ae24521

Please sign in to comment.