@dongnanyang/deploy-cli 0.8.0 → 0.8.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/bin.js CHANGED
@@ -2,7 +2,7 @@
2
2
  const argv = process.argv.slice(2);
3
3
  if (argv.length === 0 || argv[0] === "tui") await import("./main.js");
4
4
  else {
5
- const cli = await import("./cli-CP-jnPz3.js");
5
+ const cli = await import("./cli-BcnQXZsp.js");
6
6
  process.exitCode = await cli.main(argv);
7
7
  }
8
8
  //#endregion
@@ -244,6 +244,23 @@ function execFailure(cmd, args, result, note) {
244
244
  return new ExecError(`${note ? `${note}: ` : ""}${execFailureMessage(cmd, args, result)}`, result);
245
245
  }
246
246
  /**
247
+ * 回滚单项守护(R13):逐项执行,单项失败记 stderr 继续恢复其余项;回滚自身绝不抛错——
248
+ * 不得让回滚异常掩盖 nginx -t / 写盘段的原始错误。
249
+ */
250
+ function guardedRestore(items) {
251
+ for (const item of items) try {
252
+ item.fn();
253
+ } catch (error) {
254
+ console.error(`[nginx] 回滚 ${item.what} 失败: ${error instanceof Error ? error.message : String(error)}`);
255
+ }
256
+ }
257
+ /** reload 失败统一包装(R13 Minor 4):reject 通道与 code!==0 同 F6 条件化文案;ExecError 保留 result 透传 */
258
+ function wrapReloadFailure(error, note) {
259
+ const reason = error instanceof Error ? error.message : String(error);
260
+ if (error instanceof ExecError) return new ExecError(`${note}: ${reason}`, error.result);
261
+ return new Error(`${note}: ${reason}`, { cause: error });
262
+ }
263
+ /**
247
264
  * 路径探测:sites-available 与 sites-enabled 同时存在 → sites 模式(软链);否则 conf.d 模式(无软链)。
248
265
  * 仅凭 sites-available 存在不足以走 sites 模式:无 sites-enabled 时(nginx.conf 直接 include sites-available/*)
249
266
  * 软链无处落地,且 .bak/.new 会被 `include ...*` 通配吸入,破坏回滚与删除的生效性。
@@ -308,18 +325,29 @@ function createNginx(runner = run, paths) {
308
325
  const link = sitesEnabled ? join$1(sitesEnabled, confName(slug)) : void 0;
309
326
  const existed = pathExists(conf);
310
327
  const linkCreated = link ? !pathExists(link) : false;
311
- /** 统一回滚:恢复/清除 conf、软链、.new 临时文件(逐项守护,回滚自身不得再抛) */
328
+ /** 统一回滚:恢复/清除 conf、软链、.new 临时文件(R13:逐项守护,单项失败记 stderr 继续,回滚不抛) */
312
329
  const rollback = () => {
313
- try {
314
- if (existed) renameSync(bak, conf);
315
- else if (pathExists(conf)) unlinkSync(conf);
316
- } catch {}
317
- try {
318
- if (link && linkCreated && pathExists(link)) unlinkSync(link);
319
- } catch {}
320
- try {
321
- if (pathExists(tmp)) unlinkSync(tmp);
322
- } catch {}
330
+ guardedRestore([
331
+ {
332
+ what: "站点 conf",
333
+ fn: () => {
334
+ if (existed) renameSync(bak, conf);
335
+ else if (pathExists(conf)) unlinkSync(conf);
336
+ }
337
+ },
338
+ {
339
+ what: "软链",
340
+ fn: () => {
341
+ if (link && linkCreated && pathExists(link)) unlinkSync(link);
342
+ }
343
+ },
344
+ {
345
+ what: "临时文件",
346
+ fn: () => {
347
+ if (pathExists(tmp)) unlinkSync(tmp);
348
+ }
349
+ }
350
+ ]);
323
351
  };
324
352
  try {
325
353
  writeFileSync$1(tmp, content, "utf-8");
@@ -341,16 +369,14 @@ function createNginx(runner = run, paths) {
341
369
  rollback();
342
370
  throw execFailure("nginx", ["-t"], test, `nginx -t 校验失败,已回滚站点配置 ${conf}`);
343
371
  }
372
+ const reloadNote = existed ? `nginx reload 失败,保留备份 ${bak} 供人工恢复` : `nginx reload 失败(新建站点无备份可回退),conf 已写入 ${conf},可手工 nginx -s reload 重试或删除该 conf`;
344
373
  let reload;
345
374
  try {
346
375
  reload = await runner("nginx", ["-s", "reload"]);
347
376
  } catch (error) {
348
- throw error;
349
- }
350
- if (reload.code !== 0) {
351
- const note = existed ? `nginx reload 失败,保留备份 ${bak} 供人工恢复` : `nginx reload 失败(新建站点无备份可回退),conf 已写入 ${conf},可手工 nginx -s reload 重试或删除该 conf`;
352
- throw execFailure("nginx", ["-s", "reload"], reload, note);
377
+ throw wrapReloadFailure(error, reloadNote);
353
378
  }
379
+ if (reload.code !== 0) throw execFailure("nginx", ["-s", "reload"], reload, reloadNote);
354
380
  if (existed) unlinkSync(bak);
355
381
  }
356
382
  async function requestCert(domain) {
@@ -372,7 +398,7 @@ function createNginx(runner = run, paths) {
372
398
  certPath: `${ACME_HOME}/${domain}_ecc/fullchain.cer`,
373
399
  keyPath: `${ACME_HOME}/${domain}_ecc/${domain}.key`
374
400
  }];
