@taole/deploy-helper 1.2.8 → 1.2.10

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
@@ -230,7 +230,7 @@ async function main() {
230
230
  console.log(`command: scp {subdir/file} OfficialSite目录下仅放开admin子目录, 如scp admin/xxx.htm`);
231
231
  console.log(`command: scpevt {file} 复制文件{file}到events测试服务器{file}`);
232
232
  console.log(`command: scpevt {file} {dest} 复制文件{file}到events测试服务器{dest}`);
233
- console.log(`command: pipeline {pipelineName|pipelineId} [branch] 触发流水线{pipelineName|pipelineId} 指定分支(可省略)`);
233
+ console.log(`command: pipeline {pipelineName|pipelineId} [branch] [--trigger-only] 触发流水线{pipelineName|pipelineId},指定分支可省略;加 --trigger-only 则只触发不等待结果`);
234
234
  Object.keys(commandMap).forEach(command => {
235
235
  const cmdObj = commandMap[command];
236
236
  if (!cmdObj.help) return;
@@ -319,13 +319,21 @@ async function main() {
319
319
  process.exit(0);
320
320
  } else if (command === "pipeline") {
321
321
  const pipelineName = process.argv[3];
322
- if (!pipelineName) {
322
+ if (!pipelineName || pipelineName.startsWith("-")) {
323
323
  log(`pipeline参数不能为空`);
324
324
  process.exit(1);
325
325
  }
326
- const branch = process.argv[4] || "";
326
+ const rest = process.argv.slice(4);
327
+ const triggerOnly = rest.includes("--trigger-only");
328
+ const branch = rest.find((arg) => !String(arg).startsWith("-")) || "";
327
329
  try {
328
- await triggerPipeline(pipelineName, { waitResult: true, branch });
330
+ await triggerPipeline(pipelineName, {
331
+ waitResult: !triggerOnly,
332
+ branch,
333
+ });
334
+ if (triggerOnly) {
335
+ log(`已按 --trigger-only 退出(不等待流水线执行结果)`);
336
+ }
329
337
  process.exit(0);
330
338
  } catch (error) {
331
339
  log(`触发流水线失败: `, error);
@@ -136,9 +136,23 @@ export function setDevToken(token) {
136
136
  async function getPipelineInfoByName(name) {
137
137
  let pipeline = null;
138
138
  try {
139
+ // 云效 ListPipelines 不支持按创建时间排序,需本地按 createTime 升序取最早创建的同名流水线
139
140
  const pipelines = await listPipelines(organizationId, { pipelineName: name, perPage: 50, page: 1 });
140
141
  if (pipelines && Array.isArray(pipelines.items) && pipelines.items.length > 0) {
141
- pipeline = pipelines.items.find(item => item.name === name);
142
+ const matched = pipelines.items
143
+ .filter((item) => item.name === name)
144
+ .sort((a, b) => {
145
+ const ta = Number(a.createTime) || 0;
146
+ const tb = Number(b.createTime) || 0;
147
+ if (ta !== tb) {
148
+ return ta - tb;
149
+ }
150
+ return Number(a.id) - Number(b.id);
151
+ });
152
+ if (matched.length > 1) {
153
+ log(`发现${matched.length}条同名流水线【${name}】,将使用最早创建的: id=${matched[0].id}, createTime=${matched[0].createTime}`);
154
+ }
155
+ pipeline = matched[0] || null;
142
156
  }
143
157
  } catch (error) {
144
158
  throw new Error(`获取流水线信息失败, 请确认流水线名称:【${name}】是否正确或检查云效token的权限是否足够: ${error}`);
package/lib/project.mjs CHANGED
@@ -102,6 +102,20 @@ async function apiGetTemplateProjects(userCache) {
102
102
  }
103
103
  }
104
104
 
105
+ async function apiGetProject(nameOrId, userCache) {
106
+ const res = await fetch(`${apiHost}/h5projects/${encodeURIComponent(nameOrId)}`, {
107
+ headers: {
108
+ Cookie: `Tuwan_Passport=${userCache.Tuwan_Passport}`,
109
+ },
110
+ });
111
+ const resJson = await res.json();
112
+ if (!res.ok) {
113
+ const error = (resJson && resJson.message) || "获取项目信息失败";
114
+ throw new Error(error);
115
+ }
116
+ return resJson;
117
+ }
118
+
105
119
  async function apiCreateProject(name, userCache, templateId) {
106
120
  try {
107
121
  const body = { name };
@@ -388,7 +402,6 @@ export const cmdProjectPublish = async () => {
388
402
 
389
403
  export const cmdProjectPull = async () => {
390
404
  const name = process.argv[3];
391
- const projectDir = path.join(process.cwd(), name);
392
405
  if (!name) {
393
406
  log("请输入项目名称");
394
407
  process.exit(1);
@@ -402,13 +415,30 @@ export const cmdProjectPull = async () => {
402
415
  log("项目名称不能是纯数字");
403
416
  process.exit(1);
404
417
  }
418
+ const projectDir = path.join(process.cwd(), name);
405
419
  if (fs.existsSync(projectDir)) {
406
420
  log(`项目目录${projectDir}已存在`);
407
421
  process.exit(1);
408
422
  }
409
- const repoUrl = `https://codeup.aliyun.com/69b3821af7b43e00d420cb32/Web-Act/${name}`;
423
+
424
+ const userCache = await getLoginCache();
425
+ if (!userCache || !userCache.userInfo || !userCache.userInfo.uid) {
426
+ log("请先登录");
427
+ process.exit(1);
428
+ }
429
+
430
+ const project = await apiGetProject(name, userCache);
431
+ if (project.projectType !== "normal") {
432
+ log(`仅支持拉取 projectType=normal 的项目,当前类型为 ${project.projectType || "未知"}`);
433
+ process.exit(1);
434
+ }
435
+
436
+ const projectName = project.name || name;
437
+ const repoUrl =
438
+ project.repoUrl ||
439
+ `https://codeup.aliyun.com/69b3821af7b43e00d420cb32/Web-Act/${projectName}`;
410
440
  log(`项目仓库地址: ${repoUrl}`);
411
- const httpRepoUrl = repoUrl + ".git";
441
+ const httpRepoUrl = repoUrl.endsWith(".git") ? repoUrl : repoUrl + ".git";
412
442
  const sshRepoUrl = httpRepoUrl.replace(
413
443
  "https://codeup.aliyun.com/",
414
444
  "git@codeup.aliyun.com:"
@@ -416,32 +446,33 @@ export const cmdProjectPull = async () => {
416
446
  let git = simpleGit(process.cwd());
417
447
  let sshFail = false;
418
448
  try {
419
- await git.clone(sshRepoUrl, name);
449
+ await git.clone(sshRepoUrl, projectName);
420
450
  } catch (error) {
421
451
  log(`使用SSH协议克隆失败, 尝试使用HTTP协议克隆`);
422
452
  sshFail = true;
423
453
  }
424
454
  if (sshFail) {
425
455
  try {
426
- await git.clone(httpRepoUrl, name);
456
+ await git.clone(httpRepoUrl, projectName);
427
457
  } catch (error) {
428
458
  throw new Error(`克隆仓库失败`);
429
459
  }
430
460
  }
431
- log(`项目拉取成功, 项目目录: ${name}`);
432
- git = simpleGit(projectDir);
433
- const targetPackageJson = getJsonConfig(projectDir, "package.json");
434
- if (targetPackageJson.name !== name) {
435
- targetPackageJson.name = name;
461
+ const clonedDir = path.join(process.cwd(), projectName);
462
+ log(`项目拉取成功, 项目目录: ${projectName}`);
463
+ git = simpleGit(clonedDir);
464
+ const targetPackageJson = getJsonConfig(clonedDir, "package.json");
465
+ if (targetPackageJson.name !== projectName) {
466
+ targetPackageJson.name = projectName;
436
467
  fs.writeFileSync(
437
- path.join(projectDir, "package.json"),
468
+ path.join(clonedDir, "package.json"),
438
469
  JSON.stringify(targetPackageJson, null, 2)
439
470
  );
440
471
  log(`项目package.json覆盖成功`);
441
472
  await git.add(".");
442
473
  await git.commit(`-#DH-07 feat: 项目初始化`);
443
474
  }
444
- log(`请cd至项目目录: ${projectDir} 进行后续开发`);
475
+ log(`请cd至项目目录: ${clonedDir} 进行后续开发`);
445
476
  process.exit(0);
446
477
  };
447
478
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "1.2.8",
3
+ "version": "1.2.10",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",