nsbp-cli 0.2.26 → 0.2.29
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.
- package/README.md +1 -1
- package/bin/nsbp.js +94 -74
- package/package.json +1 -1
- package/templates/basic/README.md +43 -0
- package/templates/basic/docs/DEVELOPMENT_GUIDE.md +290 -0
- package/templates/basic/docs/ESLINT_AND_PRETTIER.md +184 -0
- package/templates/basic/docs/HUSKY_9_UPGRADE.md +76 -0
- package/templates/basic/docs/HUSKY_ESLINT_SETUP.md +293 -0
- package/templates/basic/docs/SETUP_GIT_HOOKS.md +106 -0
- package/templates/basic/gitignore +3 -0
- package/templates/basic/package.json +27 -3
- package/templates/basic/scripts/setup-husky.js +24 -0
- package/templates/basic/src/Routers.tsx +4 -5
- package/templates/basic/src/client/index.tsx +6 -2
- package/templates/basic/src/component/Header.tsx +10 -10
- package/templates/basic/src/component/Layout.tsx +10 -4
- package/templates/basic/src/component/Theme.tsx +6 -2
- package/templates/basic/src/containers/Home.tsx +142 -77
- package/templates/basic/src/containers/Login.tsx +3 -3
- package/templates/basic/src/containers/Photo.tsx +37 -25
- package/templates/basic/src/externals/window.d.ts +3 -1
- package/templates/basic/src/reducers/home.ts +1 -1
- package/templates/basic/src/reducers/index.ts +1 -1
- package/templates/basic/src/reducers/photo.ts +8 -3
- package/templates/basic/src/server/index.ts +35 -26
- package/templates/basic/src/server/photo.ts +14 -7
- package/templates/basic/src/server/utils.tsx +13 -15
- package/templates/basic/src/services/home.ts +3 -28
- package/templates/basic/src/services/photo.ts +30 -32
- package/templates/basic/src/store/constants.ts +1 -1
- package/templates/basic/src/store/index.ts +3 -2
- package/templates/basic/src/styled/component/header.ts +5 -1
- package/templates/basic/src/styled/home.ts +100 -24
- package/templates/basic/src/styled/photo.ts +2 -2
- package/templates/basic/src/utils/config.ts +1 -1
- package/templates/basic/src/utils/fetch.ts +4 -8
- package/templates/basic/src/utils/index.ts +1 -1
- package/templates/basic/tsconfig.json +6 -1
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# ESLint & Prettier 配置指南
|
|
2
|
+
|
|
3
|
+
本项目使用 ESLint + Prettier 进行代码风格检查和格式化。
|
|
4
|
+
|
|
5
|
+
## 📦 已安装的包
|
|
6
|
+
|
|
7
|
+
```json
|
|
8
|
+
{
|
|
9
|
+
"eslint": "^9.0.0",
|
|
10
|
+
"eslint-config-prettier": "^9.0.0",
|
|
11
|
+
"eslint-plugin-react": "^7.37.0",
|
|
12
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
|
13
|
+
"husky": "^9.0.0",
|
|
14
|
+
"lint-staged": "^15.0.0",
|
|
15
|
+
"prettier": "^3.3.0",
|
|
16
|
+
"prettier-plugin-organize-imports": "^4.0.0",
|
|
17
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
18
|
+
"@typescript-eslint/parser": "^8.0.0"
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 🔧 配置文件
|
|
23
|
+
|
|
24
|
+
### ESLint
|
|
25
|
+
- `.eslintrc.js` - ESLint 配置
|
|
26
|
+
- `.eslintignore` - ESLint 忽略规则
|
|
27
|
+
|
|
28
|
+
### Prettier
|
|
29
|
+
- `.prettierrc.js` - Prettier 配置
|
|
30
|
+
|
|
31
|
+
### Husky
|
|
32
|
+
- `.husky/pre-commit` - 提交前钩子
|
|
33
|
+
- `.husky/pre-push` - 推送前钩子
|
|
34
|
+
- `.husky/commit-msg` - 提交信息验证
|
|
35
|
+
|
|
36
|
+
## 📝 可用命令
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# Lint 检查(不修复)
|
|
40
|
+
pnpm run lint
|
|
41
|
+
|
|
42
|
+
# Lint 检查并自动修复
|
|
43
|
+
pnpm run lint:fix
|
|
44
|
+
|
|
45
|
+
# 格式化代码
|
|
46
|
+
pnpm run format
|
|
47
|
+
|
|
48
|
+
# 同时 lint 和格式化(Git 钩子自动执行)
|
|
49
|
+
pnpm run lint-staged
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## 🔍 ESLint 规则
|
|
53
|
+
|
|
54
|
+
### TypeScript 规则
|
|
55
|
+
- `@typescript-eslint/no-explicit-any`: 警告 - 允许 `any` 类型
|
|
56
|
+
- `@typescript-eslint/no-unused-vars`: 警告 - 未使用的变量(允许 `_` 开头)
|
|
57
|
+
|
|
58
|
+
### React 规则
|
|
59
|
+
- `react/react-in-jsx-scope`: 关闭 - 不需要显式 React 导入
|
|
60
|
+
- `react/prop-types`: 关闭 - 使用 TypeScript 类型系统
|
|
61
|
+
- `react-hooks/rules-of-hooks`: 错误 - 遵循 React Hooks 规则
|
|
62
|
+
- `react-hooks/exhaustive-deps`: 警告 - Hooks 依赖项检查
|
|
63
|
+
|
|
64
|
+
### 其他规则
|
|
65
|
+
- `prettier/prettier`: 错误 - 代码风格必须符合 Prettier 配置
|
|
66
|
+
- `no-console`: 警告 - 允许 `console.warn` 和 `console.error`
|
|
67
|
+
|
|
68
|
+
## 🎨 Prettier 配置
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
{
|
|
72
|
+
semi: false, // 不使用分号
|
|
73
|
+
singleQuote: true, // 使用单引号
|
|
74
|
+
tabWidth: 2, // 2 空格缩进
|
|
75
|
+
trailingComma: 'es5', // ES5 尾部逗号
|
|
76
|
+
printWidth: 100, // 每行最多 100 字符
|
|
77
|
+
arrowParens: 'always', // 箭头函数总是加括号
|
|
78
|
+
endOfLine: 'lf', // Unix 换行符
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## 📁 忽略的文件
|
|
83
|
+
|
|
84
|
+
ESLint 会忽略以下目录和文件:
|
|
85
|
+
|
|
86
|
+
- `node_modules/`
|
|
87
|
+
- `build/`
|
|
88
|
+
- `dist/`
|
|
89
|
+
- `.temp_cache/`
|
|
90
|
+
- `public/js/`
|
|
91
|
+
- `public/css/`
|
|
92
|
+
- `*.bundle.js`
|
|
93
|
+
- `loadable-stats.json`
|
|
94
|
+
- `coverage/`
|
|
95
|
+
- Webpack 配置文件(`*.config.js`)
|
|
96
|
+
|
|
97
|
+
## 🚨 常见问题
|
|
98
|
+
|
|
99
|
+
### 1. Git 钩子阻止提交
|
|
100
|
+
|
|
101
|
+
**问题**:提交代码时,Git 钩子失败
|
|
102
|
+
|
|
103
|
+
**解决方案**:
|
|
104
|
+
```bash
|
|
105
|
+
# 查看错误信息
|
|
106
|
+
pnpm run lint
|
|
107
|
+
|
|
108
|
+
# 自动修复
|
|
109
|
+
pnpm run lint:fix
|
|
110
|
+
|
|
111
|
+
# 重新提交
|
|
112
|
+
git add .
|
|
113
|
+
git commit -m "fix: resolve linting issues"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 2. TypeScript 类型错误
|
|
117
|
+
|
|
118
|
+
**问题**:ESLint 报告类型错误
|
|
119
|
+
|
|
120
|
+
**解决方案**:
|
|
121
|
+
- 确保项目已编译:`pnpm run build:server`
|
|
122
|
+
- 重启 TypeScript 服务器(TS Language Server)
|
|
123
|
+
|
|
124
|
+
### 3. Prettier 格式冲突
|
|
125
|
+
|
|
126
|
+
**问题**:手动格式化后,ESLint 仍然报错
|
|
127
|
+
|
|
128
|
+
**解决方案**:
|
|
129
|
+
```bash
|
|
130
|
+
# 使用 Prettier 重新格式化
|
|
131
|
+
pnpm run format
|
|
132
|
+
|
|
133
|
+
# 确保编辑器使用 Prettier
|
|
134
|
+
# VSCode: 安装 Prettier 插件,启用 "Format on Save"
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### 4. 跳过 Git 钩子(不推荐)
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
git commit --no-verify -m "your commit message"
|
|
141
|
+
git push --no-verify
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
⚠️ **警告**:仅在紧急情况下使用!
|
|
145
|
+
|
|
146
|
+
## 🎯 编辑器配置
|
|
147
|
+
|
|
148
|
+
### VSCode
|
|
149
|
+
|
|
150
|
+
推荐安装扩展:
|
|
151
|
+
- ESLint
|
|
152
|
+
- Prettier - Code formatter
|
|
153
|
+
- TypeScript Importer
|
|
154
|
+
|
|
155
|
+
项目根目录创建 `.vscode/settings.json`:
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"editor.formatOnSave": true,
|
|
160
|
+
"editor.codeActionsOnSave": {
|
|
161
|
+
"source.fixAll.eslint": "explicit"
|
|
162
|
+
},
|
|
163
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
|
164
|
+
"[typescript]": {
|
|
165
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
166
|
+
},
|
|
167
|
+
"[typescriptreact]": {
|
|
168
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
169
|
+
},
|
|
170
|
+
"eslint.validate": [
|
|
171
|
+
"javascript",
|
|
172
|
+
"javascriptreact",
|
|
173
|
+
"typescript",
|
|
174
|
+
"typescriptreact"
|
|
175
|
+
]
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## 📚 参考资料
|
|
180
|
+
|
|
181
|
+
- [ESlint 文档](https://eslint.org/docs/latest/)
|
|
182
|
+
- [Prettier 文档](https://prettier.io/docs/en/options)
|
|
183
|
+
- [React Hooks 规则](https://react-hooks.vercel.app/)
|
|
184
|
+
- [Conventional Commits](https://www.conventionalcommits.org/)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Husky 9.x 升级说明
|
|
2
|
+
|
|
3
|
+
## ℹ️ 关于 "husky install command is DEPRECATED" 警告
|
|
4
|
+
|
|
5
|
+
这是正常的!Husky 9.x 版本已经改变了安装方式。
|
|
6
|
+
|
|
7
|
+
### 变化说明
|
|
8
|
+
|
|
9
|
+
**Husky 8.x (旧版本)**:
|
|
10
|
+
- 需要手动运行 `husky install` 命令
|
|
11
|
+
- `prepare` 脚本:`"prepare": "husky install"`
|
|
12
|
+
|
|
13
|
+
**Husky 9.x (新版本)**:
|
|
14
|
+
- ✅ Hooks 在 `pnpm install` 时自动创建
|
|
15
|
+
- ✅ 不需要手动运行 `husky install`
|
|
16
|
+
- ✅ `prepare` 脚本:`"prepare": "husky"`(仅注册命令)
|
|
17
|
+
|
|
18
|
+
### 如何工作
|
|
19
|
+
|
|
20
|
+
运行 `pnpm install` 时:
|
|
21
|
+
1. Husky 自动创建 `.husky` 目录
|
|
22
|
+
2. 自动生成 Git hooks(pre-commit, pre-push, commit-msg)
|
|
23
|
+
3. 设置 Git hooks 路径:`git config core.hooksPath .husky`
|
|
24
|
+
|
|
25
|
+
### 当前配置
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"prepare": "husky" // 正确!注册 Husky 命令
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 验证安装
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
# 检查 hooks 是否存在
|
|
37
|
+
ls .husky
|
|
38
|
+
# 应该看到: pre-commit, pre-push, commit-msg
|
|
39
|
+
|
|
40
|
+
# 检查 hooks 是否有执行权限
|
|
41
|
+
ls -la .husky/
|
|
42
|
+
# 应该看到: -rwxr-xr-x
|
|
43
|
+
|
|
44
|
+
# 测试提交
|
|
45
|
+
git commit -m "test: verify hooks"
|
|
46
|
+
# 应该自动运行 lint-staged
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 无需操作
|
|
50
|
+
|
|
51
|
+
这个警告可以安全忽略!🎉 你的项目已经正确配置了 Husky 9.x。
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## 🔄 升级步骤(如需)
|
|
56
|
+
|
|
57
|
+
如果你之前使用的是 Husky 8.x,需要:
|
|
58
|
+
|
|
59
|
+
1. **更新 package.json**:
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"prepare": "husky" // 从 "husky install" 改为 "husky"
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
2. **重新安装依赖**:
|
|
67
|
+
```bash
|
|
68
|
+
pnpm install
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
3. **验证 hooks**:
|
|
72
|
+
```bash
|
|
73
|
+
ls .husky
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
完成!Husky 9.x 现在可以正常工作。
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# Husky 和 ESLint 配置总结
|
|
2
|
+
|
|
3
|
+
> **Husky 9.x 升级说明**:Husky 9.x 简化了安装方式,无需手动运行 `husky install`。Hooks 在 `pnpm install` 时自动创建。
|
|
4
|
+
|
|
5
|
+
## ✅ 已完成的配置
|
|
6
|
+
|
|
7
|
+
### 1. 创建的配置文件
|
|
8
|
+
|
|
9
|
+
#### ESLint 配置
|
|
10
|
+
- **`.eslintrc.js`** - ESLint 主配置文件
|
|
11
|
+
- TypeScript 支持(@typescript-eslint/parser)
|
|
12
|
+
- React 支持(eslint-plugin-react)
|
|
13
|
+
- React Hooks 支持(eslint-plugin-react-hooks)
|
|
14
|
+
- Prettier 集成(eslint-config-prettier)
|
|
15
|
+
|
|
16
|
+
- **`.eslintignore`** - ESLint 忽略规则
|
|
17
|
+
- 忽略构建产物(build, dist, public)
|
|
18
|
+
- 忽略缓存(.temp_cache)
|
|
19
|
+
- 忽略 Webpack 配置文件
|
|
20
|
+
|
|
21
|
+
#### Prettier 配置
|
|
22
|
+
- **`.prettierrc.js`** - Prettier 格式化配置
|
|
23
|
+
- 2 空格缩进
|
|
24
|
+
- 单引号
|
|
25
|
+
- 无分号
|
|
26
|
+
- 100 字符换行
|
|
27
|
+
- Unix 换行符
|
|
28
|
+
- import 组织(prettier-plugin-organize-imports)
|
|
29
|
+
|
|
30
|
+
#### Husky Git Hooks
|
|
31
|
+
- **`.husky/pre-commit`** - 提交前钩子
|
|
32
|
+
- 运行 `pnpm run lint-staged`
|
|
33
|
+
- 自动修复和格式化暂存文件
|
|
34
|
+
|
|
35
|
+
- **`.husky/pre-push`** - 推送前钩子
|
|
36
|
+
- 运行完整 lint 检查
|
|
37
|
+
- 防止推送有问题的代码
|
|
38
|
+
|
|
39
|
+
- **`.husky/commit-msg`** - 提交信息验证
|
|
40
|
+
- 验证 Conventional Commits 格式
|
|
41
|
+
- 支持的类型:feat, fix, docs, style, refactor, test, chore, build, ci, perf, revert
|
|
42
|
+
|
|
43
|
+
- **`.husky/README.md`** - Git hooks 使用说明
|
|
44
|
+
|
|
45
|
+
#### VSCode 配置
|
|
46
|
+
- **`.vscode/settings.json`** - 编辑器设置
|
|
47
|
+
- 保存时自动格式化
|
|
48
|
+
- 保存时自动修复 ESLint 问题
|
|
49
|
+
- 配置 Prettier 作为默认格式化器
|
|
50
|
+
- TypeScript 和 React 文件支持
|
|
51
|
+
|
|
52
|
+
- **`.vscode/extensions.json`** - 推荐扩展
|
|
53
|
+
- ESLint
|
|
54
|
+
- Prettier
|
|
55
|
+
- TypeScript Importer
|
|
56
|
+
- Error Lens
|
|
57
|
+
|
|
58
|
+
- **`.vscode/.gitignore`** - Git 忽略规则
|
|
59
|
+
- 允许提交 settings.json 和扩展配置
|
|
60
|
+
- 忽略 workspace 配置
|
|
61
|
+
|
|
62
|
+
### 2. 更新的文件
|
|
63
|
+
|
|
64
|
+
#### package.json
|
|
65
|
+
- **添加的依赖**:
|
|
66
|
+
```json
|
|
67
|
+
{
|
|
68
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
69
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
70
|
+
"eslint": "^9.0.0",
|
|
71
|
+
"eslint-config-prettier": "^9.0.0",
|
|
72
|
+
"eslint-plugin-react": "^7.37.0",
|
|
73
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
|
74
|
+
"husky": "^9.0.0",
|
|
75
|
+
"lint-staged": "^15.0.0",
|
|
76
|
+
"prettier-plugin-organize-imports": "^4.0.0"
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- **添加的脚本**:
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"lint": "eslint src --ext .ts,.tsx,.js,.jsx",
|
|
84
|
+
"lint:fix": "eslint src --ext .ts,.tsx,.js,.jsx --fix",
|
|
85
|
+
"prepare": "husky"
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
- **添加的 lint-staged 配置**:
|
|
90
|
+
```json
|
|
91
|
+
{
|
|
92
|
+
"*.{ts,tsx,js,jsx}": [
|
|
93
|
+
"eslint --fix",
|
|
94
|
+
"prettier --write"
|
|
95
|
+
],
|
|
96
|
+
"*.{css,less,scss}": [
|
|
97
|
+
"prettier --write"
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
#### .gitignore
|
|
103
|
+
- 添加了 `.husky/_` 忽略规则(Husky 内部文件)
|
|
104
|
+
|
|
105
|
+
#### README.md
|
|
106
|
+
- 添加了快速开始部分
|
|
107
|
+
- 添加了开发工具说明
|
|
108
|
+
- 添加了文档链接
|
|
109
|
+
|
|
110
|
+
### 3. 创建的文档
|
|
111
|
+
|
|
112
|
+
- **`docs/ESLINT_AND_PRETTIER.md`** - ESLint 和 Prettier 详细配置说明
|
|
113
|
+
- **`docs/SETUP_GIT_HOOKS.md`** - Git hooks 配置和使用说明
|
|
114
|
+
- **`docs/DEVELOPMENT_GUIDE.md`** - 完整开发指南
|
|
115
|
+
- **`docs/HUSKY_ESLINT_SETUP.md`** - 本文档
|
|
116
|
+
|
|
117
|
+
### 4. 创建的辅助脚本
|
|
118
|
+
|
|
119
|
+
- **`scripts/setup-husky.js`** - Husky 初始化脚本(备用)
|
|
120
|
+
|
|
121
|
+
## 📦 依赖版本
|
|
122
|
+
|
|
123
|
+
| 包名 | 版本 | 用途 |
|
|
124
|
+
|------|------|------|
|
|
125
|
+
| eslint | ^9.0.0 | 代码质量检查 |
|
|
126
|
+
| eslint-config-prettier | ^9.0.0 | Prettier 集成 |
|
|
127
|
+
| eslint-plugin-react | ^7.37.0 | React 规则 |
|
|
128
|
+
| eslint-plugin-react-hooks | ^5.0.0 | React Hooks 规则 |
|
|
129
|
+
| @typescript-eslint/eslint-plugin | ^8.0.0 | TypeScript 规则 |
|
|
130
|
+
| @typescript-eslint/parser | ^8.0.0 | TypeScript 解析 |
|
|
131
|
+
| prettier | ^3.3.0 | 代码格式化 |
|
|
132
|
+
| prettier-plugin-organize-imports | ^4.0.0 | import 排序 |
|
|
133
|
+
| husky | ^9.0.0 | Git hooks |
|
|
134
|
+
| lint-staged | ^15.0.0 | 暂存文件处理 |
|
|
135
|
+
|
|
136
|
+
## 🔧 ESLint 规则配置
|
|
137
|
+
|
|
138
|
+
### 主要规则
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
{
|
|
142
|
+
'prettier/prettier': 'error', // Prettier 冲突报错
|
|
143
|
+
'react/react-in-jsx-scope': 'off', // 不需要 React 导入
|
|
144
|
+
'react/prop-types': 'off', // 使用 TypeScript
|
|
145
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
146
|
+
'@typescript-eslint/no-explicit-any': 'warn', // 允许 any
|
|
147
|
+
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
|
|
148
|
+
'react-hooks/rules-of-hooks': 'error', // Hooks 规则
|
|
149
|
+
'react-hooks/exhaustive-deps': 'warn', // 依赖检查
|
|
150
|
+
'no-console': ['warn', { allow: ['warn', 'error'] }] // 允许 console
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 🎨 Prettier 配置
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
{
|
|
158
|
+
semi: false, // 不使用分号
|
|
159
|
+
singleQuote: true, // 使用单引号
|
|
160
|
+
tabWidth: 2, // 2 空格缩进
|
|
161
|
+
trailingComma: 'es5', // ES5 尾部逗号
|
|
162
|
+
printWidth: 100, // 100 字符换行
|
|
163
|
+
arrowParens: 'always', // 箭头函数加括号
|
|
164
|
+
endOfLine: 'lf', // Unix 换行符
|
|
165
|
+
plugins: ['prettier-plugin-organize-imports'],
|
|
166
|
+
importOrder: ['^react', '^@/(.*)$', '^[./]'],
|
|
167
|
+
importOrderSeparation: true,
|
|
168
|
+
importOrderSortSpecifiers: true
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## 🔄 Git Hooks 工作流程
|
|
173
|
+
|
|
174
|
+
### 提交前(pre-commit)
|
|
175
|
+
|
|
176
|
+
1. `lint-staged` 检测暂存文件
|
|
177
|
+
2. 对 TypeScript/JS 文件运行 `eslint --fix`
|
|
178
|
+
3. 对所有文件运行 `prettier --write`
|
|
179
|
+
4. 重新暂存修复后的文件
|
|
180
|
+
5. 如果有错误,阻止提交
|
|
181
|
+
|
|
182
|
+
### 推送前(pre-push)
|
|
183
|
+
|
|
184
|
+
1. 运行完整 `pnpm run lint`
|
|
185
|
+
2. 如果有错误,阻止推送
|
|
186
|
+
|
|
187
|
+
### 提交信息验证(commit-msg)
|
|
188
|
+
|
|
189
|
+
1. 检查提交信息格式:`<type>(<scope>): <subject>`
|
|
190
|
+
2. 支持的类型:feat, fix, docs, style, refactor, test, chore, build, ci, perf, revert
|
|
191
|
+
3. 如果格式不正确,阻止提交并显示帮助信息
|
|
192
|
+
|
|
193
|
+
## 📝 提交信息示例
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
# 功能
|
|
197
|
+
git commit -m "feat(core): add SSR data preloading support"
|
|
198
|
+
|
|
199
|
+
# Bug 修复
|
|
200
|
+
git commit -m "fix(client): resolve hydration mismatch error"
|
|
201
|
+
|
|
202
|
+
# 文档
|
|
203
|
+
git commit -m "docs(readme): update installation instructions"
|
|
204
|
+
|
|
205
|
+
# 样式
|
|
206
|
+
git commit -m "style: format code with prettier"
|
|
207
|
+
|
|
208
|
+
# 重构
|
|
209
|
+
git commit -m "refactor(components): simplify layout component"
|
|
210
|
+
|
|
211
|
+
# 测试
|
|
212
|
+
git commit -m "test(add): add unit tests for utils"
|
|
213
|
+
|
|
214
|
+
# 构建
|
|
215
|
+
git commit -m "build(webpack): optimize production bundle"
|
|
216
|
+
|
|
217
|
+
# CI
|
|
218
|
+
git commit -m "ci(github): update workflow configuration"
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## 🚀 使用方法
|
|
222
|
+
|
|
223
|
+
### 初始化(首次使用)
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
# 1. 安装依赖
|
|
227
|
+
pnpm install
|
|
228
|
+
|
|
229
|
+
# 2. 初始化 Git hooks(会自动运行 prepare 脚本)
|
|
230
|
+
pnpm run prepare
|
|
231
|
+
|
|
232
|
+
# 3. 验证 hooks 安装
|
|
233
|
+
ls .husky
|
|
234
|
+
# 应该看到: pre-commit, pre-push, commit-msg
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### 日常开发
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
# 开发
|
|
241
|
+
pnpm run dev
|
|
242
|
+
|
|
243
|
+
# 修改代码...
|
|
244
|
+
|
|
245
|
+
# 提交(hooks 自动运行)
|
|
246
|
+
git add .
|
|
247
|
+
git commit -m "feat: add new feature"
|
|
248
|
+
# pre-commit 自动运行: eslint --fix + prettier --write
|
|
249
|
+
|
|
250
|
+
# 如果有错误
|
|
251
|
+
pnpm run lint:fix
|
|
252
|
+
git add .
|
|
253
|
+
git commit --amend
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### 跳过 hooks(不推荐)
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
# 跳过 pre-commit
|
|
260
|
+
git commit --no-verify -m "message"
|
|
261
|
+
|
|
262
|
+
# 跳过 pre-push
|
|
263
|
+
git push --no-verify
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
⚠️ **警告**: 仅在紧急情况下使用!
|
|
267
|
+
|
|
268
|
+
## 📚 相关文档
|
|
269
|
+
|
|
270
|
+
- [README.md](../README.md) - 项目总览
|
|
271
|
+
- [ESLINT_AND_PRETTIER.md](./ESLINT_AND_PRETTIER.md) - ESLint 和 Prettier 详细配置
|
|
272
|
+
- [SETUP_GIT_HOOKS.md](./SETUP_GIT_HOOKS.md) - Git hooks 使用说明
|
|
273
|
+
- [DEVELOPMENT_GUIDE.md](./DEVELOPMENT_GUIDE.md) - 完整开发指南
|
|
274
|
+
|
|
275
|
+
## ✨ 特性
|
|
276
|
+
|
|
277
|
+
- ✅ TypeScript + React 完整支持
|
|
278
|
+
- ✅ 自动代码格式化(保存时自动运行)
|
|
279
|
+
- ✅ 提交前自动 lint 和格式化
|
|
280
|
+
- ✅ 提交信息格式验证
|
|
281
|
+
- ✅ VSCode 集成(自动格式化 + 修复)
|
|
282
|
+
- ✅ import 自动排序
|
|
283
|
+
- ✅ 支持 Conventional Commits
|
|
284
|
+
- ✅ React Hooks 规则检查
|
|
285
|
+
|
|
286
|
+
## 🎯 下一步建议
|
|
287
|
+
|
|
288
|
+
- [ ] 添加单元测试框架(Jest 或 Vitest)
|
|
289
|
+
- [ ] 配置 CI/CD 流程
|
|
290
|
+
- [ ] 添加代码覆盖率报告
|
|
291
|
+
- [ ] 配置自动化部署
|
|
292
|
+
- [ ] 添加 Commitlint 更严格的提交规范
|
|
293
|
+
- [ ] 配置 Release Please 自动化版本发布
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Git Hooks Setup
|
|
2
|
+
|
|
3
|
+
This project uses **Husky** for managing Git hooks.
|
|
4
|
+
|
|
5
|
+
## Quick Setup
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Install dependencies
|
|
9
|
+
pnpm install
|
|
10
|
+
|
|
11
|
+
# Initialize Git hooks (optional, runs automatically on prepare)
|
|
12
|
+
pnpm run prepare
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## How It Works
|
|
16
|
+
|
|
17
|
+
Husky automatically creates Git hooks in the `.husky` directory:
|
|
18
|
+
|
|
19
|
+
- **pre-commit**: Runs linting on staged files
|
|
20
|
+
- **pre-push**: Runs full lint check before pushing
|
|
21
|
+
- **commit-msg**: Validates commit message format
|
|
22
|
+
|
|
23
|
+
## Commit Message Format
|
|
24
|
+
|
|
25
|
+
Follow the [Conventional Commits](https://www.conventionalcommits.org/) specification:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
<type>(<scope>): <subject>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Type Options
|
|
32
|
+
|
|
33
|
+
- `feat`: A new feature
|
|
34
|
+
- `fix`: A bug fix
|
|
35
|
+
- `docs`: Documentation only changes
|
|
36
|
+
- `style`: Code style changes (formatting, etc.)
|
|
37
|
+
- `refactor`: Code change that neither fixes a bug nor adds a feature
|
|
38
|
+
- `test`: Adding missing tests or correcting existing tests
|
|
39
|
+
- `chore`: Maintenance tasks
|
|
40
|
+
- `build`: Changes that affect the build system
|
|
41
|
+
- `ci`: Changes to CI configuration files
|
|
42
|
+
- `perf`: Performance improvements
|
|
43
|
+
- `revert`: Reverts a previous commit
|
|
44
|
+
|
|
45
|
+
### Examples
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Feature
|
|
49
|
+
git commit -m "feat(core): add SSR data preloading support"
|
|
50
|
+
|
|
51
|
+
# Bug fix
|
|
52
|
+
git commit -m "fix(client): resolve hydration mismatch error"
|
|
53
|
+
|
|
54
|
+
# Documentation
|
|
55
|
+
git commit -m "docs(readme): update installation instructions"
|
|
56
|
+
|
|
57
|
+
# Style (formatting)
|
|
58
|
+
git commit -m "style: format code with prettier"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Available Scripts
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
# Lint all files
|
|
65
|
+
pnpm run lint
|
|
66
|
+
|
|
67
|
+
# Lint and fix issues
|
|
68
|
+
pnpm run lint:fix
|
|
69
|
+
|
|
70
|
+
# Format code
|
|
71
|
+
pnpm run format
|
|
72
|
+
|
|
73
|
+
# Reinstall Git hooks
|
|
74
|
+
pnpm run prepare
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Troubleshooting
|
|
78
|
+
|
|
79
|
+
### Hooks not working?
|
|
80
|
+
|
|
81
|
+
1. Make sure hooks are executable:
|
|
82
|
+
```bash
|
|
83
|
+
chmod +x .husky/*
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
2. Reinstall hooks:
|
|
87
|
+
```bash
|
|
88
|
+
pnpm run prepare
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
3. Check `.git/config` for hooks path:
|
|
92
|
+
```bash
|
|
93
|
+
git config core.hooksPath
|
|
94
|
+
```
|
|
95
|
+
Should output: `.husky`
|
|
96
|
+
|
|
97
|
+
### Skip hooks (not recommended)
|
|
98
|
+
|
|
99
|
+
If you need to bypass hooks temporarily:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
git commit --no-verify
|
|
103
|
+
git push --no-verify
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
⚠️ **Warning**: Use `--no-verify` only in emergencies!
|