Skip to content

Commit a2aa78a

Browse files
authored
feat: use read-env-value (#11)
node-modules/read-env-value#2 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced pre-commit hooks to enforce linting and formatting on staged files. - Added configuration files for OXLint and Prettier to standardize code quality and style. - Added a runtime dependency for safer environment variable handling. - **Bug Fixes** - Improved error handling and type safety in log message processing. - **Refactor** - Replaced ESLint with OXLint and switched from Egg.js test runner to Vitest. - Modernized imports and assertion styles in tests. - Updated code formatting and logging consistency throughout the codebase. - **Chores** - Updated and added dependencies, including test and linting tools. - Expanded Node.js version support in CI and improved workflow formatting. - Updated README usage examples to use ES module syntax. - Simplified TypeScript configuration. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 90661bc commit a2aa78a

File tree

18 files changed

+378
-144
lines changed

18 files changed

+378
-144
lines changed

.eslintrc

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

.github/workflows/nodejs.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ name: CI
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [master]
66
pull_request:
7-
branches: [ master ]
7+
branches: [master]
88

99
jobs:
1010
Job:
1111
name: Node.js
1212
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1313
with:
1414
os: 'ubuntu-latest, macos-latest'
15-
version: '18.19.0, 18, 20, 22'
15+
version: '18.19.0, 18, 20, 22, 24'
1616
secrets:
1717
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Release
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [master]
66

77
jobs:
88
release:

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx lint-staged

.oxlintrc.json

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{
2+
"$schema": "./node_modules/oxlint/configuration_schema.json",
3+
"env": {
4+
"node": true,
5+
"mocha": true
6+
},
7+
"categories": {
8+
"correctness": "error",
9+
"perf": "error",
10+
"nursery": "error",
11+
"restriction": "error",
12+
"style": "error",
13+
"pedantic": "error",
14+
"suspicious": "error"
15+
},
16+
"plugins": [
17+
"import",
18+
"typescript",
19+
"unicorn",
20+
"jsdoc",
21+
"node",
22+
"promise",
23+
"oxc"
24+
],
25+
"rules": {
26+
// eslint
27+
"constructor-super": "error",
28+
"getter-return": "error",
29+
"no-undef": "error",
30+
"no-unreachable": "error",
31+
"no-var": "error",
32+
"no-eq-null": "error",
33+
"no-await-in-loop": "allow",
34+
"eqeqeq": ["error", "smart"],
35+
"init-declarations": "allow",
36+
"curly": "allow",
37+
"no-ternary": "allow",
38+
"max-params": ["error", 5],
39+
"no-await-expression-member": "error",
40+
"no-continue": "allow",
41+
"guard-for-in": "allow",
42+
"func-style": "allow",
43+
"sort-imports": "allow",
44+
"yoda": "allow",
45+
"sort-keys": "allow",
46+
"no-magic-numbers": "allow",
47+
"no-duplicate-imports": "error",
48+
"no-multi-assign": "error",
49+
"func-names": "error",
50+
"default-param-last": "error",
51+
"prefer-object-spread": "error",
52+
"no-undefined": "allow",
53+
"no-plusplus": "allow",
54+
"no-console": "allow",
55+
"no-extraneous-class": "allow",
56+
"no-empty-function": "allow",
57+
"max-depth": ["error", 6],
58+
"max-lines-per-function": "allow",
59+
"no-lonely-if": "error",
60+
"max-lines": "allow",
61+
"require-await": "allow",
62+
"max-nested-callbacks": ["error", 5],
63+
"max-classes-per-file": "allow",
64+
"radix": "allow",
65+
"no-negated-condition": "error",
66+
"no-else-return": "error",
67+
"no-throw-literal": "error",
68+
69+
// import
70+
"import/exports-last": "allow",
71+
"import/max-dependencies": "allow",
72+
"import/no-cycle": "error",
73+
"import/no-anonymous-default-export": "allow",
74+
"import/no-namespace": "error",
75+
"import/named": "error",
76+
"import/export": "error",
77+
"import/no-default-export": "allow",
78+
"import/unambiguous": "error",
79+
"import/group-exports": "allow",
80+
81+
// promise
82+
"promise/no-return-wrap": "error",
83+
"promise/param-names": "error",
84+
"promise/prefer-await-to-callbacks": "error",
85+
"promise/prefer-await-to-then": "error",
86+
"promise/prefer-catch": "error",
87+
"promise/no-return-in-finally": "error",
88+
"promise/avoid-new": "error",
89+
90+
// unicorn
91+
"unicorn/error-message": "error",
92+
"unicorn/no-null": "allow",
93+
"unicorn/filename-case": "allow",
94+
"unicorn/prefer-structured-clone": "error",
95+
"unicorn/prefer-logical-operator-over-ternary": "error",
96+
"unicorn/prefer-number-properties": "error",
97+
"unicorn/prefer-array-some": "error",
98+
"unicorn/prefer-string-slice": "error",
99+
// "unicorn/no-null": "error",
100+
"unicorn/throw-new-error": "error",
101+
"unicorn/catch-error-name": "allow",
102+
"unicorn/prefer-spread": "allow",
103+
"unicorn/numeric-separators-style": "error",
104+
"unicorn/prefer-string-raw": "error",
105+
"unicorn/text-encoding-identifier-case": "error",
106+
"unicorn/no-array-for-each": "error",
107+
"unicorn/explicit-length-check": "error",
108+
"unicorn/no-lonely-if": "error",
109+
"unicorn/no-useless-undefined": "allow",
110+
"unicorn/prefer-date-now": "error",
111+
"unicorn/no-static-only-class": "allow",
112+
"unicorn/no-typeof-undefined": "error",
113+
"unicorn/prefer-negative-index": "error",
114+
"unicorn/no-anonymous-default-export": "allow",
115+
116+
// oxc
117+
"oxc/no-map-spread": "error",
118+
"oxc/no-rest-spread-properties": "allow",
119+
"oxc/no-optional-chaining": "allow",
120+
"oxc/no-async-await": "allow",
121+
122+
// typescript
123+
"typescript/explicit-function-return-type": "allow",
124+
"typescript/consistent-type-imports": "error",
125+
"typescript/consistent-type-definitions": "error",
126+
"typescript/consistent-indexed-object-style": "allow",
127+
"typescript/no-inferrable-types": "error",
128+
"typescript/array-type": "error",
129+
"typescript/no-non-null-assertion": "error",
130+
"typescript/no-explicit-any": "error",
131+
"typescript/no-import-type-side-effects": "error",
132+
"typescript/no-dynamic-delete": "error",
133+
"typescript/prefer-ts-expect-error": "error",
134+
"typescript/ban-ts-comment": "error",
135+
"typescript/prefer-enum-initializers": "error",
136+
137+
// jsdoc
138+
"jsdoc/require-returns": "allow",
139+
"jsdoc/require-param": "allow"
140+
},
141+
"ignorePatterns": ["index.d.ts", "test/fixtures/**", "__snapshots__"]
142+
}

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CHANGELOG.md
2+
__snapshots__

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"tabWidth": 2,
5+
"arrowParens": "avoid"
6+
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ npm i graceful-process
1717

1818
## Usage
1919

20-
Require this module and execute it on every child process file.
20+
Import this module and execute it on every child process file.
2121

22-
```js
23-
// mycli.js
24-
const { graceful } = require('graceful-process');
22+
```ts
23+
// mycli.ts
24+
import { graceful } from 'graceful-process';
2525

2626
graceful({ logger: console, label: 'mycli-child-cmd' });
2727
```

package.json

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,43 @@
1414
"engines": {
1515
"node": ">= 18.19.0"
1616
},
17-
"dependencies": {},
17+
"dependencies": {
18+
"read-env-value": "^1.1.0"
19+
},
1820
"devDependencies": {
19-
"@arethetypeswrong/cli": "^0.17.1",
20-
"@eggjs/tsconfig": "1",
21-
"@types/mocha": "10",
21+
"@arethetypeswrong/cli": "^0.18.1",
22+
"@eggjs/tsconfig": "2",
2223
"@types/node": "22",
23-
"coffee": "^5.5.1",
24-
"egg-bin": "6",
25-
"eslint": "8",
26-
"eslint-config-egg": "14",
27-
"fkill": "^7.2.1",
28-
"mm": "^3.4.0",
24+
"@vitest/coverage-v8": "3",
25+
"coffee": "5",
26+
"fkill": "9",
27+
"husky": "9",
28+
"lint-staged": "15",
29+
"mm": "4",
30+
"oxlint": "^0.16.10",
31+
"prettier": "3",
2932
"tshy": "3",
3033
"tshy-after": "1",
3134
"typescript": "5",
32-
"urllib": "^4.6.8"
35+
"urllib": "4",
36+
"vitest": "3"
3337
},
3438
"scripts": {
35-
"lint": "eslint --cache src test --ext .ts",
39+
"lint": "oxlint",
3640
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
37-
"test": "egg-bin test",
41+
"test": "vitest run --testTimeout=30000",
3842
"preci": "npm run lint && npm run prepublishOnly",
39-
"ci": "egg-bin cov && attw --pack",
40-
"prepublishOnly": "tshy && tshy-after"
43+
"ci": "vitest run --testTimeout=30000 --coverage",
44+
"postci": "npm run prepublishOnly",
45+
"prepublishOnly": "tshy && tshy-after && attw --pack",
46+
"prepare": "husky"
47+
},
48+
"lint-staged": {
49+
"*": "prettier --write --ignore-unknown --cache",
50+
"*.{ts,js,json,md,yml}": [
51+
"prettier --ignore-unknown --write",
52+
"oxlint --fix"
53+
]
4154
},
4255
"type": "module",
4356
"tshy": {

src/exit.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import assert from 'node:assert';
22
import { setTimeout } from 'node:timers/promises';
3+
34
import type { Logger, BeforeExit } from './types.js';
45

56
const TIMEOUT = Symbol('before exit timeout');
67

7-
export function getExitFunction(logger: Logger, label: string, timeout: number, beforeExit?: BeforeExit) {
8+
export function getExitFunction(
9+
logger: Logger,
10+
label: string,
11+
timeout: number,
12+
beforeExit?: BeforeExit
13+
) {
814
if (beforeExit) {
9-
assert(typeof beforeExit === 'function', 'beforeExit only support function');
15+
assert.ok(
16+
typeof beforeExit === 'function',
17+
'beforeExit only support function'
18+
);
1019
}
1120

1221
// only call beforeExit once
@@ -29,9 +38,12 @@ export function getExitFunction(logger: Logger, label: string, timeout: number,
2938
throw new Error(`Timeout ${timeout}ms`);
3039
}
3140
logger.info('[%s] beforeExit success', label);
41+
// oxlint-disable-next-line unicorn/no-process-exit
3242
process.exit(code);
33-
} catch (err: any) {
34-
logger.error('[%s] beforeExit fail, error: %s', label, err.message);
43+
} catch (err: unknown) {
44+
const message = err instanceof Error ? err.message : String(err);
45+
logger.error('[%s] beforeExit fail, error: %s', label, message);
46+
// oxlint-disable-next-line unicorn/no-process-exit
3547
process.exit(code);
3648
}
3749
};

0 commit comments

Comments
 (0)