Skip to content

Commit a31a80e

Browse files
feat: support cjs and esm both by tshy (#8)
BREAKING CHANGE: drop Node.js < 18.19.0 support part of eggjs/egg#3644 eggjs/egg#5257 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced new GitHub Actions workflows for publishing and release processes. - Added TypeScript configuration for stricter type-checking and modern module support. - New exit handler functionality for graceful application termination. - **Documentation** - Updated README.md for clarity on usage and installation instructions. - **Bug Fixes** - Improved error handling and logging in the exit process. - **Chores** - Updated `.gitignore` and `package.json` to reflect new structure and dependencies. - Modified CI pipeline to use updated Node.js versions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 8551bae commit a31a80e

23 files changed

+256
-142
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "eslint-config-egg"
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
36
}

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ jobs:
1212
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1313
with:
1414
os: 'ubuntu-latest, macos-latest'
15-
version: '14, 16, 18, 20, 22'
15+
version: '18.19.0, 18, 20, 22'
1616
secrets:
1717
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/pkg.pr.new.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Publish Any Commit
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v4
11+
12+
- run: corepack enable
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: 20
16+
17+
- name: Install dependencies
18+
run: npm install
19+
20+
- name: Build
21+
run: npm run prepublishOnly --if-present
22+
23+
- run: npx pkg-pr-new publish

.github/workflows/release.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
7+
jobs:
8+
release:
9+
name: Node.js
10+
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
11+
secrets:
12+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
13+
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ run/
77
.DS_Store
88
*.swp
99
.tmp
10+
.tshy*
11+
.eslintcache
12+
dist
13+
coverage

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
# graceful-process
22

