@taole/deploy-helper 1.2.5 → 1.2.7
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 +2 -1
- package/lib/project.mjs +95 -1
- package/package.json +1 -1
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(可选 [title] [--verbose])");
|
|
157
158
|
registerCommand("commit", cmdCommit, "按任务号提交(参数为说明,如 feat: xxx)");
|
|
158
159
|
registerCommand("whoami", cmdWhoami, "查看当前登录用户信息");
|
|
159
160
|
registerCommand("logout", cmdLogout, "退出登录");
|
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,73 @@ 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
|
+
* 用法: dh mr [title] [--verbose]
|
|
454
|
+
*/
|
|
455
|
+
export const cmdProjectMr = async () => {
|
|
456
|
+
const rawArgs = process.argv.slice(3);
|
|
457
|
+
const verbose = rawArgs.some((arg) => arg === "--verbose" || arg === "-V");
|
|
458
|
+
const titleArg = rawArgs
|
|
459
|
+
.filter((arg) => arg !== "--verbose" && arg !== "-V")
|
|
460
|
+
.join(" ")
|
|
461
|
+
.trim();
|
|
462
|
+
|
|
463
|
+
const git = simpleGit(process.cwd());
|
|
464
|
+
const gitStatus = await git.status();
|
|
465
|
+
|
|
466
|
+
if (gitStatus.detached || !gitStatus.current) {
|
|
467
|
+
log("当前处于 detached HEAD,请先切换到功能分支");
|
|
468
|
+
process.exit(1);
|
|
469
|
+
}
|
|
470
|
+
if (!gitStatus.isClean()) {
|
|
471
|
+
log("工作区还有未提交的代码,请先提交代码");
|
|
472
|
+
process.exit(1);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const sourceBranch = gitStatus.current;
|
|
476
|
+
if (MR_FORBIDDEN_BRANCHES.has(sourceBranch)) {
|
|
477
|
+
log(`当前分支为 ${sourceBranch},不允许从 master/test 创建 MR,请切换到功能分支`);
|
|
478
|
+
process.exit(1);
|
|
479
|
+
}
|
|
480
|
+
if (!gitStatus.tracking) {
|
|
481
|
+
log(`当前分支 ${sourceBranch} 未设置远程跟踪分支,请先 push(如: git push -u origin ${sourceBranch})`);
|
|
482
|
+
process.exit(1);
|
|
483
|
+
}
|
|
484
|
+
if ((gitStatus.ahead || 0) > 0) {
|
|
485
|
+
log(`当前分支有 ${gitStatus.ahead} 个本地 commit 尚未 push,请先 push 后再创建 MR`);
|
|
486
|
+
process.exit(1);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const userCache = await getLoginCache();
|
|
490
|
+
if (!userCache || !userCache.userInfo || !userCache.userInfo.uid) {
|
|
491
|
+
log("请先登录");
|
|
492
|
+
process.exit(1);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const packageJson = getJsonConfig(process.cwd(), "package.json");
|
|
496
|
+
if (!packageJson || !packageJson.name) {
|
|
497
|
+
log("package.json 或其 name 字段不存在");
|
|
498
|
+
process.exit(1);
|
|
499
|
+
}
|
|
500
|
+
await checkPackageNameWithGitRemote(git, packageJson.name);
|
|
501
|
+
|
|
502
|
+
log(`开始创建 MR: ${sourceBranch} → master(项目 ${packageJson.name})`);
|
|
503
|
+
const result = await apiCreateMergeRequest(packageJson.name, sourceBranch, userCache, {
|
|
504
|
+
title: titleArg || undefined,
|
|
505
|
+
});
|
|
506
|
+
const mrUrl = result.detailUrl || result.webUrl || "";
|
|
507
|
+
log(`创建 MR 成功${result.localId != null ? ` !${result.localId}` : ""}`);
|
|
508
|
+
if (mrUrl) {
|
|
509
|
+
log(`MR 地址: ${mrUrl}`);
|
|
510
|
+
}
|
|
511
|
+
if (verbose) {
|
|
512
|
+
log(`MR 接口返回:\n${JSON.stringify(result, null, 2)}`);
|
|
513
|
+
}
|
|
514
|
+
process.exit(0);
|
|
515
|
+
};
|