standards-cli 1.0.5 → 1.0.8

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 CHANGED
@@ -107,7 +107,7 @@ standards init [选项]
107
107
  ```json
108
108
  {
109
109
  "scripts": {
110
- "prepare": "husky install",
110
+ "prepare": "husky",
111
111
  "cz": "cz",
112
112
  "commit": "cz"
113
113
  },
@@ -115,6 +115,11 @@ standards init [选项]
115
115
  "commitizen": {
116
116
  "path": "cz-git"
117
117
  }
118
+ },
119
+ "pnpm": {
120
+ "overrides": {
121
+ "string-width": "^7.0.0"
122
+ }
118
123
  }
119
124
  }
120
125
  ```
@@ -132,7 +137,23 @@ standards init [选项]
132
137
 
133
138
  初始化完成后,可以使用以下两种方式提交:
134
139
 
135
- ### 方式一:交互式提交(推荐)
140
+ ### 方式一:符合规范格式的直接提交(推荐)
141
+
142
+ ```bash
143
+ git commit -m "feat(login): 完成登录相关功能"
144
+ git commit -m "fix(user): 修复用户信息更新bug"
145
+ ```
146
+
147
+ **格式要求:** `type(scope): subject`
148
+
149
+ - `type`: 提交类型(feat/fix/docs 等)
150
+ - `scope`: 模块名称(必填,如 login、user、api 等)
151
+ - `subject`: 简短描述
152
+
153
+ ✅ **只要符合格式,就能正常提交**
154
+ ❌ **不符合格式会被 commit-msg hook 拦截**
155
+
156
+ ### 方式二:交互式提交
136
157
 
137
158
  ```bash
138
159
  pnpm commit
@@ -142,24 +163,24 @@ pnpm cz
142
163
 
143
164
  使用交互式界面选择提交类型、填写描述等,自动生成符合规范的提交信息。
144
165
 
145
- **交互流程:**
166
+ ## 团队成员使用流程
146
167
 
147
- 1. 选择提交类型(feat/fix/docs/...)
148
- 2. **填写 scope(对应代码模块)** - 必填,手动输入如 `auth`
149
- 3. 填写简短描述(subject)- 必填
150
- 4. 确认提交
151
-
152
- **最终提交信息格式:** `feat (auth): add login feature`
153
-
154
- ### 方式二:直接提交
168
+ 团队成员克隆项目后,只需执行:
155
169
 
156
170
  ```bash
157
- git commit -m "feat: add new feature"
171
+ # 1. 安装依赖(会自动执行 prepare 脚本,初始化 Git hooks)
172
+ pnpm install
173
+
174
+ # 2. 之后正常提交即可(符合规范就能提交,不符合会被拦截)
175
+ git commit -m "feat(login): 完成登录相关功能"
158
176
  ```
159
177
 
160
- 直接使用符合规范的提交信息格式提交,commit-msg hook 会自动验证。
178
+ **说明:**
161
179
 
162
- **注意:** 无论使用哪种方式,commit-msg hook 都会拦截不符合规范的提交信息。
180
+ - `pnpm install` 会自动触发 `prepare` 脚本
181
+ - `prepare` 脚本执行 `husky`,自动配置 Git hooks
182
+ - 配置完成后,每次 commit 都会自动检查提交信息格式
183
+ - **无需额外操作**,开箱即用
163
184
 
164
185
  ## lint-staged 智能配置
165
186
 
@@ -219,24 +240,24 @@ CLI 会自动检测项目是否已安装 `eslint` 和 `prettier`,并生成对
219
240
  **示例:**
220
241
 
221
242
  ```bash
222
- # 正确
223
- git commit -m "feat (auth): add login"
224
- git commit -m "fix (api): resolve login bug"
225
- git commit -m "docs: update README"
226
-
227
- # 错误(会被拦截)
228
- git commit -m "add login"
229
- git commit -m "fix bug"
230
- git commit -m "update"
231
- git commit -m "feat: add login" # 缺少 scope
243
+ # ✅ 正确格式(能提交)
244
+ git commit -m "feat(login): 完成登录相关功能"
245
+ git commit -m "fix(api): 修复接口超时问题"
246
+ git commit -m "docs(readme): 更新文档"
247
+
248
+ # ❌ 错误格式(会被拦截)
249
+ git commit -m "add login" # 缺少 type 和 scope
250
+ git commit -m "feat: add login" # 缺少 scope
251
+ git commit -m "update" # 格式不符合
232
252
  ```
233
253
 
234
254
  ## 注意事项
235
255
 
236
- 1. **eslint/prettier 依赖**:本 CLI 不安装 eslint 和 prettier,请根据项目需要自行安装
237
- 2. **lint-staged 配置**:CLI 会自动检测并生成合适的配置
238
- 3. **Node 版本**:需要 Node.js >= 18
239
- 4. **提交方式**:`git commit` 和 `pnpm commit` 都会被 commit-msg hook 拦截验证
256
+ 1. **团队成员使用**:只需 `pnpm install`,会自动初始化 Git hooks
257
+ 2. **提交方式**:`git commit -m "feat(scope): message"` 只要符合格式就能提交
258
+ 3. **eslint/prettier 依赖**:本 CLI 不安装 eslint 和 prettier,请根据项目需要自行安装
259
+ 4. **lint-staged 配置**:CLI 会自动检测并生成合适的配置
260
+ 5. **Node 版本**:需要 Node.js >= 18
240
261
 
241
262
  ## 开发
242
263
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "standards-cli",
3
- "version": "1.0.5",
3
+ "version": "1.0.8",
4
4
  "description": "一键初始化前端项目提交规范链路(cz-git + commitlint + husky + lint-staged)",
5
5
  "type": "module",
6
6
  "bin": {
@@ -14,7 +14,10 @@
14
14
  "templates"
15
15
  ],
16
16
  "scripts": {
17
- "test": "node src/index.js init"
17
+ "test": "node src/index.js init",
18
+ "prepare": "husky || exit 0",
19
+ "cz": "cz",
20
+ "commit": "cz"
18
21
  },
19
22
  "keywords": [
20
23
  "commitlint",
@@ -38,5 +41,23 @@
38
41
  },
39
42
  "engines": {
40
43
  "node": ">=18"
44
+ },
45
+ "config": {
46
+ "commitizen": {
47
+ "path": "cz-git"
48
+ }
49
+ },
50
+ "pnpm": {
51
+ "overrides": {
52
+ "string-width": "^7.0.0"
53
+ }
54
+ },
55
+ "devDependencies": {
56
+ "@commitlint/cli": "^20.3.1",
57
+ "@commitlint/config-conventional": "^20.3.1",
58
+ "commitizen": "^4.3.1",
59
+ "cz-git": "^1.12.0",
60
+ "husky": "^9.1.7",
61
+ "lint-staged": "^16.2.7"
41
62
  }
42
63
  }
@@ -163,14 +163,16 @@ async function generateConfigFiles(pkg) {
163
163
  await ensureDir(".husky");
164
164
 
165
165
  // .husky/pre-commit
166
- const preCommitContent = `npx --no-install lint-staged
166
+ const preCommitContent = `#!/bin/sh
167
+ npx --no-install lint-staged
167
168
  `;
