dsh-desktop-statusbar 3.0.0 → 3.0.2

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.
@@ -21,8 +21,8 @@
21
21
  */
22
22
 
23
23
  import { spawn } from 'node:child_process'
24
- import { existsSync, readFileSync } from 'node:fs'
25
- import { dirname, join } from 'node:path'
24
+ import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
25
+ import { basename, dirname, join } from 'node:path'
26
26
  import { ensureReleaseAgeExclude, findProfileDir } from './release-age.js'
27
27
 
28
28
  /** 本插件包名。 */
@@ -34,6 +34,12 @@ const INSTALL_TIMEOUT_MS = 180000
34
34
  /** 传给 pnpm 的输出上限,避免日志撑爆内存。 */
35
35
  const OUTPUT_LIMIT = 64000
36
36
 
37
+ /** 交回给界面/HTTP 的 detail 上限(字符)。错误通常在尾部,截断时保留尾部。 */
38
+ const DETAIL_LIMIT = 8000
39
+
40
+ /** 回滚时要还原的三个文件(都在 profile 目录下),顺序无关。 */
41
+ const SNAPSHOT_FILES = ['package.json', 'pnpm-lock.yaml', 'pnpm-workspace.yaml']
42
+
37
43
  /** 单次更新的进行态;同一时刻只允许一个。 */
38
44
  let running = null
39
45
 
@@ -390,6 +396,158 @@ export function isReleaseAgeBlocked(output) {
390
396
  return text.includes('NO_MATURE_MATCHING_VERSION') || text.includes('MINIMUM_RELEASE_AGE_VIOLATION')
391
397
  }
392
398
 
