@pubinfo/commitlint 2.1.8 → 2.1.10-beta.1

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/dist/index.d.mts CHANGED
@@ -20,9 +20,33 @@ declare function createGitMessage(rootPath?: string): boolean;
20
20
  declare function runGitMessage(rootPath?: string): boolean;
21
21
  //#endregion
22
22
  //#region src/init/simple.git.hook.d.ts
23
+ /**
24
+ * 配置 simple-git-hooks 到 package.json
25
+ * 注意:此函数只写入配置,不会删除配置
26
+ */
23
27
  declare function runSimpleGitHooks(script?: string, cwd?: string, enabled?: boolean): boolean;
28
+ /**
29
+ * 执行 npx simple-git-hooks
30
+ * 会根据 package.json 中的配置安装 hooks 到 .git/hooks/
31
+ */
24
32
  declare function runNpx(cwd?: string): boolean;
25
- declare function configureGitHooksPath(enabled?: boolean, cwd?: string): boolean;
33
+ /**
34
+ * 清理已安装的 git hooks 文件
35
+ * 只删除 .git/hooks 下的 hook 文件,不会修改 package.json
36
+ */
37
+ declare function removeGitHooks(cwd?: string): boolean;
38
+ /**
39
+ * 禁用 git hooks(不修改 package.json 配置)
40
+ * 1. 清理已安装的 hooks 文件
41
+ * 2. 清除 core.hooksPath 配置(如果有)
42
+ */
43
+ declare function disableCommitlint(cwd?: string): boolean;
44
+ /**
45
+ * 启用 git hooks
46
+ * 1. 清除残留的 hooksPath 配置
47
+ * 2. 执行 npx simple-git-hooks 重新安装 hooks
48
+ */
49
+ declare function enableCommitlint(cwd?: string): boolean;
26
50
  //#endregion
27
51
  //#region src/prompt/index.d.ts
