Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition #12

Merged
merged 2 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)