399
+ /**
400
+ * 把 pnpm 的输出归类成具体失败原因。
401
+ *
402
+ * **本轮只做框架,等真机错误码再校准**:用户机器上那次失败没留下日志,
403
+ * 所以下面这些正则来自「已知的 pnpm 错误族」而非实测样本。
404
+ * 命中不了就返回 'install-failed'(老值,语义不变)。
405
+ *
406
+ * 判定顺序由调用方保证(不许调换):timedOut → 'timeout';
407
+ * isReleaseAgeBlocked → 'release-age';否则才进这里。
408
+ *
409
+ * @param {string} output pnpm 的 stdout + stderr
410
+ * @returns {'outdated-lockfile'|'store-error'|'postinstall-failed'|'symlink-failed'|'disk-full'|'install-failed'}
411
+ */
412
+ function classifyPnpmFailure(output) {
413
+ const text = String(output === null || output === undefined ? '' : output)
414
+ if (text.includes('ERR_PNPM_OUTDATED_LOCKFILE')) return 'outdated-lockfile'
415
+ if (/ERR_SQLITE_ERROR|store.*(open|lock)/i.test(text)) return 'store-error'
416
+ if (/spawn (EPERM|EACCES)/i.test(text) || /ELIFECYCLE/.test(text)) return 'postinstall-failed'
417
+ if (/\[EPERM\].*symlink|operation not permitted, symlink/i.test(text)) return 'symlink-failed'
418
+ if (/ENOSPC|no space left/i.test(text)) return 'disk-full'
419
+ return 'install-failed'
420
+ }
421
+
422
+ /**
423
+ * 把一段输出裁成可以直接交出去的大小。
424
+ *
425
+ * 为什么不再 `.slice(-600)`:用户机器上那次 `install-failed`,pnpm 的真正原因
426
+ * (哪一步、哪个包、什么错误码)全在 600 字符之外,界面上只剩「更新失败」。
427
+ * 保留**尾部**是因为 pnpm 的失败结论总在最后几行。
428
+ *
429
+ * @param {string} text
430
+ * @returns {string}
431
+ */
432
+ function detailOf(text) {
433
+ const value = String(text === null || text === undefined ? '' : text)
434
+ if (value.length <= DETAIL_LIMIT) return value
435
+ return '[已截断,仅保留最后 ' + DETAIL_LIMIT + ' 字符]\n' + value.slice(-DETAIL_LIMIT)
436
+ }
437
+
438
+ /**
439
+ * 把 profile 里的一个文件读成逐字节快照。
440
+ *
441
+ * 为什么用 Buffer 而不是 parse:回滚必须**逐字节**还原(含原有换行风格、键顺序、
442
+ * 末尾空行)。解析再序列化会改格式、产生无意义的 diff,用户看到一堆虚假改动更难排查。
443
+ *
444
+ * @param {string} file
445
+ * @returns {{ok: true, buf: Buffer}|{ok: false, missing: true}}
446
+ */
447
+ function snapshotFile(file) {
448
+ try {
449
+ return { ok: true, buf: readFileSync(file) }
450
+ } catch (error) {
451
+ /* 文件不存在是正常的(例如某些 profile 没有 pnpm-workspace.yaml)—— 记成"原本不存在",
452
+ 回滚时如果它被创建了,就删掉。 */
453
+ return { ok: false, missing: true }
454
+ }
455
+ }
456
+
457
+ /**
458
+ * 修复安装要装的版本号:读 profile 的 `package.json` 里本插件当前写的那个。
459
+ *
460
+ * 为什么读清单而不是读已装实物:修复的用途就是「声明与实物不一致」——
461
+ * 清单是**声明**,实物才是要修的那一头。拿清单里的版本重装一遍,四处自然对齐。
462
+ * 读不出来(文件没了 / 不是 JSON / 依赖段里没有本插件)就退回 `'latest'`:
463
+ * 那种情况说明清单里本来就没有它,让 pnpm 按最新版补上是对的。
464
+ *
465
+ * @param {string} profileDir
466
+ * @returns {string}
467
+ */
468
+ function repairTargetVersion(profileDir) {
469
+ try {
470
+ const raw = readFileSync(join(profileDir, 'package.json'), 'utf8')
471
+ const parsed = JSON.parse(raw)
472
+ const deps = parsed !== null && typeof parsed === 'object' && typeof parsed.dependencies === 'object'
473
+ ? parsed.dependencies
474
+ : null
475
+ const value = deps === null ? null : deps[SELF_NAME]
476
+ if (typeof value === 'string' && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(value.trim())) {
477
+ return value.trim()
478
+ }
479
+ } catch (error) {
480
+ /* 读不出来就走兜底 */
481
+ }
482
+ return 'latest'
483
+ }
484
+
485
+ /**
486
+ * 给三个文件做逐字节快照,键是绝对路径。
487
+ * @param {string} profileDir
488
+ * @returns {Map<string, {ok: true, buf: Buffer}|{ok: false, missing: true}>}
489
+ */
490
+ function snapshotProfile(profileDir) {
491
+ const map = new Map()
492
+ for (const name of SNAPSHOT_FILES) {
493
+ const file = join(profileDir, name)
494
+ map.set(file, snapshotFile(file))
495
+ }
496
+ return map
497
+ }
498
+
499
+ /**
500
+ * 把快照还原到盘上,**只动真正变过的文件**。
501
+ *
502
+ * 为什么只动变过的:内容没变却重写一遍,会让 mtime 无意义地跳一下 ——
503
+ * 用户/别的工具看到「刚刚改过」,排查时反而多一条假线索。
504
+ *
505
+ * @param {Map<string, {ok: true, buf: Buffer}|{ok: false, missing: true}>} snapshot
506
+ * @returns {{ok: boolean, restored: string[], removed: string[], error: string|null}}
507
+ */
508
+ function rollbackProfile(snapshot) {
509
+ const restored = []
510
+ const removed = []
511
+ try {
512
+ for (const [file, saved] of snapshot) {
513
+ const current = snapshotFile(file)
514
+ if (saved.ok === true) {
515
+ if (current.ok === true && Buffer.compare(current.buf, saved.buf) === 0) continue
516
+ writeFileSync(file, saved.buf)
517
+ restored.push(file)
518
+ } else if (current.ok === true) {
519
+ /* 原本不存在、现在被创建了 —— 删掉,才算真的回到原样 */
520
+ unlinkSync(file)
521
+ removed.push(file)
522
+ }
523
+ }
524
+ return { ok: true, restored, removed, error: null }
525
+ } catch (error) {
526
+ return {
527
+ ok: false,
528
+ restored,
529
+ removed,
530
+ error: String(error && error.message ? error.message : error),
531
+ }
532
+ }
533
+ }
534
+
535
+ /**
536
+ * 回滚失败的说明文本:先把回滚的错摆在最前面,再把原本那次 pnpm 的输出附在后面。
537
+ *
538
+ * 两段都不能丢:只看回滚错误会不知道「用户本来在干什么」,
539
+ * 只看 pnpm 输出会不知道「环境为什么还是脏的」。
540
+ *
541
+ * @param {string} rollbackError
542
+ * @param {string} output pnpm 的原始输出
543
+ * @returns {string}
544
+ */
545
+ function rollbackFailureDetail(rollbackError, output) {
546
+ const head = '[回滚失败] ' + rollbackError
547
+ const tail = String(output === null || output === undefined ? '' : output)
548
+ return detailOf(tail.length > 0 ? head + '\n\n--- pnpm 输出 ---\n' + tail : head)
549
+ }
550
+
393
551
  /**
394
552
  * 单次命令用的「发布年龄归零」开关。
395
553
  *
@@ -413,34 +571,84 @@ export const RELEASE_AGE_OVERRIDE = '--config.minimum-release-age=0'
413
571
  * 带上下面的开关:
414
572
  * - `--save-exact`:清单里写死精确版本,避免 `^` 范围日后漂移。
415
573
  * - 发布年龄归零:见 RELEASE_AGE_OVERRIDE。
574
+ * - `--ignore-scripts`:不跑依赖的生命周期脚本(取舍见下)。
575
+ * - `--reporter=append-only`:非交互输出(不刷进度条、不重绘行)。
576
+ * - `--config.confirmModulesPurge=false`:永不因清空 node_modules 停下来问人。
416
577
  *
417
578
  * 不加 `--no-frozen-lockfile`:pnpm 11 的 `add` 不认这个选项
418
579
  * (实测报 `Unknown option: 'frozen-lockfile'`;宿主当前自带 pnpm 11.28.5,同样如此)。
419
580
  * `add` 本来就会更新锁文件,不需要它。
420
581
  *
582
+ * `--ignore-scripts` 的取舍:这个 add 命令会连带重装 profile 里的其它依赖,
583
+ * 加上它会让 @dataiku/uv / @google/genai / protobufjs 等包的 postinstall 不跑。
584
+ * 那些脚本与本插件无关,跳过它们**大概率是好事**(少一个失败点、少一份供应面)。
585
+ * 代价:如果将来本插件自己需要 postinstall/preinstall/prepare,这里必须重新评估 ——
586
+ * 本插件当前没有这三个钩子(已核对 package.json)。
587
+ *
588
+ * 修复模式(`mode === 'repair'`)与更新模式的差别,每一条都有理由:
589
+ * 1. **同样是 `add`,不是 `install`**:用户原话「你更新都只更新你自己,你修复你还为什么
590
+ * 还要管别人?」—— `pnpm install` 会把 profile 里**全部**依赖重装一遍(十几个包),
591
+ * 任何一个包的 postinstall 失败都会让整条命令失败(实测 `@dataiku/uv` 就抛过
592
+ * `spawn EPERM`)。修复是兜底动作,不该因为别人的包而失败。
593
+ * 2. **目标版本取「清单里当前写的那个」**:修复不装新版本,只是让 pnpm 按现有清单
594
+ * 把**本插件**重新对齐。所以调用方要把 `package.json` 里现存的版本传进来;
595
+ * 传空就退回 `@latest`(那种情况说明清单里本来就没有它)。
596
+ * 3. **带 `--ignore-scripts`**:与更新模式一致。本插件没有 postinstall/preinstall/
597
+ * prepare(已核对 package.json),跳过脚本不会留下装不完整的自己,
598
+ * 却能避开别人的生命周期脚本。
599
+ *
421
600
  * @param {string[]} base pnpm 自身的参数(例如打包 pnpm 的 .mjs 路径)
422
601
  * @param {string} target 安装目标 `包名@版本`
602
+ * @param {'update'|'repair'} [mode] 两种模式命令相同,只有 target 的来源不同
423
603
  * @returns {string[]}
424
604
  */
