@huaiyou/hooks-git 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ pnpm commitlint --edit $1
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ pnpm lint-staged
package/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # @huaiyou/hooks-git
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - fdfb6fc: 首次发布 @huaiyou 系列工具包 v1.0.0 正式版,包含api请求、工程配置、Git钩子、类型定义、UI组件、通用工具等核心能力,适配前端工程化开发全流程
8
+
9
+ ## 0.0.0
10
+
11
+ ### Initial Release
12
+
13
+ - ✨ 集成 Husky 9.x Git 钩子
14
+ - ✨ Commitlint 配置(Conventional Commits 规范)
15
+ - ✨ Lint-staged 自动代码检查和格式化
16
+ - ✨ Pre-commit 和 commit-msg 钩子
17
+ - 📝 完整的使用文档和最佳实践指南
package/README.md ADDED
@@ -0,0 +1,246 @@
1
+ # @huaiyou/hooks-git
2
+
3
+ > Git 钩子配置,包含 Husky、Commitlint 和 Lint-staged,确保代码质量和提交规范。
4
+
5
+ ## 📦 安装
6
+
7
+ ```bash
8
+ pnpm add -D @huaiyou/hooks-git husky @commitlint/cli lint-staged
9
+ ```
10
+
11
+ ## 🚀 快速开始
12
+
13
+ ### 1. 安装 Husky
14
+
15
+ 在项目根目录运行:
16
+
17
+ ```bash
18
+ pnpm prepare
19
+ ```
20
+
21
+ ### 2. 复制配置文件
22
+
23
+ 将以下文件复制到项目根目录:
24
+
25
+ **commitlint.config.js**:
26
+
27
+ ```javascript
28
+ module.exports = require('@huaiyou/hooks-git/commitlint.config');
29
+ ```
30
+
31
+ **lint-staged.config.js**:
32
+
33
+ ```javascript
34
+ module.exports = require('@huaiyou/hooks-git/lint-staged.config');
35
+ ```
36
+
37
+ ### 3. 配置 Husky 钩子
38
+
39
+ **创建 .husky/pre-commit**:
40
+
41
+ ```bash
42
+ #!/usr/bin/env sh
43
+ . "$(dirname -- "$0")/_/husky.sh"
44
+
45
+ pnpm lint-staged
46
+ ```
47
+
48
+ **创建 .husky/commit-msg**:
49
+
50
+ ```bash
51
+ #!/usr/bin/env sh
52
+ . "$(dirname -- "$0")/_/husky.sh"
53
+
54
+ pnpm commitlint --edit $1
55
+ ```
56
+
57
+ ### 4. 添加脚本
58
+
59
+ 在 `package.json` 中:
60
+
61
+ ```json
62
+ {
63
+ "scripts": {
64
+ "prepare": "husky install"
65
+ }
66
+ }
67
+ ```
68
+
69
+ ## ✨ 特性
70
+
71
+ ### Commitlint
72
+
73
+ 强制执行 [Conventional Commits](https://www.conventionalcommits.org/) 规范:
74
+
75
+ - ✅ **feat**: 新功能
76
+ - ✅ **fix**: Bug 修复
77
+ - ✅ **docs**: 文档更新
78
+ - ✅ **style**: 代码格式(不影响代码运行)
79
+ - ✅ **refactor**: 重构
80
+ - ✅ **perf**: 性能优化
81
+ - ✅ **test**: 测试相关
82
+ - ✅ **build**: 构建系统或依赖
83
+ - ✅ **ci**: CI 配置
84
+ - ✅ **chore**: 其他修改
85
+ - ✅ **revert**: 回退提交
86
+
87
+ ### Lint-staged
88
+
89
+ 在提交前自动运行代码检查和格式化:
90
+
91
+ - **TypeScript/JavaScript**: ESLint + Prettier
92
+ - **JSON/Markdown/YAML**: Prettier
93
+
94
+ ## 📝 提交消息格式
95
+
96
+ ### 基本格式
97
+
98
+ ```
99
+ <type>(<scope>): <subject>
100
+
101
+ <body>
102
+
103
+ <footer>
104
+ ```
105
+
106
+ ### 示例
107
+
108
+ ```bash
109
+ # 简单提交
110
+ feat: add user authentication
111
+
112
+ # 带作用域
113
+ feat(auth): implement JWT token validation
114
+
115
+ # 带详细描述
116
+ fix(api): resolve CORS issue in production
117
+
118
+ This fix addresses the CORS error that was preventing
119
+ requests from the frontend in production environment.
120
+
121
+ Closes #123
122
+ ```
123
+
124
+ ### 规则
125
+
126
+ 1. **type**: 必填,见上述类型列表
127
+ 2. **scope**: 可选,表示影响范围
128
+ 3. **subject**: 必填,简短描述(小写开头,不要句号)
129
+ 4. **body**: 可选,详细描述
130
+ 5. **footer**: 可选,关联 Issue 或 Breaking Changes
131
+
132
+ ## 🔧 自定义配置
133
+
134
+ ### 修改 Commitlint 规则
135
+
136
+ 创建 `commitlint.config.js`:
137
+
138
+ ```javascript
139
+ const baseConfig = require('@huaiyou/hooks-git/commitlint.config');
140
+
141
+ module.exports = {
142
+ ...baseConfig,
143
+ rules: {
144
+ ...baseConfig.rules,
145
+ 'subject-max-length': [2, 'always', 100], // 自定义主题长度
146
+ },
147
+ };
148
+ ```
149
+
150
+ ### 修改 Lint-staged 规则
151
+
152
+ 创建 `lint-staged.config.js`:
153
+
154
+ ```javascript
155
+ const baseConfig = require('@huaiyou/hooks-git/lint-staged.config');
156
+
157
+ module.exports = {
158
+ ...baseConfig,
159
+ '*.{ts,tsx}': [
160
+ 'eslint --fix',
161
+ 'prettier --write',
162
+ 'vitest related --run', // 添加测试
163
+ ],
164
+ };
165
+ ```
166
+
167
+ ## 🚫 跳过钩子
168
+
169
+ ### 临时跳过(不推荐)
170
+
171
+ ```bash
172
+ # 跳过 pre-commit 钩子
173
+ git commit --no-verify
174
+
175
+ # 跳过 commit-msg 钩子
176
+ git commit -m "message" --no-verify
177
+ ```
178
+
179
+ ### 禁用钩子
180
+
181
+ ```bash
182
+ # 临时禁用 Husky
183
+ export HUSKY=0
184
+
185
+ # 提交
186
+ git commit -m "message"
187
+
188
+ # 重新启用
189
+ unset HUSKY
190
+ ```
191
+
192
+ ## 📋 最佳实践
193
+
194
+ 1. **原子提交**: 每次提交只做一件事
195
+ 2. **清晰描述**: 提交信息要能让他人理解
196
+ 3. **关联 Issue**: 使用 `Closes #123` 关联问题
197
+ 4. **Breaking Changes**: 重大变更要在 footer 说明
198
+ 5. **定期提交**: 不要积累太多修改
199
+
200
+ ### Breaking Changes 示例
201
+
202
+ ```bash
203
+ feat(api): change authentication method
204
+
205
+ BREAKING CHANGE: The old API key authentication is replaced
206
+ with JWT tokens. All clients must update to use the new
207
+ authentication method.
208
+ ```
209
+
210
+ ## 🔍 故障排除
211
+
212
+ ### Husky 钩子不执行
213
+
214
+ ```bash
215
+ # 重新安装 Husky
216
+ rm -rf .husky
217
+ pnpm prepare
218
+ ```
219
+
220
+ ### Commitlint 不工作
221
+
222
+ ```bash
223
+ # 检查配置文件
224
+ cat commitlint.config.js
225
+
226
+ # 手动测试
227
+ echo "test: message" | pnpm commitlint
228
+ ```
229
+
230
+ ### Lint-staged 不运行
231
+
232
+ ```bash
233
+ # 检查配置
234
+ cat lint-staged.config.js
235
+
236
+ # 手动运行
237
+ pnpm lint-staged
238
+ ```
239
+
240
+ ## 🤝 贡献
241
+
242
+ 欢迎提交 Issue 和 Pull Request!
243
+
244
+ ## 📄 License
245
+
246
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const types = {
6
+ feat: "\u2728 \u65B0\u529F\u80FD",
7
+ fix: "\u{1F41B} \u4FEE\u590D Bug",
8
+ docs: "\u{1F4DA} \u6587\u6863\u66F4\u65B0",
9
+ style: "\u{1F484} \u4EE3\u7801\u683C\u5F0F\uFF08\u4E0D\u5F71\u54CD\u903B\u8F91\uFF09",
10
+ refactor: "\u267B\uFE0F \u91CD\u6784\u4EE3\u7801\uFF08\u4E0D\u5F71\u54CD\u529F\u80FD\uFF09",
11
+ perf: "\u26A1 \u6027\u80FD\u4F18\u5316",
12
+ test: "\u{1F9EA} \u6D4B\u8BD5\u76F8\u5173",
13
+ chore: "\u{1F527} \u5DE5\u5177\u6216\u811A\u624B\u67B6\u53D8\u66F4",
14
+ ci: "\u2699\uFE0F CI/CD \u76F8\u5173",
15
+ build: "\u{1F6E0} \u6784\u5EFA\u7CFB\u7EDF\u6216\u5916\u90E8\u4F9D\u8D56\u53D8\u66F4",
16
+ revert: "\u23EA \u56DE\u6EDA\u63D0\u4EA4",
17
+ update: "\u2B06\uFE0F \u66F4\u65B0\u67D0\u529F\u80FD\uFF08\u975E\u65B0\u529F\u80FD/\u975E\u4FEE\u590D\uFF09"
18
+ };
19
+ const allowTypes = Object.keys(types);
20
+ const rule$2 = (parsed, when, value) => {
21
+ const type = parsed.type;
22
+ if (!type) {
23
+ return [false, "\u2716 \u8BF7\u586B\u5199\u63D0\u4EA4\u7C7B\u578B\uFF08\u5982 feat / fix / docs\uFF09"];
24
+ }
25
+ if (!allowTypes.includes(type)) {
26
+ const list = allowTypes.map((k) => `- ${k}: ${types[k]}`).join("\n");
27
+ return [false, `\u2716 \u63D0\u4EA4\u7C7B\u578B\u4E0D\u5408\u6CD5\uFF0C\u8BF7\u4ECE\u4EE5\u4E0B\u7C7B\u578B\u4E2D\u9009\u62E9\uFF1A
28
+ ${list}`];
29
+ }
30
+ return [true];
31
+ };
32
+
33
+ const rule$1 = (parsed) => {
34
+ const subject = parsed.subject?.trim();
35
+ if (!subject) {
36
+ return [false, "\u2716 \u63D0\u4EA4\u63CF\u8FF0\uFF08subject\uFF09\u4E0D\u80FD\u4E3A\u7A7A\uFF0C\u8BF7\u8865\u5145\u4E00\u53E5\u6E05\u6670\u63CF\u8FF0"];
37
+ }
38
+ return [true];
39
+ };
40
+
41
+ const rule = (parsed, _when, value = 120) => {
42
+ const header = parsed.header || "";
43
+ if (header.length > value) {
44
+ return [false, `\u2716 \u63D0\u4EA4\u4FE1\u606F\u8FC7\u957F\uFF08\u5F53\u524D ${header.length} \u5B57\u7B26\uFF09\uFF0C\u6700\u5927\u5141\u8BB8 ${value} \u5B57\u7B26`];
45
+ }
46
+ return [true];
47
+ };
48
+
49
+ const rules = {
50
+ "zh-type-enum": rule$2,
51
+ "zh-subject-empty": rule$1,
52
+ "zh-header-max-length": rule
53
+ };
54
+ const plugin = {
55
+ rules
56
+ };
57
+ const config = {
58
+ plugins: ["@huaiyou/hooks-git"],
59
+ rules: {
60
+ "zh-type-enum": [2, "always"],
61
+ "zh-subject-empty": [2, "always"],
62
+ "zh-header-max-length": [2, "always", 72],
63
+ // Disable standard rules that we are replacing or don't need
64
+ "type-enum": [0],
65
+ "type-empty": [0],
66
+ "subject-empty": [0],
67
+ "header-max-length": [0]
68
+ }
69
+ };
70
+
71
+ exports.config = config;
72
+ exports.default = plugin;
73
+ exports.rules = rules;
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@huaiyou/hooks-git",
3
+ "version": "1.0.0",
4
+ "description": "Git hooks configuration with Husky, Commitlint and Lint-staged",
5
+ "files": [
6
+ ".husky",
7
+ "*.config.js",
8
+ "README.md",
9
+ "CHANGELOG.md"
10
+ ],
11
+ "main": "./dist/index.cjs",
12
+ "module": "./dist/index.mjs",
13
+ "exports": {
14
+ ".": {
15
+ "require": "./dist/index.cjs",
16
+ "import": "./dist/index.mjs"
17
+ }
18
+ },
19
+ "devDependencies": {
20
+ "@commitlint/types": "^19.8.1",
21
+ "@types/minimatch": "^6.0.0",
22
+ "typescript": "^5.9.3",
23
+ "unbuild": "^3.6.1",
24
+ "undici-types": "^7.16.0",
25
+ "vitest": "^2.1.8",
26
+ "@huaiyou/config-ts": "^1.0.0"
27
+ },
28
+ "dependencies": {
29
+ "minimatch": "^10.1.1"
30
+ },
31
+ "peerDependencies": {
32
+ "@commitlint/lint": ">=7.6.0"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public",
36
+ "registry": "https://registry.npmjs.org/"
37
+ },
38
+ "scripts": {
39
+ "build": "unbuild",
40
+ "dev": "unbuild --stub",
41
+ "test": "vitest run",
42
+ "test:watch": "vitest",
43
+ "test:ui": "vitest --ui"
44
+ }
45
+ }