diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.travis.yml b/.travis.yml index e0cc348..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js node_js: + - '10' - '8' - '6' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f7aed97 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,37 @@ +/** +Run some code when the process exits. + +The `process.on('exit')` event doesn't catch all the ways a process can exit. + +This package is useful for cleaning up before exiting. + +@param callback - The callback to execute when the process exits. +@returns A function that removes the hook when called. + +@example +``` +import exitHook = require('exit-hook'); + +exitHook(() => { + console.log('Exiting'); +}); + +// You can add multiple hooks, even across files +exitHook(() => { + console.log('Exiting 2'); +}); + +throw new Error('🦄'); + +//=> 'Exiting' +//=> 'Exiting 2' + +// Removing an exit hook: +const unsubscribe = exitHook(() => {}); + +unsubscribe(); +``` +*/ +declare function exitHook(callback: () => void): () => void; + +export = exitHook; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..bf4525f --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from 'tsd'; +import exitHook = require('.'); + +const unsubscribe = exitHook(() => {}); + +expectType<() => void>(unsubscribe); +unsubscribe(); diff --git a/package.json b/package.json index ee042fa..5a4d4ac 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "exit", @@ -35,8 +36,9 @@ "signal" ], "devDependencies": { - "ava": "*", - "execa": "^0.10.0", - "xo": "*" + "ava": "^1.4.1", + "execa": "^1.0.0", + "tsd": "^0.7.2", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index 9629570..782ffb2 100644 --- a/readme.md +++ b/readme.md @@ -47,6 +47,19 @@ unsubscribe(); ``` +## API + +### exitHook(callback) + +Returns a function that removes the hook when called. + +#### callback + +Type: `Function` + +The callback to execute when the process exits. + + ## License MIT © [Sindre Sorhus](https://sindresorhus.com)