425
- export function updateArgs(base, target) {
426
- return [...base, 'add', '--save-exact', RELEASE_AGE_OVERRIDE, target]
605
+ export function updateArgs(base, target, mode) {
606
+ /* 修复与更新用的是同一条命令:都只 `add` 本插件,只差 target 从哪来。
607
+ `mode` 保留在签名里是为了调用方读得懂意图,也方便将来真要分叉时有落点。 */
608
+ void mode
609
+ return [
610
+ ...base,
611
+ 'add',
612
+ '--save-exact',
613
+ '--ignore-scripts',
614
+ '--reporter=append-only',
615
+ '--config.confirmModulesPurge=false',
616
+ RELEASE_AGE_OVERRIDE,
617
+ target,
618
+ ]
427
619
  }
428
620
 
429
621
  /**
430
- * 执行一次自更新。全程不抛异常,失败以结构化结果返回。
622
+ * 执行一次自更新(或一次修复安装)。全程不抛异常,失败以结构化结果返回。
623
+ *
624
+ * `options.mode`:
625
+ * - `'update'`(默认):装一个**精确版本**,装完回读版本号核对。
626
+ * - `'repair'`:不装新版本,让 pnpm 按现有清单把 node_modules 对齐。
627
+ * 用途是「用户机器的 package.json / pnpm-lock.yaml / .modules.yaml / 实物
628
+ * 四处状态互相矛盾」(升级失败后残留)。它与 update 的差别见 updateArgs 的注释;
629
+ * 这里另外三处必须跟着变:
630
+ * 1. 不调 ensureReleaseAgeExclude —— 没有新版本需要放行,写豁免只会污染用户配置;
631
+ * 2. 跳过版本回读校验 —— repair 不改版本,拿 options.version 去比必然对不上;
632
+ * 3. 成功判据 = 退出码 0 且未超时。
431
633
  *
432
- * @param {{selfDir: string, version: string, logger?: Function, timeoutMs?: number}} options
634
+ * `result.to` 在 repair 模式下是 **null**:这次操作没有"目标版本",
635
+ * 拿 `options.version` 填进去会让人误以为修复成功就是装成了那个版本。
636
+ *
637
+ * @param {{selfDir: string, version?: string, mode?: 'update'|'repair', logger?: Function, timeoutMs?: number}} options
433
638
  * @returns {Promise<object>}
434
639
  */
435
640
  export async function runSelfUpdate(options) {
436
641
  const selfDir = options.selfDir
437
642
  const log = typeof options.logger === 'function' ? options.logger : () => {}
643
+ /** 更新还是修复;任何其它值一律按更新处理(保持旧行为)。 */
644
+ const mode = options.mode === 'repair' ? 'repair' : 'update'
438
645
  const from = installedVersion(selfDir)
439
646
  /* restartRequired 一律为 true:在 DSH 里替换已安装插件的版本**必然**需要重启,
440
647
  刷新页面不算数 —— 插件包元数据(含 client.js 的 revision)在启动时缓存、直到重启才更新
441
648
  (见 @deepseek-ai/dsh-hmr 的已知限制与 dsh-client-modules 的说明)。
442
- 不用「要不要重启」去猜,直接如实告诉界面。 */
443
- const result = { ok: false, from, to: options.version, reason: null, detail: null, profileDir: null, changedExclude: false, restartRequired: true, tried: [] }
649
+ 不用「要不要重启」去猜,直接如实告诉界面。repair 重写的是 node_modules 里的实物,
650
+ 同样要重启才生效,所以两种模式都是 true。 */
651
+ const result = { ok: false, mode, from, to: mode === 'repair' ? null : options.version, reason: null, detail: null, profileDir: null, changedExclude: false, rolledBack: false, restartRequired: true, tried: [] }
444
652
 
445
653
  /* 注意:必须在找 profile 之前就报 pnpm-not-found —— 找不到 pnpm 时 profile
446
654
  目录是否存在无关紧要,先告诉用户真正的卡点。 */
@@ -471,25 +679,45 @@ export async function runSelfUpdate(options) {
471
679
  return result
472
680
  }
473
681
 
474
- /* 先把目标版本写进豁免表:这是 pnpm 自己的豁免机制,写进去后目标版本无论多新
475
- 都算「已放行」。写不成也继续 —— 下面那次运行还带着一次性归零开关兜底。 */
476
- const ensured = ensureReleaseAgeExclude(profileDir, options.version)
477
- result.changedExclude = ensured.changed === true
478
- if (ensured.ok !== true) {
479
- log('release-age: 豁免写入未成功(' + String(ensured.reason) + '),继续尝试安装')
480
- } else if (ensured.changed === true) {
481
- log('release-age: 已放行 ' + SELF_NAME + '@' + options.version)
682
+ /* M1:快照必须在这里 —— **在 ensureReleaseAgeExclude 之前、调 pnpm 之前**。
683
+ 为什么必须在豁免写入之前:豁免表就是 ensureReleaseAgeExclude 改的,
684
+ 快照放在它之后,那次改动就落在快照「内侧」,回滚永远还原不掉它 ——
685
+ 实测会留下 `dsh-desktop-statusbar@9.9.9` 这行残留,正是用户抱怨的「状态矛盾」之一。
686
+ 放在它之前,回滚目标 = 「还没动过的用户原始状态」:pnpm 改的 package.json /
687
+ pnpm-lock.yaml 被还原,本次新增的豁免被还原,而用户原有的豁免本就在快照里,一行不动。 */
688
+ const snapshot = snapshotProfile(profileDir)
689
+
690
+ /* repair 模式**不写豁免表**:那条豁免是为了"放行某个刚发布的新版本",
691
+ 而修复安装压根不装新版本 —— 写进去只会在用户的 pnpm-workspace.yaml 里
692
+ 留下一条无意义的版本号,正是这个功能想清理的那种"状态矛盾"。 */
693
+ if (mode !== 'repair') {
694
+ /* 先把目标版本写进豁免表:这是 pnpm 自己的豁免机制,写进去后目标版本无论多新
695
+ 都算「已放行」。写不成也继续 —— 下面那次运行还带着一次性归零开关兜底。 */
696
+ const ensured = ensureReleaseAgeExclude(profileDir, options.version)
697
+ result.changedExclude = ensured.changed === true
698
+ if (ensured.ok !== true) {
699
+ log('release-age: 豁免写入未成功(' + String(ensured.reason) + '),继续尝试安装')
700
+ } else if (ensured.changed === true) {
701
+ log('release-age: 已放行 ' + SELF_NAME + '@' + options.version)
702
+ }
482
703
  }
483
704
 
484
- /* 精确版本,不用 @latest:后者在发布年龄策略下会静默装成旧的成熟版 */
485
- const target = SELF_NAME + '@' + options.version
705
+ /* 精确版本,不用 @latest:后者在发布年龄策略下会静默装成旧的成熟版。
706
+ repair 模式的目标是「清单里当前写着的那个版本」—— 修复不装新版本,只把本插件
707
+ 重新对齐一次;清单里读不到(还没被 pnpm 写过)就退回 @latest 兜底。 */
708
+ const target = mode === 'repair'
709
+ ? SELF_NAME + '@' + repairTargetVersion(profileDir)
710
+ : SELF_NAME + '@' + options.version
486
711
  const env = { ...process.env }
487
712
  if (runner.electron === true) env.ELECTRON_RUN_AS_NODE = '1'
488
- /* CI=true:pnpm 在非交互环境下不会弹提示卡住(与官方插件管理器同一做法) */
489
- env.CI = 'true'
490
- const args = updateArgs(runner.args, target)
491
-
492
- log('update: 开始安装 ' + target)
713
+ /* 这里**刻意不设 CI=true**:CI 模式下 pnpm 默认走 frozen-lockfile,
714
+ 而本场景恰恰是「先改清单、再让 pnpm 同步锁文件」,两者天然冲突 ——
715
+ 设了它,pnpm 会因为锁文件与清单不一致直接报错退出。
716
+ 非交互能力改由 updateArgs 里的 --reporter=append-only 与
717
+ --config.confirmModulesPurge=false 显式提供,不靠 CI 环境变量。 */
718
+ const args = updateArgs(runner.args, target, mode)
719
+
720
+ log(mode === 'repair' ? 'update: 开始修复安装(只重装本插件,不碰其它依赖):' + target : 'update: 开始安装 ' + target)
493
721
  const run = await runCommand(runner.command, args, {
494
722
  cwd: profileDir,
495
723
  env,
@@ -497,14 +725,28 @@ export async function runSelfUpdate(options) {
497
725
  })
498
726
  const output = (run.stdout + '\n' + run.stderr).trim()
499
727
 
728
+ /* 判定顺序固定,不许调换:
729
+ 1. timedOut → 'timeout'(输出不完整,按文本猜会误判)
730
+ 2. isReleaseAgeBlocked → 'release-age'(有准确语义,优先于泛化分类)
731
+ 3. 其余 → classifyPnpmFailure(),它内部兜底 'install-failed'
732
+ 老的 reason 值语义一个都没动,新分类只是把 'install-failed' 拆细。 */
500
733
  if (run.timedOut === true) {
501
734
  result.reason = 'timeout'
502
- result.detail = output.slice(-600)
503
- return result
735
+ } else if (run.exitCode !== 0) {
736
+ result.reason = isReleaseAgeBlocked(output) ? 'release-age' : classifyPnpmFailure(output)
737
+ }
738
+
739
+ if (result.reason !== null) {
740
+ result.detail = detailOf(output)
741
+ return finishFailure(result, { snapshot, run, output, args, runner, profileDir, log })
504
742
  }
505
- if (run.exitCode !== 0) {
506
- result.reason = isReleaseAgeBlocked(output) ? 'release-age' : 'install-failed'
507
- result.detail = output.slice(-600)
743
+
744
+ /* repair 不改版本,没有"装没装上某个版本"可核对 —— 成功判据就是退出码 0 且未超时
745
+ (上面两个分支已经把超时与非零退出拦掉了)。修复本来就可能什么都没改
746
+ (四处状态已经一致),这时 pnpm 一样退出 0,也算成功。 */
747
+ if (mode === 'repair') {
748
+ result.ok = true
749
+ log('update: 修复安装完成(退出码 0)')
508
750
  return result
509
751
  }
510
752
 
@@ -513,12 +755,12 @@ export async function runSelfUpdate(options) {
513
755
  const now = installedVersion(selfDir)
514
756
  if (now === null) {
515
757
  result.reason = 'verify-failed'
516
- return result
758
+ return finishFailure(result, { snapshot, run, output, args, runner, profileDir, log })
517
759
  }
518
760
  if (now !== options.version) {
519
761
  result.reason = 'version-mismatch'
520
- result.detail = '期望 ' + options.version + ',实际 ' + now
521
- return result
762
+ result.detail = detailOf('期望 ' + options.version + ',实际 ' + now)
763
+ return finishFailure(result, { snapshot, run, output, args, runner, profileDir, log })
522
764
  }
523
765
 
524
766
  result.ok = true
@@ -527,12 +769,72 @@ export async function runSelfUpdate(options) {
527
769
  }
528
770
 
529
771
  /**
530
- * 带并发保护地执行自更新:同一时刻只跑一个。
531
- * @param {{selfDir: string, version: string, logger?: Function, timeoutMs?: number}} options
772
+ * 失败收尾:回滚那三份文件,再把结构化失败信息挂到 result 上。
773
+ *
774
+ * 为什么单独抽一个函数:失败有四个出口(超时 / 非零退出 / verify-failed /
775
+ * version-mismatch),回滚与 failureReport 必须在**每一个**出口都发生,
776
+ * 抄四遍迟早漏一处。这里是唯一出口。
777
+ *
778
+ * 关于 failureReport 的分工:runSelfUpdate 是 lib 层、**不能发 HTTP**
779
+ * (宿主那边负责路由与上报)。这里只把结构化信息放进 result,
780
+ * 由宿主的 index.js 读取后去 POST。本函数里不许出现任何网络调用。
781
+ *
782
+ * @param {object} result
783
+ * @param {{
784
+ * snapshot: Map<string, object>, run: object, output: string,
785
+ * args: string[], runner: object, profileDir: string, log: Function,
786
+ * }} ctx
787
+ * @returns {object} 同一个 result(原地改完返回,方便调用方直接 return)
788
+ */
789
+ function finishFailure(result, ctx) {
790
+ const rollback = rollbackProfile(ctx.snapshot)
791
+ result.rolledBack = rollback.ok === true
792
+
793
+ if (rollback.ok !== true) {
794
+ /* 回滚自身抛错:reason 换成 rollback-failed,但原 pnpm 输出必须留在 detail 里,
795
+ 否则「环境为什么脏」和「本来在干什么」这两条线索会丢一条。 */
796
+ result.reason = 'rollback-failed'
797
+ result.detail = rollbackFailureDetail(rollback.error, ctx.output)
798
+ } else if (rollback.restored.length > 0 || rollback.removed.length > 0) {
799
+ ctx.log('update: 已回滚 ' + rollback.restored.map((one) => basename(one)).join(', ')
800
+ + (rollback.removed.length > 0
801
+ ? ';已删除 ' + rollback.removed.map((one) => basename(one)).join(', ')
802
+ : ''))
803
+ } else {
804
+ ctx.log('update: 无需回滚(三份文件与快照逐字节一致)')
805
+ }
806
+
807
+ result.failureReport = {
808
+ stage: 'host-install',
809
+ /* mode 一并带出去:界面要在"更新失败"和"修复失败"之间区分文案,
810
+ 宿主的日志也要能分清这次到底跑的是哪条命令。 */
811
+ mode: result.mode,
812
+ reason: result.reason,
813
+ from: result.from,
814
+ to: result.to,
815
+ exitCode: ctx.run.exitCode,
816
+ timedOut: ctx.run.timedOut === true,
817
+ command: ctx.runner.command,
818
+ args: ctx.args,
819
+ cwd: ctx.profileDir,
820
+ stdout: ctx.run.stdout,
821
+ stderr: ctx.run.stderr,
822
+ rolledBack: result.rolledBack === true,
823
+ }
824
+ return result
825
+ }
826
+
827
+ /**
828
+ * 带并发保护地执行自更新(或修复安装):同一时刻只跑一个。
829
+ *
830
+ * `options.mode` 原样透传给 runSelfUpdate(见那里的说明)。
831
+ *
832
+ * @param {{selfDir: string, version?: string, mode?: 'update'|'repair', logger?: Function, timeoutMs?: number}} options
532
833
  * @returns {Promise<object>}
533
834
  */
534
835
  export async function selfUpdateOnce(options) {
535
- if (running !== null) return { ok: false, reason: 'busy', busy: true }
836
+ const mode = options.mode === 'repair' ? 'repair' : 'update'
837
+ if (running !== null) return { ok: false, mode, reason: 'busy', busy: true }
536
838
  const task = (async () => {
537
839
  try {
538
840
  const value = await runSelfUpdate(options)
@@ -541,8 +843,9 @@ export async function selfUpdateOnce(options) {
541
843
  } catch (error) {
542
844
  const failure = {
543
845
  ok: false,
846
+ mode,
544
847
  from: null,
545
- to: options.version,
848
+ to: mode === 'repair' ? null : options.version,
546
849
  reason: 'unexpected',
547
850
  detail: String(error && error.message),
548
851
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-desktop-statusbar",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "type": "module",
5
5
  "description": "把 DSH 桌面端对话区底部的统计行换成可配置状态栏:9 个可选字段可排序 + 栏首上下文圆环,按官方峰谷口径估算费用,支持自定义模型单价与账户余额。",
6
6
  "icon": "icon.svg",