Skip to content

Commit 78b618d

Browse files
committed
ci(workflow): 添加脚本测试与改进预提交检查
- 在 GitHub Actions CI 工作流中添加 npm run test:scripts 测试步骤 - 在 package.json 中新增 test:scripts 脚本用于运行脚本相关测试 - 优化 husky 预提交钩子,失败时输出提示信息并阻止提交 - 新增 scripts/pre-commit.js 脚本,利用 lint-staged API 实现更严格的预提交检查 - 预提交检查包含 ESLint 自动修复和 Prettier 格式化,确保代码质量
1 parent 004ab3e commit 78b618d

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ jobs:
4848

4949
- name: Test
5050
run: npm test
51+
52+
- name: Test Scripts
53+
run: npm run test:scripts

.husky/pre-commit

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
npx lint-staged
1+
npm run pre-commit || {
2+
echo ''
3+
echo '===================================================='
4+
echo 'pre-commit checks failed. in case of emergency, run:'
5+
echo ''
6+
echo 'git commit --no-verify'
7+
echo '===================================================='
8+
exit 1
9+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
"start": "node scripts/start.js",
2727
"build-and-start": "npm run build && npm run start",
2828
"test": "npm run test --workspaces --if-present",
29+
"test:scripts": "node --test scripts/__tests__/*.test.js",
2930
"release:version": "node scripts/version.js",
3031
"prepare:package": "node scripts/prepare-package.js",
3132
"prepare:vscode": "node scripts/prepare-vscode.js",
3233
"changelog": "node scripts/generate-changelog.js",
33-
"prepare": "husky && npm run build && npm run bundle"
34+
"prepare": "husky && npm run build && npm run bundle",
35+
"pre-commit": "node scripts/pre-commit.js"
3436
},
3537
"devDependencies": {
3638
"@eslint/js": "^9.39.4",

scripts/pre-commit.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* pre-commit hook 脚本
3+
*
4+
* 在 git commit 前自动执行 lint-staged,对暂存区的文件进行:
5+
* - ESLint 检查并自动修复(*.{ts,tsx,js,mjs,cjs,jsx})
6+
* - Prettier 格式化(*.{ts,tsx,js,mjs,cjs,jsx,json} 及 .prettierrc)
7+
*
8+
* 由 .husky/pre-commit 调用,lint-staged 失败时会阻止提交。
9+
*/
10+
11+
import { execSync } from "node:child_process";
12+
import lintStaged from "lint-staged";
13+
14+
try {
15+
// 获取 git 仓库根目录,作为 lint-staged 的工作目录
16+
const root = execSync("git rev-parse --show-toplevel").toString().trim();
17+
18+
// 通过 lint-staged API 直接运行,仅处理暂存区文件
19+
const passed = await lintStaged({ cwd: root });
20+
21+
// lint-staged 全部通过则正常退出,否则以失败码退出阻止提交
22+
process.exit(passed ? 0 : 1);
23+
} catch {
24+
process.exit(1);
25+
}

0 commit comments

Comments
 (0)