28
52
  interface PromptResult {
@@ -40,4 +64,4 @@ declare function isInsideGitRepo(): boolean;
40
64
  declare function lintMessage(message: string): Promise<boolean>;
41
65
  declare function lintAndReturn(message: string): Promise<string>;
42
66
  //#endregion
43
- export { commitPreset, configureGitHooksPath, createCzConfig, createGitMessage, isCommitlintEnabled, isInsideGitRepo, lintAndReturn, lintMessage, loadCommitConfig, runCzConfig, runGitMessage, runNpx, runPrompt, runSimpleGitHooks };
67
+ export { commitPreset, createCzConfig, createGitMessage, disableCommitlint, enableCommitlint, isCommitlintEnabled, isInsideGitRepo, lintAndReturn, lintMessage, loadCommitConfig, removeGitHooks, runCzConfig, runGitMessage, runNpx, runPrompt, runSimpleGitHooks };
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { a as configureGitHooksPath, c as createGitMessage, d as runCzConfig, f as isCommitlintEnabled, i as runPrompt, l as runGitMessage, m as loadCommitConfig, n as lintMessage, o as runNpx, p as commitPreset, r as isInsideGitRepo, s as runSimpleGitHooks, t as lintAndReturn, u as createCzConfig } from "./lint.message.mjs";
1
+ import { a as disableCommitlint, c as runNpx, d as runGitMessage, f as createCzConfig, g as loadCommitConfig, h as commitPreset, i as runPrompt, l as runSimpleGitHooks, m as isCommitlintEnabled, n as lintMessage, o as enableCommitlint, p as runCzConfig, r as isInsideGitRepo, s as removeGitHooks, t as lintAndReturn, u as createGitMessage } from "./lint.message.mjs";
2
2
 
3
- export { commitPreset, configureGitHooksPath, createCzConfig, createGitMessage, isCommitlintEnabled, isInsideGitRepo, lintAndReturn, lintMessage, loadCommitConfig, runCzConfig, runGitMessage, runNpx, runPrompt, runSimpleGitHooks };
3
+ export { commitPreset, createCzConfig, createGitMessage, disableCommitlint, enableCommitlint, isCommitlintEnabled, isInsideGitRepo, lintAndReturn, lintMessage, loadCommitConfig, removeGitHooks, runCzConfig, runGitMessage, runNpx, runPrompt, runSimpleGitHooks };
@@ -337,6 +337,10 @@ function runGitMessage(rootPath = process.cwd()) {
337
337
 
338
338
  //#endregion
339
339
  //#region src/init/simple.git.hook.ts
340
+ /**
341
+ * 配置 simple-git-hooks 到 package.json
342
+ * 注意:此函数只写入配置,不会删除配置
343
+ */
340
344
  function runSimpleGitHooks(script = "pubinfo-commit", cwd = process.cwd(), enabled = true) {
341
345
  if (!enabled) return false;
342
346
  const pkgPath = resolve(cwd, "package.json");
@@ -347,7 +351,6 @@ function runSimpleGitHooks(script = "pubinfo-commit", cwd = process.cwd(), enabl
347
351
  const hooks = json["simple-git-hooks"] || {};
348
352
  const desiredPre = "pnpm lint-staged";
349
353
  const desiredCommitMsg = `pnpm exec ${script} --edit $1`;
350
- json["simple-git-hooks"] = hooks;
351
354
  if (hooks["pre-commit"] !== desiredPre) hooks["pre-commit"] = desiredPre;
352
355
  if (hooks["commit-msg"] !== desiredCommitMsg) hooks["commit-msg"] = desiredCommitMsg;
353
356
  json["simple-git-hooks"] = hooks;
@@ -357,6 +360,10 @@ function runSimpleGitHooks(script = "pubinfo-commit", cwd = process.cwd(), enabl
357
360
  return false;
358
361
  }
359
362
  }
363
+ /**
364
+ * 执行 npx simple-git-hooks
365
+ * 会根据 package.json 中的配置安装 hooks 到 .git/hooks/
366
+ */
360
367
  function runNpx(cwd = process.cwd()) {
361
368
  try {
362
369
  execSync("npx simple-git-hooks", {
@@ -368,22 +375,58 @@ function runNpx(cwd = process.cwd()) {
368
375
  return false;
369
376
  }
370
377
  }
371
- function configureGitHooksPath(enabled = true, cwd = process.cwd()) {
378
+ /**
379
+ * 清理已安装的 git hooks 文件
380
+ * 只删除 .git/hooks 下的 hook 文件,不会修改 package.json
381
+ */
382
+ function removeGitHooks(cwd = process.cwd()) {
383
+ try {
384
+ const hooksDir = resolve(cwd, ".git", "hooks");
385
+ const preCommit = resolve(hooksDir, "pre-commit");
386
+ const commitMsg = resolve(hooksDir, "commit-msg");
387
+ if (fs.existsSync(preCommit)) fs.removeSync(preCommit);
388
+ if (fs.existsSync(commitMsg)) fs.removeSync(commitMsg);
389
+ return true;
390
+ } catch {
391
+ return false;
392
+ }
393
+ }
394
+ /**
395
+ * 禁用 git hooks(不修改 package.json 配置)
396
+ * 1. 清理已安装的 hooks 文件
397
+ * 2. 清除 core.hooksPath 配置(如果有)
398
+ */
399
+ function disableCommitlint(cwd = process.cwd()) {
372
400
  try {
373
- if (enabled) {
401
+ removeGitHooks(cwd);
402
+ try {
374
403
  execSync("git config --unset core.hooksPath", {
375
404
  cwd,
376
405
  stdio: "ignore"
377
406
  });
378
- return true;
379
- }
380
- execSync("git config core.hooksPath /dev/null", {
381
- cwd,
382
- stdio: "ignore"
383
- });
407
+ } catch {}
408
+ return true;
409
+ } catch {
410
+ return false;
411
+ }
412
+ }
413
+ /**
414
+ * 启用 git hooks
415
+ * 1. 清除残留的 hooksPath 配置
416
+ * 2. 执行 npx simple-git-hooks 重新安装 hooks
417
+ */
418
+ function enableCommitlint(cwd = process.cwd()) {
419
+ try {
420
+ try {
421
+ execSync("git config --unset core.hooksPath", {
422
+ cwd,
423
+ stdio: "ignore"
424
+ });
425
+ } catch {}
426
+ runNpx(cwd);
384
427
  return true;
385
428
  } catch {
386
- return enabled;
429
+ return false;
387
430
  }
388
431
  }
389
432
 
@@ -464,4 +507,4 @@ async function lintAndReturn(message) {
464
507
  }
465
508
 
466
509
  //#endregion
467
- export { configureGitHooksPath as a, createGitMessage as c, runCzConfig as d, isCommitlintEnabled as f, runPrompt as i, runGitMessage as l, loadCommitConfig as m, lintMessage as n, runNpx as o, commitPreset as p, isInsideGitRepo as r, runSimpleGitHooks as s, lintAndReturn as t, createCzConfig as u };
510
+ export { disableCommitlint as a, runNpx as c, runGitMessage as d, createCzConfig as f, loadCommitConfig as g, commitPreset as h, runPrompt as i, runSimpleGitHooks as l, isCommitlintEnabled as m, lintMessage as n, enableCommitlint as o, runCzConfig as p, isInsideGitRepo as r, removeGitHooks as s, lintAndReturn as t, createGitMessage as u };
package/dist/run.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as configureGitHooksPath, c as createGitMessage, d as runCzConfig, f as isCommitlintEnabled, i as runPrompt, l as runGitMessage, n as lintMessage, o as runNpx, r as isInsideGitRepo, s as runSimpleGitHooks, u as createCzConfig } from "./lint.message.mjs";
1
+ import { a as disableCommitlint, c as runNpx, d as runGitMessage, f as createCzConfig, i as runPrompt, l as runSimpleGitHooks, m as isCommitlintEnabled, n as lintMessage, p as runCzConfig, r as isInsideGitRepo, u as createGitMessage } from "./lint.message.mjs";
2
2
  import { spawnSync } from "node:child_process";
3
3
  import process from "node:process";
4
4
  import fs from "node:fs";
@@ -6,7 +6,7 @@ import { defineCommand, runMain as runMain$1 } from "citty";
6
6
 
7
7
  //#region package.json
8
8
  var name = "@pubinfo/commitlint";
9
- var version = "2.1.8";
9
+ var version = "2.1.10-beta.1";
10
10
  var description = "commitlint config for Pubinfo projects";
11
11
 
12
12
  //#endregion
@@ -51,14 +51,10 @@ const main = defineCommand({
51
51
  if (init) {
52
52
  const cwd = process.cwd();
53
53
  if (!enabled) {
54
- const removedDefault = runSimpleGitHooks("pubinfo-commit", cwd, false);
55
- const removedCli = runSimpleGitHooks("pubinfo commit", cwd, false);
56
- const disabledHooks = configureGitHooksPath(false, cwd);
57
- if (removedDefault || removedCli || disabledHooks) console.log("[pubinfo-commit] 检测到 pubinfo.config 中已关闭 commitlint,已停用 git hooks。");
54
+ if (disableCommitlint(cwd)) console.log("[pubinfo-commit] 检测到 pubinfo.config 中已关闭 commitlint,已停用 git hooks。");
58
55
  else console.log("[pubinfo-commit] 检测到 pubinfo.config 中已关闭 commitlint,跳过初始化。");
59
56
  return;
60
57
  }
61
- configureGitHooksPath(true, cwd);
62
58
  const pubinfoDir = `${cwd}/.pubinfo`;
63
59
  if (!fs.existsSync(pubinfoDir)) fs.mkdirSync(pubinfoDir);
64
60
  const gitMessageStatus = createGitMessage();
@@ -78,7 +74,7 @@ const main = defineCommand({
78
74
  }
79
75
  if (edit) {
80
76
  if (!enabled) {
81
- configureGitHooksPath(false, process.cwd());
77
+ disableCommitlint(process.cwd());
82
78
  return;
83
79
  }
84
80
  try {
@@ -95,7 +91,7 @@ const main = defineCommand({
95
91
  }
96
92
  if (!enabled) {
97
93
  console.log("[pubinfo-commit] commitlint 已关闭,交互模式不可用。");
98
- configureGitHooksPath(false, process.cwd());
94
+ disableCommitlint(process.cwd());
99
95
  return;
100
96
  }
101
97
  const result = await runPrompt();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pubinfo/commitlint",
3
3
  "type": "module",
4
- "version": "2.1.8",
4
+ "version": "2.1.10-beta.1",
5
5
  "description": "commitlint config for Pubinfo projects",
6
6
  "exports": {
7
7
  ".": "./dist/index.mjs"