@taole/deploy-helper 1.0.4 → 1.0.5

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.
Files changed (3) hide show
  1. package/index.mjs +2 -0
  2. package/lib/git.mjs +102 -0
  3. package/package.json +1 -1
package/index.mjs CHANGED
@@ -10,6 +10,7 @@ import path from 'path';
10
10
  import { fileURLToPath } from 'url';
11
11
  import { cmdWhoami, cmdLogout, cmdLogin } from './lib/login.mjs';
12
12
  import { cmdProjectCreate, cmdProjectPublish, cmdProjectPull } from './lib/project.mjs';
13
+ import { cmdCommit } from './lib/git.mjs';
13
14
 
14
15
  const __filename = fileURLToPath(import.meta.url);
15
16
  const __dirname = path.dirname(__filename);
@@ -128,6 +129,7 @@ function doRegisterCommands() {
128
129
  registerCommand("create", cmdProjectCreate, "创建项目");
129
130
  registerCommand("publish", cmdProjectPublish, "更新项目到H5平台");
130
131
  registerCommand("pull", cmdProjectPull, "拉取已有项目到本地");
132
+ registerCommand("commit", cmdCommit, "按任务号提交(参数为说明,如 feat: xxx)");
131
133
  registerCommand("whoami", cmdWhoami, "查看当前登录用户信息");
132
134
  registerCommand("logout", cmdLogout, "退出登录");
133
135
  registerCommand("login", cmdLogin, "登录");
package/lib/git.mjs ADDED
@@ -0,0 +1,102 @@
1
+ // Git 辅助操作(基于 simple-git)
2
+ import { simpleGit } from "simple-git";
3
+ import { log } from "./util.mjs";
4
+
5
+ /**
6
+ * @param {string} branch
7
+ * @param {string} latestCommitMsg
8
+ */
9
+ function extractTaskId(branch, latestCommitMsg) {
10
+ const fromBranch = branch.match(/#([A-Z]+-\d+)$/);
11
+ if (fromBranch) return fromBranch[1];
12
+ const fromCommit = latestCommitMsg.match(/-#([A-Z]+-\d+)/);
13
+ if (fromCommit) return fromCommit[1];
14
+ return null;
15
+ }
16
+
17
+ function getUserMessage() {
18
+ const argvMsg = process.argv.slice(3).join(" ").trim();
19
+ if (argvMsg) return argvMsg;
20
+ const envMsg = (
21
+ process.env.npm_config_message ||
22
+ process.env.npm_config_msg ||
23
+ ""
24
+ ).trim();
25
+ return envMsg;
26
+ }
27
+
28
+ /**
29
+ * @param {string} input
30
+ * @returns {{ full: string, typePart: null } | { full: null, typePart: string } | null}
31
+ */
32
+ /** 参数是否已自带 `-#PROJ-123` 形式任务前缀(无需再从分支/历史解析) */
33
+ function hasExplicitTaskPrefix(msg) {
34
+ return /^-#[A-Z]+-\d+/.test(msg.trim());
35
+ }
36
+
37
+ function normalizeMessage(input) {
38
+ const msg = input.trim();
39
+ if (!msg) return null;
40
+ if (msg.startsWith("-#")) {
41
+ return { full: msg, typePart: null };
42
+ }
43
+ const match = msg.match(/^([a-z]+):\s*(.+)$/i);
44
+ if (!match) return null;
45
+ return { full: null, typePart: `${match[1]}: ${match[2]}` };
46
+ }
47
+
48
+ async function getLatestSubject(git) {
49
+ try {
50
+ const logResult = await git.log({ maxCount: 1 });
51
+ if (!logResult.latest) return "";
52
+ return logResult.latest.message.split("\n")[0].trim();
53
+ } catch {
54
+ return "";
55
+ }
56
+ }
57
+
58
+ /**
59
+ * 按任务号规范提交:默认从分支名(末尾 #任务号)或最新提交中提取任务号;若参数已含 `-#DDKH-4169` 形式前缀则直接使用该说明提交。
60
+ * 用法: dh commit "feat: 说明" 或 dh commit "-#DDKH-4169 feat: 完整信息"
61
+ */
62
+ export async function cmdCommit() {
63
+ const git = simpleGit(process.cwd());
64
+ const rawMsg = getUserMessage();
65
+
66
+ let commitMsg;
67
+
68
+ if (rawMsg.trim() && hasExplicitTaskPrefix(rawMsg)) {
69
+ const parsed = normalizeMessage(rawMsg);
70
+ if (!parsed) {
71
+ log('请传入提交说明,例如: dh commit "feat:我的提交信息"');
72
+ process.exit(1);
73
+ }
74
+ commitMsg = parsed.full;
75
+ } else {
76
+ const branch = (await git.status()).current;
77
+ const latestCommitMsg = await getLatestSubject(git);
78
+ const taskId = extractTaskId(branch, latestCommitMsg);
79
+ if (!taskId) {
80
+ log("未能从分支名或最新提交中提取任务号(如 DDKH-4169)");
81
+ process.exit(1);
82
+ }
83
+
84
+ const parsed = normalizeMessage(rawMsg);
85
+ if (!parsed) {
86
+ log('请传入提交说明,例如: dh commit "feat:我的提交信息"');
87
+ process.exit(1);
88
+ }
89
+
90
+ commitMsg = parsed.full || `-#${taskId} ${parsed.typePart}`;
91
+ }
92
+
93
+ const st = await git.status();
94
+ if (st.isClean()) {
95
+ log("没有可提交的改动");
96
+ process.exit(1);
97
+ }
98
+ await git.add(".");
99
+ await git.commit(commitMsg);
100
+ log(`commit 完成: ${commitMsg}`);
101
+ process.exit(0);
102
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",