Skip to content

Commit

Permalink
Add TypeScript definition (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 16, 2019
1 parent 79c3bce commit 09ebd60
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
37 changes: 37 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd';
import exitHook = require('.');

const unsubscribe = exitHook(() => {});

expectType<() => void>(unsubscribe);
unsubscribe();
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"exit",
Expand All @@ -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"
}
}
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 09ebd60

Please sign in to comment.