@taole/deploy-helper 1.2.4 → 1.2.6

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/index.mjs CHANGED
@@ -10,7 +10,7 @@ import { setDebug, log } from './lib/util.mjs';
10
10
  import path from 'path';
11
11
  import { fileURLToPath } from 'url';
12
12
  import { cmdWhoami, cmdLogout, cmdLogin } from './lib/login.mjs';
13
- import { cmdProjectCreate, cmdProjectPublish, cmdProjectPull } from './lib/project.mjs';
13
+ import { cmdProjectCreate, cmdProjectPublish, cmdProjectPull, cmdProjectMr } from './lib/project.mjs';
14
14
  import { cmdCommit } from './lib/git.mjs';
15
15
  import { lightDeploy } from './lib/lightDeploy.mjs';
16
16
 
@@ -154,6 +154,7 @@ function doRegisterCommands() {
154
154
  registerCommand("create", cmdProjectCreate, "创建项目");
155
155
  registerCommand("publish", cmdProjectPublish, "更新项目到H5平台");
156
156
  registerCommand("pull", cmdProjectPull, "拉取已有项目到本地");
157
+ registerCommand("mr", cmdProjectMr, "创建合并请求到 master(可选参数为 MR 标题)");
157
158
  registerCommand("commit", cmdCommit, "按任务号提交(参数为说明,如 feat: xxx)");
158
159
  registerCommand("whoami", cmdWhoami, "查看当前登录用户信息");
159
160
  registerCommand("logout", cmdLogout, "退出登录");
@@ -199,6 +200,7 @@ async function main() {
199
200
  log(`deploy-helper v${version}`);
200
201
  notifier.notify({
201
202
  defer: false,
203
+ isGlobal: true,
202
204
  message: `发现新版本 {latestVersion}(当前 {currentVersion}),建议更新:\n{updateCommand}`,
203
205
  });
204
206
  }
package/lib/project.mjs CHANGED
@@ -56,6 +56,31 @@ async function apiDeployProject(name, version, env, userCache) {
56
56
  }
57
57
  }
58
58
 
59
+ async function apiCreateMergeRequest(name, sourceBranch, userCache, opts = {}) {
60
+ const urlPath = `/h5projects/${name}/merge-request`;
61
+ const body = { sourceBranch };
62
+ if (opts.title) {
63
+ body.title = opts.title;
64
+ }
65
+ if (opts.description != null) {
66
+ body.description = opts.description;
67
+ }
68
+ const res = await fetch(`${apiHost}${urlPath}`, {
69
+ method: "POST",
70
+ body: JSON.stringify(body),
71
+ headers: {
72
+ "Content-Type": "application/json",
73
+ Cookie: `Tuwan_Passport=${userCache.Tuwan_Passport}`,
74
+ },
75
+ });
76
+ const resJson = await res.json();
77
+ if (!res.ok) {
78
+ const error = (resJson && resJson.message) || "创建合并请求失败";
79
+ throw new Error(error);
80
+ }
81
+ return resJson;
82
+ }
83
+
59
84
  async function apiGetTemplateProjects(userCache) {
60
85
  try {
61
86
  const res = await fetch(`${apiHost}/h5projects/template`, {
@@ -418,4 +443,63 @@ export const cmdProjectPull = async () => {
418
443
  }
419
444
  log(`请cd至项目目录: ${projectDir} 进行后续开发`);
420
445
  process.exit(0);
421
- }
446
+ };
447
+
448
+ const MR_FORBIDDEN_BRANCHES = new Set(["master", "test"]);
449
+
450
+ /**
451
+ * 创建合并请求:当前分支 → master
452
+ * 前置:工作区干净、非 master/test、无未推送 commit
453
+ */
454
+ export const cmdProjectMr = async () => {
455
+ const git = simpleGit(process.cwd());
456
+ const gitStatus = await git.status();
457
+
458
+ if (gitStatus.detached || !gitStatus.current) {
459
+ log("当前处于 detached HEAD,请先切换到功能分支");
460
+ process.exit(1);
461
+ }
462
+ if (!gitStatus.isClean()) {
463
+ log("工作区还有未提交的代码,请先提交代码");
464
+ process.exit(1);
465
+ }
466
+
467
+ const sourceBranch = gitStatus.current;
468
+ if (MR_FORBIDDEN_BRANCHES.has(sourceBranch)) {
469
+ log(`当前分支为 ${sourceBranch},不允许从 master/test 创建 MR,请切换到功能分支`);
470
+ process.exit(1);
471
+ }
472
+ if (!gitStatus.tracking) {
473
+ log(`当前分支 ${sourceBranch} 未设置远程跟踪分支,请先 push(如: git push -u origin ${sourceBranch})`);
474
+ process.exit(1);
475
+ }
476
+ if ((gitStatus.ahead || 0) > 0) {
477
+ log(`当前分支有 ${gitStatus.ahead} 个本地 commit 尚未 push,请先 push 后再创建 MR`);
478
+ process.exit(1);
479
+ }
480
+
481
+ const userCache = await getLoginCache();
482
+ if (!userCache || !userCache.userInfo || !userCache.userInfo.uid) {
483
+ log("请先登录");
484
+ process.exit(1);
485
+ }
486
+
487
+ const packageJson = getJsonConfig(process.cwd(), "package.json");
488
+ if (!packageJson || !packageJson.name) {
489
+ log("package.json 或其 name 字段不存在");
490
+ process.exit(1);
491
+ }
492
+ await checkPackageNameWithGitRemote(git, packageJson.name);
493
+
494
+ const titleArg = process.argv.slice(3).join(" ").trim();
495
+ log(`开始创建 MR: ${sourceBranch} → master(项目 ${packageJson.name})`);
496
+ const result = await apiCreateMergeRequest(packageJson.name, sourceBranch, userCache, {
497
+ title: titleArg || undefined,
498
+ });
499
+ const mrUrl = result.detailUrl || result.webUrl || "";
500
+ log(`创建 MR 成功${result.localId != null ? ` !${result.localId}` : ""}`);
501
+ if (mrUrl) {
502
+ log(`MR 地址: ${mrUrl}`);
503
+ }
504
+ process.exit(0);
505
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",