375
- for (const candidate of candidates) if ((await runner("sh", ["-c", `test -f ${candidate.certPath}`])).code === 0) return {
401
+ for (const candidate of candidates) if ((await runner("sh", ["-c", `test -f "${candidate.certPath}"`])).code === 0) return {
376
402
  tool: "acme.sh",
377
403
  ...candidate
378
404
  };
@@ -407,8 +433,9 @@ function createNginx(runner = run, paths) {
407
433
  try {
408
434
  cert = await requestCert(spec.serverName);
409
435
  } catch (error) {
410
- const reason = error instanceof Error ? error.message : String(error);
411
- throw new Error(`${reason};注意:HTTP 块已上线(deploy-cli-${spec.slug}.conf 已生效,站点当前仅 HTTP)`, { cause: error });
436
+ const message = `${error instanceof Error ? error.message : String(error)};注意:HTTP 块已上线(deploy-cli-${spec.slug}.conf 已生效,站点当前仅 HTTP)`;
437
+ if (error instanceof ExecError) throw new ExecError(message, error.result);
438
+ throw new Error(message, { cause: error });
412
439
  }
413
440
  await writeSite(spec.slug, renderBlock({
414
441
  ...spec,
@@ -426,12 +453,28 @@ function createNginx(runner = run, paths) {
426
453
  const bak = join$1(sitesAvailable, bakName(slug));
427
454
  const link = sitesEnabled ? join$1(sitesEnabled, confName(slug)) : void 0;
428
455
  const hadLink = link ? pathExists(link) : false;
429
- renameSync(conf, bak);
430
- if (link && hadLink) unlinkSync(link);
456
+ /**
457
+ * 与 applySite 同等回滚对等(R2)+ 写盘段同款收口(R13):逐项守护(单项失败记 stderr 继续恢复其余项),
458
+ * 回滚自身绝不抛错;软链仅在缺失时重建(unlink 失败的半删场景下软链仍在,不得 EEXIST 二次伤害)。
459
+ */
431
460
  const rollback = () => {
432
- renameSync(bak, conf);
433
- if (link && hadLink) symlinkSync(conf, link);
461
+ guardedRestore([{
462
+ what: "站点 conf",
463
+ fn: () => renameSync(bak, conf)
464
+ }, {
465
+ what: "软链",
466
+ fn: () => {
467
+ if (link && hadLink && !pathExists(link)) symlinkSync(conf, link);
468
+ }
469
+ }]);
434
470
  };
471
+ try {
472
+ renameSync(conf, bak);
473
+ if (link && hadLink) unlinkSync(link);
474
+ } catch (error) {
475
+ rollback();
476
+ throw error;
477
+ }
435
478
  let test;
436
479
  try {
437
480
  test = await runner("nginx", ["-t"]);
@@ -443,13 +486,14 @@ function createNginx(runner = run, paths) {
443
486
  rollback();
444
487
  throw execFailure("nginx", ["-t"], test, `nginx -t 校验失败,已回滚删除操作 ${conf}`);
445
488
  }
489
+ const reloadNote = `nginx reload 失败,保留备份 ${bak} 供人工恢复`;
446
490
  let reload;
447
491
  try {
448
492
  reload = await runner("nginx", ["-s", "reload"]);
449
493
  } catch (error) {
450
- throw error;
494
+ throw wrapReloadFailure(error, reloadNote);
451
495
  }
452
- if (reload.code !== 0) throw execFailure("nginx", ["-s", "reload"], reload, `nginx reload 失败,保留备份 ${bak} 供人工恢复`);
496
+ if (reload.code !== 0) throw execFailure("nginx", ["-s", "reload"], reload, reloadNote);
453
497
  unlinkSync(bak);
454
498
  },
455
499
  async listSites() {
@@ -1446,6 +1490,10 @@ const USAGE$1 = `用法: deploy-cli status [project]
1446
1490
  HTTP healthUrl 探测结果(2xx=状态码,失败=FAIL,未配置=-)
1447
1491
  NGINX deploy-cli 站点 conf 在位(✓ 在 / ✗ 不在 / - 未知)
1448
1492
 
1493
+ 退出码(契约):
1494
+ 0 命令成功(表格已输出)——退出码不反映健康,健康与否看表内 FAIL/✗ 行
1495
+ 1 命令失败(参数错误 / 项目未注册 / 注册表损坏);行级失败降级展示,不影响退出码
1496
+
1449
1497
  示例:
1450
1498
  deploy-cli status
1451
1499
  deploy-cli status my-app`;
@@ -1529,7 +1577,7 @@ const USAGE = `用法:
1529
1577
  deploy-cli nginx 站点配置:apply / remove / list(--help 看示例)
1530
1578
  deploy-cli deploy 执行部署流水线(--help 看示例)
1531
1579
  deploy-cli status 项目健康状态聚合(--help 看示例)
1532
- deploy-cli --version 输出版本号`;
1580
+ deploy-cli --version 输出版本号(-V 同义)`;
1533
1581
  const PKG_NAME = "@dongnanyang/deploy-cli";
1534
1582
  /** 读取 package.json version(M2-T2 #13):自当前模块逐级向上找本包 package.json(构建产物 dist/ 与测试 src/ 均成立) */
1535
1583
  function readVersion() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongnanyang/deploy-cli",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Local CLI TUI for SSH tunnels & deploy scripts",
5
5
  "type": "module",
6
6
  "bin": {