diff --git a/index.d.ts b/index.d.ts index 83bdecc..698e05f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -type SignalListener = (done: () => void, event: any, signal: string) => (undefined | Promise) +type SignalListener = (done: () => void, event: any, signal: string) => (undefined | Promise | Promise | Promise) type cancelSubscription = () => void diff --git a/index.js b/index.js index bb5aaaa..137484d 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ // +----------------------------------------------------------------------+ -// | node-graceful v0.3.0 (https://github.com/mrbar42/node-graceful) | +// | node-graceful v0.3.1 (https://github.com/mrbar42/node-graceful) | // | Graceful process exit manager. | // |----------------------------------------------------------------------| 'use strict'; @@ -170,3 +170,7 @@ Graceful.prototype._invokeListener = function _invokeListener(listener, quit, ev let graceful = new Graceful(); module.exports = graceful; + +// monkey patch exports to support both old & new & Typescript module systems +module.exports.Graceful = graceful; +module.exports.default = graceful; diff --git a/package-lock.json b/package-lock.json index 1a95f45..efb2938 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,9 +1,15 @@ { "name": "node-graceful", - "version": "0.3.0", + "version": "0.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/node": { + "version": "9.6.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-9.6.0.tgz", + "integrity": "sha512-h3YZbOq2+ZoDFI1z8Zx0Ck/xRWkOESVaLdgLdd/c25mMQ1Y2CAkILu9ny5A15S5f32gGcQdaUIZ2jzYr8D7IFg==", + "dev": true + }, "ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", @@ -25,6 +31,12 @@ "sprintf-js": "1.0.3" } }, + "arrify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/arrify/-/arrify-1.0.1.tgz", + "integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=", + "dev": true + }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -222,6 +234,12 @@ "esprima": "4.0.0" } }, + "make-error": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.4.tgz", + "integrity": "sha512-0Dab5btKVPhibSalc9QGXb559ED7G7iLjFXBaj9Wq8O3vorueR5K5jaE3hkG6ZQINyhA/JgG6Qk4qdFQjsYV6g==", + "dev": true + }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", @@ -231,6 +249,29 @@ "brace-expansion": "1.1.8" } }, + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + }, + "dependencies": { + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + } + } + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -267,6 +308,21 @@ "integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==", "dev": true }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + }, + "source-map-support": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.4.tgz", + "integrity": "sha512-PETSPG6BjY1AHs2t64vS2aqAgu6dMIMXJULWFBGbh2Gr8nVLbCFDo6i/RMMvviIQ2h1Z8+5gQhVKSn2je9nmdg==", + "dev": true, + "requires": { + "source-map": "0.6.1" + } + }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -288,6 +344,22 @@ "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", "dev": true }, + "ts-node": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-5.0.1.tgz", + "integrity": "sha512-XK7QmDcNHVmZkVtkiwNDWiERRHPyU8nBqZB1+iv2UhOG0q3RQ9HsZ2CMqISlFbxjrYFGfG2mX7bW4dAyxBVzUw==", + "dev": true, + "requires": { + "arrify": "1.0.1", + "chalk": "2.3.0", + "diff": "3.4.0", + "make-error": "1.3.4", + "minimist": "1.2.0", + "mkdirp": "0.5.1", + "source-map-support": "0.5.4", + "yn": "2.0.0" + } + }, "tslib": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.0.tgz", @@ -324,9 +396,9 @@ } }, "typescript": { - "version": "2.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.6.2.tgz", - "integrity": "sha1-PFtv1/beCRQmkCfwPAlGdY92c6Q=", + "version": "2.7.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.7.2.tgz", + "integrity": "sha512-p5TCYZDAO0m4G344hD+wx/LATebLWZNkkh2asWUFqSsD2OrDNhbAHuSjobrmsUmdzjJjEeZVU9g1h3O6vpstnw==", "dev": true }, "wrappy": { @@ -334,6 +406,12 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true + }, + "yn": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yn/-/yn-2.0.0.tgz", + "integrity": "sha1-5a2ryKz0CPY4X8dklWhMiOavaJo=", + "dev": true } } } diff --git a/package.json b/package.json index 2bd59b2..8a2cb4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-graceful", - "version": "0.3.0", + "version": "0.3.1", "description": "Graceful process exit manager. allows waiting on multiple async services.", "main": "index", "repository": { @@ -18,15 +18,18 @@ ], "scripts": { "test:unit": "node tests/runner", - "test:lint": "tslint -c tslint.json --fix tests/*.ts", - "test": "npm run test:unit && npm run test:lint", + "test:unit-ts": "ts-node tests/wait-for-multiple-promise.ts", + "test:lint": "tsc && tslint -c tslint.json --fix tests/*.ts", + "test": "npm run test:lint && npm run test:unit && npm run test:unit-ts", "transpile": "tsc --allowJs --outFile es5/index.js --pretty index.js ", - "prepublish": "npm run test && npm run transpile" + "prepublishOnly": "npm run test && npm run transpile" }, "author": "mrbar42", "license": "WTFPL", "devDependencies": { + "@types/node": "9.6.0", + "ts-node": "5.0.1", "tslint": "5.9.1", - "typescript": "2.6.2" + "typescript": "2.7.2" } } diff --git a/tests/TypeScript.ts b/tests/TypeScript.ts index 37b3fa1..c7c3af1 100644 --- a/tests/TypeScript.ts +++ b/tests/TypeScript.ts @@ -1,12 +1,18 @@ -import {Graceful} from "../"; +import Graceful from "../"; -const listener = (done, event, signal) => { +const listener = (done: () => {}, event: any, signal: string) => { done(); }; -const listenerPromise = (done, event, signal) => Promise.resolve(); - +const listenerPromise = (done: () => {}, event: any, signal: string) => Promise.resolve("abc"); Graceful.on("exit", listener); +const listenerPromise1 = (done: () => {}, event: any, signal: string) => Promise.resolve({}); +Graceful.on("exit", listenerPromise1); +const listenerPromise2 = (done: () => {}, event: any, signal: string) => new Promise((resolve) => { + resolve(); +}); +Graceful.on("exit", listenerPromise2); + Graceful.on("exit", listener, true); Graceful.on("exit", listenerPromise); Graceful.on("exit", listenerPromise, true); diff --git a/tests/wait-for-multiple-promise.ts b/tests/wait-for-multiple-promise.ts new file mode 100644 index 0000000..64b85c3 --- /dev/null +++ b/tests/wait-for-multiple-promise.ts @@ -0,0 +1,26 @@ +"use strict"; + +import Graceful from "../"; + +const handlerSuccess = () => { + return new Promise((resolve) => { + setTimeout(() => { + process.stdout.write("ok"); + resolve(); + }, 100); + }); +}; + +const handlerReject = () => { + return new Promise((resolve, reject) => { + setTimeout(() => { + process.stdout.write("ok"); + reject(); + }, 200); + }); +}; + +Graceful.on("exit", handlerSuccess); +Graceful.on("exit", handlerReject); + +Graceful.exit(); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..63cee0a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "target": "es2015", + "noImplicitAny": true, + "strictNullChecks": true, + "noEmit": true, + "alwaysStrict": true, + "preserveConstEnums": true, + "sourceMap": false, + "moduleResolution": "Node", + "declaration": true, + "lib": [ + "es2015" + ] + } +} diff --git a/tslint.json b/tslint.json index 32fa6e5..7d89339 100644 --- a/tslint.json +++ b/tslint.json @@ -1,9 +1,6 @@ { - "defaultSeverity": "error", - "extends": [ - "tslint:recommended" - ], - "jsRules": {}, - "rules": {}, - "rulesDirectory": [] -} \ No newline at end of file + "defaultSeverity": "error", + "extends": [ + "tslint:recommended" + ] +}