From e1024116750451ebcc4320b0a3b84e32f2e899ec Mon Sep 17 00:00:00 2001 From: Westin Wrzesinski Date: Wed, 20 Apr 2022 12:58:06 -0500 Subject: [PATCH] chore: use prettier --- .eslintrc.js | 12 +- .github/workflows/main.yml | 4 +- .husky/.gitignore | 1 + .husky/pre-commit | 4 + .prettierignore | 4 + .prettierrc.json | 1 + README.md | 5 +- babel.config.json | 6 +- index.d.ts | 10 +- index.js | 2 +- index.test-d.ts | 5 +- package.json | 15 +- src/express.js | 277 +++++++++++++++++++------------------ src/index.js | 2 +- 14 files changed, 187 insertions(+), 161 deletions(-) create mode 100644 .husky/.gitignore create mode 100755 .husky/pre-commit create mode 100644 .prettierignore create mode 100644 .prettierrc.json diff --git a/.eslintrc.js b/.eslintrc.js index f1e6b96..e9915fa 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,10 +1,10 @@ /* @flow */ module.exports = { - 'extends': './node_modules/@krakenjs/grumbler-scripts/config/.eslintrc-node.js', - 'rules': { - // legacy rule settings - 'prefer-const': 'off', - 'flowtype/require-exact-type': 'off' - } + extends: "./node_modules/@krakenjs/grumbler-scripts/config/.eslintrc-node.js", + rules: { + // legacy rule settings + "prefer-const": "off", + "flowtype/require-exact-type": "off", + }, }; diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 23f7a02..487afd6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,8 +16,8 @@ jobs: - name: ⎔ Setup node uses: actions/setup-node@v2 with: - node-version: '14' - registry-url: 'https://registry.npmjs.org' + node-version: "14" + registry-url: "https://registry.npmjs.org" - name: 📥 Download deps uses: bahmutov/npm-install@v1 diff --git a/.husky/.gitignore b/.husky/.gitignore new file mode 100644 index 0000000..31354ec --- /dev/null +++ b/.husky/.gitignore @@ -0,0 +1 @@ +_ diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..36af219 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +npx lint-staged diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..cbafbf9 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,4 @@ +build +dist +coverage +flow-typed diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1 @@ +{} diff --git a/README.md b/README.md index aa7aa75..006eb2f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -express-promisified -------------------- +## express-promisified Express with promises @@ -42,4 +41,4 @@ const certificate = fs.readFileSync('./creds/localhost.crt'); ... server.listenHTTPS({ port, privateKey, certificate }) -``` \ No newline at end of file +``` diff --git a/babel.config.json b/babel.config.json index d1ffb75..2d3a6b3 100644 --- a/babel.config.json +++ b/babel.config.json @@ -1,3 +1,3 @@ -{ - "extends": "@krakenjs/grumbler-scripts/config/.babelrc-node" -} \ No newline at end of file +{ + "extends": "@krakenjs/grumbler-scripts/config/.babelrc-node" +} diff --git a/index.d.ts b/index.d.ts index 968c4eb..e9ad93d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,11 +1,11 @@ -import express from 'express'; +import express from "express"; export interface AppServerType { - EVENT: { SHUTDOWN: 'shutdown' }; + EVENT: { SHUTDOWN: "shutdown" }; close: () => Promise; - on: (event: string, handler: () => void) => { cancel: () => void; } ; - listen: (options: {port: number}) => Promise; + on: (event: string, handler: () => void) => { cancel: () => void }; + listen: (options: { port: number }) => Promise; get: (url: string, handler: (req: express.Request, res: express.Response) => Promise | void) => AppServerType; } -export function server(): AppServerType; \ No newline at end of file +export function server(): AppServerType; diff --git a/index.js b/index.js index dc79816..e83406f 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ /* @flow */ // eslint-disable-next-line import/no-commonjs -module.exports = require('./src'); +module.exports = require("./src"); diff --git a/index.test-d.ts b/index.test-d.ts index c96b7c2..e46c0a8 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,5 @@ -import { expectType } from 'tsd'; - -import {AppServerType, server} from '.'; +import { expectType } from "tsd"; +import { AppServerType, server } from "."; const appServer: AppServerType = server(); diff --git a/package.json b/package.json index 11e7e66..0900636 100644 --- a/package.json +++ b/package.json @@ -8,12 +8,15 @@ "flow-typed": "flow-typed install", "flow:build": "flow gen-flow-files ./src/index.js --out-dir ./src", "babel": "babel ./src --ignore=node_modules --out-dir ./src --source-maps inline", - "test": "npm run flow-typed && npm run lint && npm run flow && npm run tsd", + "test": "npm run format:check && npm run flow-typed && npm run lint && npm run flow && npm run tsd", "tsd": "tsd", "flow": "flow", + "format": "prettier --write --ignore-unknown .", + "format:check": "prettier --check .", "lint": "eslint src/ *.js", "prepublish": "in-publish && npm run babel || not-in-publish", - "postpublish": "git checkout ./src" + "postpublish": "git checkout ./src", + "prepare": "husky install" }, "repository": { "type": "git", @@ -31,14 +34,20 @@ }, "homepage": "https://github.com/krakenjs/express-promisified#readme", "devDependencies": { - "@krakenjs/grumbler-scripts": "^6.0.2", + "@krakenjs/grumbler-scripts": "^7.0.0-alpha.1", "@types/express": "^4.17.13", "flow-bin": "^0.69.0", + "husky": "^7.0.4", "in-publish": "^2.0.1", + "lint-staged": "^12.4.0", + "prettier": "^2.6.2", "tsd": "^0.19.1", "yargs-parser": "^10.0.0" }, "peerDependencies": { "express": "^4" + }, + "lint-staged": { + "*": "prettier --write --ignore-unknown" } } diff --git a/src/express.js b/src/express.js index c15a0ce..0b63951 100644 --- a/src/express.js +++ b/src/express.js @@ -1,157 +1,166 @@ /* @flow */ -import https from 'https'; +import https from "https"; -import express from 'express'; +import express from "express"; type ExpressRequest = express$Request; // eslint-disable-line no-undef type ExpressResponse = express$Response; // eslint-disable-line no-undef const EVENT = { - SHUTDOWN: 'shutdown' + SHUTDOWN: "shutdown", }; type Cancelable = { - cancel : () => void + cancel: () => void, }; export type AppServerType = { - EVENT : typeof EVENT, - close : () => Promise, - on : (event : string, handler : () => void) => Cancelable, - emit : (event : string) => void, - listen : (options : { port : number }) => Promise, - get : (url : string, handler : (req : ExpressRequest, res : ExpressResponse) => Promise | void) => AppServerType + EVENT: typeof EVENT, + close: () => Promise, + on: (event: string, handler: () => void) => Cancelable, + emit: (event: string) => void, + listen: (options: { port: number }) => Promise, + get: (url: string, handler: (req: ExpressRequest, res: ExpressResponse) => Promise | void) => AppServerType, }; -export function server() : AppServerType { - - let expressApp = express(); - let expressServer; - - let getExpressServer = () => { - if (!expressServer) { - throw new Error(`Server not started`); - } - return expressServer; - }; - - let shutdownListeners = []; - - const appServer : AppServerType = { - EVENT, - async close() : Promise { - await new Promise((resolve, reject) => { - return getExpressServer().close(err => { - return err ? reject(err) : resolve(); - }); - }); - for (let handler of shutdownListeners) { - await handler(); - } - }, - on(event, handler) : Cancelable { - expressApp.addListener(event, handler); - return { - cancel() { - expressApp.removeListener(event, handler); - } - }; - }, - onShutdown(handler) : Cancelable { - shutdownListeners.push(handler); - return { - cancel() { - shutdownListeners.splice(shutdownListeners.indexOf(handler), 1); - } - }; - }, - use(middleware) : AppServerType { - // $FlowFixMe - expressApp.use(async (req, res, next) => { - try { - await middleware(req, res); - } catch (err) { - return next(err); - } - return next(); - }); - return appServer; - }, - useCallback() : AppServerType { - expressApp.use.apply(expressApp, arguments); - return appServer; +export function server(): AppServerType { + let expressApp = express(); + let expressServer; + + let getExpressServer = () => { + if (!expressServer) { + throw new Error(`Server not started`); + } + return expressServer; + }; + + let shutdownListeners = []; + + const appServer: AppServerType = { + EVENT, + async close(): Promise { + await new Promise((resolve, reject) => { + return getExpressServer().close((err) => { + return err ? reject(err) : resolve(); + }); + }); + for (let handler of shutdownListeners) { + await handler(); + } + }, + on(event, handler): Cancelable { + expressApp.addListener(event, handler); + return { + cancel() { + expressApp.removeListener(event, handler); }, - emit(event) { - expressApp.emit(event); + }; + }, + onShutdown(handler): Cancelable { + shutdownListeners.push(handler); + return { + cancel() { + shutdownListeners.splice(shutdownListeners.indexOf(handler), 1); }, - async listen({ port } : { port : number }) : Promise { - await new Promise((resolve, reject) => { - expressServer = expressApp.listen(port, err => { - if (err) { - return reject(err); - } - if (!expressServer) { - return reject(new Error('No server found')); - } - - // $FlowFixMe - console.log(`Listening on http://localhost:${ port }`); // eslint-disable-line no-console - return resolve(); - }); - }); - - return appServer; - }, - async listenHTTPS({ port, privateKey, certificate } : { port : number, privateKey : string, certificate : string }) : Promise { - await new Promise((resolve, reject) => { - expressServer = https.createServer( - { - key: privateKey, - cert: certificate - }, - expressApp - ).listen(port, err => { - if (err) { - return reject(err); - } - if (!expressServer) { - return reject(new Error('No server found')); - } - - // $FlowFixMe - console.log(`Listening on https://localhost:${ port }`); // eslint-disable-line no-console - return resolve(); - }); - }); + }; + }, + use(middleware): AppServerType { + // $FlowFixMe + expressApp.use(async (req, res, next) => { + try { + await middleware(req, res); + } catch (err) { + return next(err); + } + return next(); + }); + return appServer; + }, + useCallback(): AppServerType { + expressApp.use.apply(expressApp, arguments); + return appServer; + }, + emit(event) { + expressApp.emit(event); + }, + async listen({ port }: { port: number }): Promise { + await new Promise((resolve, reject) => { + expressServer = expressApp.listen(port, (err) => { + if (err) { + return reject(err); + } + if (!expressServer) { + return reject(new Error("No server found")); + } + + // $FlowFixMe + console.log(`Listening on http://localhost:${port}`); // eslint-disable-line no-console + return resolve(); + }); + }); + + return appServer; + }, + async listenHTTPS({ + port, + privateKey, + certificate, + }: { + port: number, + privateKey: string, + certificate: string, + }): Promise { + await new Promise((resolve, reject) => { + expressServer = https + .createServer( + { + key: privateKey, + cert: certificate, + }, + expressApp + ) + .listen(port, (err) => { + if (err) { + return reject(err); + } + if (!expressServer) { + return reject(new Error("No server found")); + } - return appServer; - }, - get(url : string, handler : (ExpressRequest, ExpressResponse) => Promise | void) : AppServerType { // $FlowFixMe - expressApp.get(url, async (req, res) => { - try { - await handler(req, res); - } catch (err) { - console.error(err); // eslint-disable-line no-console - res.status(500).send(`Internal server error`); - } - }); - return appServer; - }, - post(url : string, handler : (ExpressRequest, ExpressResponse) => Promise | void) : AppServerType { - // $FlowFixMe - expressApp.post(url, async (req, res) => { - try { - await handler(req, res); - } catch (err) { - console.error(err); // eslint-disable-line no-console - res.status(500).send(`Internal server error`); - } - }); - return appServer; + console.log(`Listening on https://localhost:${port}`); // eslint-disable-line no-console + return resolve(); + }); + }); + + return appServer; + }, + get(url: string, handler: (ExpressRequest, ExpressResponse) => Promise | void): AppServerType { + // $FlowFixMe + expressApp.get(url, async (req, res) => { + try { + await handler(req, res); + } catch (err) { + console.error(err); // eslint-disable-line no-console + res.status(500).send(`Internal server error`); + } + }); + return appServer; + }, + post(url: string, handler: (ExpressRequest, ExpressResponse) => Promise | void): AppServerType { + // $FlowFixMe + expressApp.post(url, async (req, res) => { + try { + await handler(req, res); + } catch (err) { + console.error(err); // eslint-disable-line no-console + res.status(500).send(`Internal server error`); } - }; + }); + return appServer; + }, + }; - return appServer; + return appServer; } diff --git a/src/index.js b/src/index.js index f9df06d..692724c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,3 @@ /* @flow */ -export * from './express'; +export * from "./express";