Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 27, 2021
1 parent 06b9fa4 commit c640189
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 36 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 6
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
6 changes: 3 additions & 3 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const exitHook = require('.');
import process from 'node:process';
import exitHook from './index.js';

exitHook(() => {
console.log('foo');
Expand All @@ -15,4 +15,4 @@ const unsubscribe = exitHook(() => {

unsubscribe();

process.exit(); // eslint-disable-line unicorn/no-process-exit
process.exit(0); // eslint-disable-line unicorn/no-process-exit
8 changes: 3 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ 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.
@param onExit - The callback function to execute when the process exits.
@returns A function that removes the hook when called.
@example
```
import exitHook = require('exit-hook');
import exitHook from 'exit-hook';
exitHook(() => {
console.log('Exiting');
Expand All @@ -32,6 +32,4 @@ const unsubscribe = exitHook(() => {});
unsubscribe();
```
*/
declare function exitHook(callback: () => void): () => void;

export = exitHook;
export default function exitHook(onExit: () => void): () => void;
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
import process from 'node:process';

const callbacks = new Set();
let isCalled = false;
let isRegistered = false;

function exit(exit, signal) {
function exit(shouldManuallyExit, signal) {
if (isCalled) {
return;
}
Expand All @@ -15,20 +15,20 @@ function exit(exit, signal) {
callback();
}

if (exit === true) {
if (shouldManuallyExit === true) {
process.exit(128 + signal); // eslint-disable-line unicorn/no-process-exit
}
}

module.exports = callback => {
callbacks.add(callback);
export default function exitHook(onExit) {
callbacks.add(onExit);

if (!isRegistered) {
isRegistered = true;

process.once('exit', exit);
process.once('SIGINT', exit.bind(null, true, 2));
process.once('SIGTERM', exit.bind(null, true, 15));
process.once('SIGINT', exit.bind(undefined, true, 2));
process.once('SIGTERM', exit.bind(undefined, true, 15));

// PM2 Cluster shutdown message. Caught to support async handlers with pm2, needed because
// explicitly calling process.exit() doesn't trigger the beforeExit event, and the exit
Expand All @@ -41,6 +41,6 @@ module.exports = callback => {
}

return () => {
callbacks.delete(callback);
callbacks.delete(onExit);
};
};
}
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expectType} from 'tsd';
import exitHook = require('./index.js');
import exitHook from './index.js';

const unsubscribe = exitHook(() => {});
const unsubscribe = exitHook(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function

expectType<() => void>(unsubscribe);
unsubscribe();
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=6"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -37,9 +39,9 @@
"signal"
],
"devDependencies": {
"ava": "^1.4.1",
"execa": "^1.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"execa": "^5.1.1",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install exit-hook
## Usage

```js
const exitHook = require('exit-hook');
import exitHook from 'exit-hook';

exitHook(() => {
console.log('Exiting');
Expand All @@ -35,7 +35,7 @@ throw new Error('🦄');
Removing an exit hook:

```js
const exitHook = require('exit-hook');
import exitHook from 'exit-hook';

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

Expand All @@ -44,15 +44,15 @@ unsubscribe();

## API

### exitHook(callback)
### exitHook(onExit)

Returns a function that removes the hook when called.

#### callback
#### onExit

Type: `Function`

The callback to execute when the process exits.
The callback function to execute when the process exits.

---

Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import process from 'node:process';
import test from 'ava';
import execa from 'execa';
import exitHook from '.';
import exitHook from './index.js';

test('main', async t => {
const {stdout} = await execa(process.execPath, ['fixture.js']);
Expand Down

0 comments on commit c640189

Please sign in to comment.