3-
graceful exit process even parent exit on SIGKILL.
4-
53
[![NPM version](https://img.shields.io/npm/v/graceful-process.svg?style=flat-square)](https://npmjs.org/package/graceful-process)
64
[![NPM quality](http://npm.packagequality.com/shield/graceful-process.svg?style=flat-square)](http://packagequality.com/#?package=graceful-process)
75
[![NPM download](https://img.shields.io/npm/dm/graceful-process.svg?style=flat-square)](https://npmjs.org/package/graceful-process)
8-
6+
[![Node.js Version](https://img.shields.io/node/v/graceful-process.svg?style=flat)](https://nodejs.org/en/download/)
97
[![Continuous Integration](https://github.com/node-modules/graceful-process/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/graceful-process/actions/workflows/nodejs.yml)
108
[![Test coverage](https://img.shields.io/codecov/c/github/node-modules/graceful-process.svg?style=flat-square)](https://codecov.io/gh/node-modules/graceful-process)
119

10+
graceful exit process even parent exit on SIGKILL.
1211

1312
## Install
1413

1514
```bash
16-
npm i graceful-process --save
15+
npm i graceful-process
1716
```
1817

1918
## Usage
@@ -22,11 +21,19 @@ Require this module and execute it on every child process file.
2221

2322
```js
2423
// mycli.js
25-
require('graceful-process')({ logger: console, label: 'mycli-child-cmd' });
24+
const { graceful } = require('graceful-process');
25+
26+
graceful({ logger: console, label: 'mycli-child-cmd' });
2627
```
2728

2829
## Support
2930

3031
- [x] cluster
3132
- [x] child_process.fork()
3233
- [ ] child_process.spawn()
34+
35+
## Contributors
36+
37+
[![Contributors](https://contrib.rocks/image?repo=node-modules/graceful-process)](https://github.com/node-modules/graceful-process/graphs/contributors)
38+
39+
Made with [contributors-img](https://contrib.rocks).

exit.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

package.json

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22
"name": "graceful-process",
33
"version": "1.2.0",
44
"description": "graceful exit process even parent exit on SIGKILL.",
5-
"files": [
6-
"index.js",
7-
"exit.js"
8-
],
9-
"scripts": {
10-
"lint": "eslint *.js test",
11-
"test": "npm run lint && npm run test-local",
12-
"test-local": "egg-bin test",
13-
"cov": "cross-env COV=true egg-bin cov",
14-
"ci": "npm run lint && npm run cov"
15-
},
165
"repository": {
176
"type": "git",
187
"url": "[email protected]:node-modules/graceful-process.git"
@@ -22,19 +11,59 @@
2211
},
2312
"author": "fengmk2",
2413
"license": "MIT",
25-
"dependencies": {
26-
"once": "^1.4.0",
27-
"promise-timeout": "^1.3.0"
14+
"engines": {
15+
"node": ">= 18.19.0"
2816
},
17+
"dependencies": {},
2918
"devDependencies": {
30-
"coffee": "^4.1.0",
31-
"cross-env": "^5.0.1",
32-
"egg-bin": "^6.13.0",
33-
"eslint": "^4.18.1",
34-
"eslint-config-egg": "^7.0.0",
19+
"@arethetypeswrong/cli": "^0.17.1",
20+
"@eggjs/tsconfig": "1",
21+
"@types/mocha": "10",
22+
"@types/node": "22",
23+
"coffee": "^5.5.1",
24+
"egg-bin": "6",
25+
"eslint": "8",
26+
"eslint-config-egg": "14",
3527
"fkill": "^7.2.1",
36-
"mm": "^2.2.0",
37-
"mz-modules": "^2.1.0",
38-
"urllib": "^2.26.0"
39-
}
28+
"mm": "^3.4.0",
29+
"tshy": "3",
30+
"tshy-after": "1",
31+
"typescript": "5",
32+
"urllib": "^4.6.8"
33+
},
34+
"scripts": {
35+
"lint": "eslint --cache src test --ext .ts",
36+
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
37+
"test": "egg-bin test",
38+
"preci": "npm run lint && npm run prepublishOnly",
39+
"ci": "egg-bin cov && attw --pack",
40+
"prepublishOnly": "tshy && tshy-after"
41+
},
42+
"type": "module",
43+
"tshy": {
44+
"exports": {
45+
".": "./src/index.ts",
46+
"./package.json": "./package.json"
47+
}
48+
},
49+
"exports": {
50+
".": {
51+
"import": {
52+
"types": "./dist/esm/index.d.ts",
53+
"default": "./dist/esm/index.js"
54+
},
55+
"require": {
56+
"types": "./dist/commonjs/index.d.ts",
57+
"default": "./dist/commonjs/index.js"
58+
}
59+
},
60+
"./package.json": "./package.json"
61+
},
62+
"files": [
63+
"dist",
64+
"src"
65+
],
66+
"types": "./dist/commonjs/index.d.ts",
67+
"main": "./dist/commonjs/index.js",
68+
"module": "./dist/esm/index.js"
4069
}

src/exit.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import assert from 'node:assert';
2+
import { setTimeout } from 'node:timers/promises';
3+
import type { Logger, BeforeExit } from './types.js';
4+
5+
const TIMEOUT = Symbol('before exit timeout');
6+
7+
export function getExitFunction(logger: Logger, label: string, timeout: number, beforeExit?: BeforeExit) {
8+
if (beforeExit) {
9+
assert(typeof beforeExit === 'function', 'beforeExit only support function');
10+
}
11+
12+
// only call beforeExit once
13+
let called = false;
14+
const handler = async () => {
15+
if (called) return;
16+
called = true;
17+
if (beforeExit) {
18+
await beforeExit();
19+
}
20+
};
21+
22+
return async function exitFunction(code: number) {
23+
try {
24+
const result = await Promise.race([
25+
handler(),
26+
setTimeout(timeout, TIMEOUT),
27+
]);
28+
if (result === TIMEOUT) {
29+
throw new Error(`Timeout ${timeout}ms`);
30+
}
31+
logger.info('[%s] beforeExit success', label);
32+
process.exit(code);
33+
} catch (err: any) {
34+
logger.error('[%s] beforeExit fail, error: %s', label, err.message);
35+
process.exit(code);
36+
}
37+
};
38+
}

0 commit comments

Comments
 (0)