168
169
  await writeFile(".husky/pre-commit", preCommitContent);
169
170
  await chmodSafe(".husky/pre-commit");
170
171
  console.log(" ✔ .husky/pre-commit");
171
172
 
172
173
  // .husky/commit-msg
173
- const commitMsgContent = `npx --no-install commitlint --edit "$1"
174
+ const commitMsgContent = `#!/bin/sh
175
+ npx --no-install commitlint --edit "$1"
174
176
  `;
175
177
  await writeFile(".husky/commit-msg", commitMsgContent);
176
178
  await chmodSafe(".husky/commit-msg");
@@ -194,8 +196,12 @@ async function updatePackageJson() {
194
196
 
195
197
  // 添加 prepare(如果不存在)
196
198
  if (!pkg.scripts.prepare) {
197
- pkg.scripts.prepare = "husky install";
198
- console.log(' ✔ 添加 scripts.prepare = "husky install"');
199
+ pkg.scripts.prepare = "husky";
200
+ console.log(' ✔ 添加 scripts.prepare = "husky"');
201
+ modified = true;
202
+ } else if (pkg.scripts.prepare === "husky install" || pkg.scripts.prepare === "husky init") {
203
+ pkg.scripts.prepare = "husky";
204
+ console.log(' ✔ 更新 scripts.prepare = "husky"');
199
205
  modified = true;
200
206
  } else {
201
207
  console.log(" ⚠ scripts.prepare (已存在,跳过)");
@@ -236,6 +242,23 @@ async function updatePackageJson() {
236
242
  console.log(" ⚠ config.commitizen.path (已存在,跳过)");
237
243
  }
238
244
 
245
+ // 添加 pnpm overrides 来降级 string-width(兼容 Node.js 18)
246
+ if (!pkg.pnpm) {
247
+ pkg.pnpm = {};
248
+ }
249
+ if (!pkg.pnpm.overrides) {
250
+ pkg.pnpm.overrides = {};
251
+ }
252
+ if (pkg.pnpm.overrides["string-width"] !== "^7.0.0") {
253
+ pkg.pnpm.overrides["string-width"] = "^7.0.0";
254
+ console.log(
255
+ ' ✔ 添加 pnpm.overrides.string-width = "^7.0.0" (兼容 Node.js 18)'
256
+ );
257
+ modified = true;
258
+ } else {
259
+ console.log(" ⚠ pnpm.overrides.string-width (已存在,跳过)");
260
+ }
261
+
239
262
  if (modified) {
240
263
  await writeJson("package.json", pkg);
241
264
  }
@@ -269,17 +292,19 @@ async function installDependencies(pm, missingDeps) {
269
292
  }
270
293
 
271
294
  /**
272
- * 执行 husky install
295
+ * 执行 husky init
273
296
  */
274
297
  async function runHuskyInstall(pm) {
275
298
  console.log("\n🔧 初始化 husky...");
276
- const command = getExecCommand(pm, "husky install");
277
- console.log(` 执行: ${command}`);
278
- const code = await execLive(command);
279
- if (code !== 0) {
280
- throw new Error(`husky install 失败,退出码: ${code}`);
299
+
300
+ // 设置 Git hooks 路径为 .husky
301
+ const gitConfigCmd = "git config core.hooksPath .husky";
302
+ console.log(` 执行: ${gitConfigCmd}`);
303
+ const gitCode = await execLive(gitConfigCmd);
304
+ if (gitCode !== 0) {
305
+ throw new Error(`设置 Git hooks 路径失败,退出码: ${gitCode}`);
281
306
  }
282
- console.log(" ✔ husky 初始化成功");
307
+ console.log(" ✔ Git hooks 路径已设置为 .husky");
283
308
  }
284
309
 
285
310
  /**
@@ -310,7 +335,7 @@ function printInstallCommand(missingDeps, defaultPm) {
310
335
  const marker = pm === defaultPm ? " [推荐]" : "";
311
336
  console.log(` ${command}${marker}`);
312
337
  }
313
- console.log("\n安装后请执行: husky install\n");
338
+ console.log("\n安装后请运行 prepare 脚本来初始化 hooks(npm install 会自动执行)\n");
314
339
  }
315
340
 
316
341
  /**