dsh-desktop-statusbar 3.0.2 → 3.0.3
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/LICENSE +21 -21
- package/README.en.md +145 -145
- package/README.md +142 -142
- package/cordis.patch.yml +14 -14
- package/docs/official-models.json +468 -0
- package/docs/official-models.md +190 -0
- package/docs/vendor-intelligence-order.json +124 -0
- package/docs/vendor-intelligence-order.md +77 -0
- package/icon.svg +10 -10
- package/lib/balance-api.js +516 -499
- package/lib/client.js +11436 -9670
- package/lib/exchange-rate.js +199 -199
- package/lib/holiday.js +128 -128
- package/lib/index.js +1723 -376
- package/lib/release-age.js +509 -509
- package/lib/self-update.js +913 -870
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +72 -72
- package/tools/plugin-dir.cjs +0 -4
- package/tools/shot.cjs +9 -10
package/lib/self-update.js
CHANGED
|
@@ -1,870 +1,913 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 自更新:定位 pnpm → 放行发布年龄 → 装精确版本 → 核对结果。
|
|
3
|
-
*
|
|
4
|
-
* 为什么不用 DSH 官方的插件安装接口(`ctx.remote.pluginManager.installBundle`):
|
|
5
|
-
* 1. 那个服务由**另一个插件**(插件管理器)提供。没装它的 profile 上 `ctx.remote`
|
|
6
|
-
* 根本取不到 —— DSH 的依赖注入对未声明的服务会同步抛错,异常从 onClick 冒出
|
|
7
|
-
* 没人接,界面表现就是「点了完全没反应」。
|
|
8
|
-
* 2. 它内部跑裸 `pnpm add pkg@latest`,在 pnpm 11 的 `minimumReleaseAge`
|
|
9
|
-
* (默认 1440 分钟 = 24 小时)下会**静默装成旧的成熟版**,而且退出码是 0。
|
|
10
|
-
*
|
|
11
|
-
* 本模块的修法:自己跑 pnpm,并在跑之前把目标版本写进 profile 的
|
|
12
|
-
* `minimumReleaseAgeExclude`(见 release-age.js),再用**精确版本**安装。
|
|
13
|
-
* 精确版本是关键 —— 同样的策略下 `@latest` 会静默降级,而 `pkg@x.y.z` 要么装上、
|
|
14
|
-
* 要么明确报 `ERR_PNPM_NO_MATURE_MATCHING_VERSION`,两种结果都能被程序分辨。
|
|
15
|
-
*
|
|
16
|
-
* 走 pnpm(而不是自己下载 tgz 替换文件)的另一个原因:`pnpm add` 会同时更新
|
|
17
|
-
* profile 的 `package.json` 与锁文件,插件目录和清单保持一致;只换 node_modules
|
|
18
|
-
* 里的文件会让下一次 `pnpm install` 把版本退回去。
|
|
19
|
-
*
|
|
20
|
-
* @module dsh-desktop-statusbar/self-update
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { spawn } from 'node:child_process'
|
|
24
|
-
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
25
|
-
import { basename, dirname, join } from 'node:path'
|
|
26
|
-
import { ensureReleaseAgeExclude, findProfileDir } from './release-age.js'
|
|
27
|
-
|
|
28
|
-
/** 本插件包名。 */
|
|
29
|
-
export const SELF_NAME = 'dsh-desktop-statusbar'
|
|
30
|
-
|
|
31
|
-
/** 一次安装的整体超时(毫秒)。pnpm 冷缓存下可能较慢。 */
|
|
32
|
-
const INSTALL_TIMEOUT_MS = 180000
|
|
33
|
-
|
|
34
|
-
/** 传给 pnpm 的输出上限,避免日志撑爆内存。 */
|
|
35
|
-
const OUTPUT_LIMIT = 64000
|
|
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
|
-
|
|
43
|
-
/** 单次更新的进行态;同一时刻只允许一个。 */
|
|
44
|
-
let running = null
|
|
45
|
-
|
|
46
|
-
/** 最近一次更新的结果(给界面与 GET 状态用)。 */
|
|
47
|
-
let lastResult = null
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 读取某个插件目录里 package.json 声明的版本。
|
|
51
|
-
* @param {string} selfDir
|
|
52
|
-
* @returns {string|null}
|
|
53
|
-
*/
|
|
54
|
-
export function installedVersion(selfDir) {
|
|
55
|
-
try {
|
|
56
|
-
const manifest = JSON.parse(readFileSync(join(selfDir, 'package.json'), 'utf8'))
|
|
57
|
-
return typeof manifest.version === 'string' ? manifest.version : null
|
|
58
|
-
} catch (error) {
|
|
59
|
-
return null
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 解析本插件自己的**包根目录**(含 package.json 的那一层)。
|
|
65
|
-
*
|
|
66
|
-
* 用 `import.meta.url` 反推,再向上找到 name 匹配的那一层 —— 比猜固定层数可靠:
|
|
67
|
-
* 插件可能装在任意 profile、任意 node_modules 布局(hoisted 直接放
|
|
68
|
-
* `<profile>/node_modules/<包名>`,isolated 会经过 `.pnpm/<包名>@<版本>/node_modules/`)。
|
|
69
|
-
*
|
|
70
|
-
* @param {string} [moduleUrl] 覆盖用(测试注入)。
|
|
71
|
-
* @returns {string}
|
|
72
|
-
*/
|
|
73
|
-
export function resolveSelfDir(moduleUrl) {
|
|
74
|
-
const here = moduleUrl === undefined || moduleUrl === null ? import.meta.url : moduleUrl
|
|
75
|
-
let path = String(here)
|
|
76
|
-
if (path.startsWith('file://')) {
|
|
77
|
-
path = path.slice('file://'.length)
|
|
78
|
-
if (/^\/[A-Za-z]:/.test(path)) path = path.slice(1)
|
|
79
|
-
try { path = decodeURIComponent(path) } catch (error) { /* 保持原样 */ }
|
|
80
|
-
}
|
|
81
|
-
let current = dirname(path)
|
|
82
|
-
for (let level = 0; level < 4; level += 1) {
|
|
83
|
-
try {
|
|
84
|
-
const manifest = JSON.parse(readFileSync(join(current, 'package.json'), 'utf8'))
|
|
85
|
-
if (manifest !== null && typeof manifest === 'object' && manifest.name === SELF_NAME) return current
|
|
86
|
-
} catch (error) {
|
|
87
|
-
/* 这一层没有可读的 package.json,继续往上 */
|
|
88
|
-
}
|
|
89
|
-
const parent = dirname(current)
|
|
90
|
-
if (parent === current) break
|
|
91
|
-
current = parent
|
|
92
|
-
}
|
|
93
|
-
/* 找不到就退回模块上一级;调用方读不到 package.json 会失败,不会误改别处 */
|
|
94
|
-
return dirname(dirname(path))
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* 取一个「可能是字符串」的注入值:类型不对时退回默认值。
|
|
99
|
-
* 传 `null` / 数字 / 对象都按「显式给了一个不可用的值」处理,不回退。
|
|
100
|
-
*
|
|
101
|
-
* @param {unknown} value 注入值
|
|
102
|
-
* @param {unknown} fallback 默认值
|
|
103
|
-
* @returns {string}
|
|
104
|
-
*/
|
|
105
|
-
function asText(value, fallback) {
|
|
106
|
-
if (value === undefined) return typeof fallback === 'string' ? fallback : ''
|
|
107
|
-
return typeof value === 'string' ? value : ''
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 判断一段文本看起来是不是绝对路径。
|
|
112
|
-
*
|
|
113
|
-
* 不用 `node:path` 的 `isAbsolute()`:它按**当前进程**的平台判断,而这里允许测试
|
|
114
|
-
* 注入 `options.platform`,两者可能对不上。两种平台的写法都认:
|
|
115
|
-
* `C:\x`(盘符)、`\\server\share`(UNC)、`/x`(POSIX 根)。
|
|
116
|
-
*
|
|
117
|
-
* @param {string} text
|
|
118
|
-
* @returns {boolean}
|
|
119
|
-
*/
|
|
120
|
-
function looksAbsolutePath(text) {
|
|
121
|
-
return /^[A-Za-z]:[\\/]/.test(text)
|
|
122
|
-
|| text.startsWith('\\\\')
|
|
123
|
-
|| text.startsWith('/')
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 去掉路径结尾的分隔符;盘符根(`C:\`)原样保留 —— 去掉会变成 `C:`。
|
|
128
|
-
*
|
|
129
|
-
* @param {string} text
|
|
130
|
-
* @returns {string}
|
|
131
|
-
*/
|
|
132
|
-
function trimTrailingSeparators(text) {
|
|
133
|
-
let out = text
|
|
134
|
-
while (out.length > 1 && /[\\/]$/.test(out) === true) {
|
|
135
|
-
if (/^[A-Za-z]:[\\/]$/.test(out) === true) break
|
|
136
|
-
out = out.slice(0, -1)
|
|
137
|
-
}
|
|
138
|
-
return out
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* 解析安装根下的 `resources` 目录。
|
|
143
|
-
*
|
|
144
|
-
* 优先级(与宿主自己的 `runtimeResources()` 取值顺序一致):
|
|
145
|
-
* 1. 显式注入的 `resourcesPath`(测试用,也可以是调用方认定的权威值);
|
|
146
|
-
* 2. `process.resourcesPath` —— Electron 打包版给的权威值,比推断更稳;
|
|
147
|
-
* 3. 由 `execPath` 推断:`dirname(execPath)/resources`(非 Electron 环境兜底)。
|
|
148
|
-
*
|
|
149
|
-
* @param {string} resourcesPath 已处理过默认值(可能是空字符串)
|
|
150
|
-
* @param {string} execPath 已处理过默认值(可能是空字符串)
|
|
151
|
-
* @returns {string|null} 推断不出来时返回 null
|
|
152
|
-
*/
|
|
153
|
-
function resolveResourcesDir(resourcesPath, execPath) {
|
|
154
|
-
if (resourcesPath.length > 0 && looksAbsolutePath(resourcesPath) === true) {
|
|
155
|
-
return trimTrailingSeparators(resourcesPath)
|
|
156
|
-
}
|
|
157
|
-
if (execPath.length > 0) return trimTrailingSeparators(join(dirname(execPath), 'resources'))
|
|
158
|
-
return null
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 定位 pnpm,返回**结构化结果**(永不抛异常),把「试过哪些候选」一并带回。
|
|
163
|
-
*
|
|
164
|
-
* 只做定位,不执行任何外部命令。
|
|
165
|
-
*
|
|
166
|
-
* 候选顺序(命中即返回,逐个记进 `tried`):
|
|
167
|
-
* 1. `DSH_DESKTOP_STATUSBAR_PNPM`:插件自己的覆盖口,优先级最高。可以是 JSON 数组
|
|
168
|
-
* (`["node","/path/pnpm.mjs"]`,用别的 node 跑打包的 pnpm 时用),也可以是
|
|
169
|
-
* 单个可执行文件路径。**不检查文件是否存在** —— 用户显式指定的入口就该照办。
|
|
170
|
-
* 2. `DSH_DESKTOP_PNPM_ENTRY`:宿主自己的口径(宿主 `runtimeResources()` 在开发
|
|
171
|
-
* 模式下读它)。排在文件候选之前,宿主以后挪位置也不会失配。
|
|
172
|
-
* 3. 宿主随包提供的 pnpm,按 `resources` 目录拼出来,三条候选依次尝试:
|
|
173
|
-
* - `runtime/primary-runtime/dependencies/pnpm/bin/pnpm.mjs`:**当前宿主布局**。
|
|
174
|
-
* 优先用同一套运行时里的 node(`runtime/primary-runtime/dependencies/node/bin/node.exe`)
|
|
175
|
-
* 去跑它,不依赖 Electron 的 `ELECTRON_RUN_AS_NODE`;那套 node 不在时退回宿主可执行文件
|
|
176
|
-
* (此时才需要 `ELECTRON_RUN_AS_NODE=1`)。
|
|
177
|
-
* - `runtime/pnpm/bin/pnpm.mjs`:更旧的宿主布局,保留兼容。
|
|
178
|
-
* - `app.asar.unpacked/dsh/node_modules/pnpm/bin/pnpm.mjs`:再旧的打包布局,保留兼容。
|
|
179
|
-
*
|
|
180
|
-
* 找不到时**不做 PATH 搜索**:Windows 上 PATH 里多半是 `pnpm.cmd`,spawn 它需要
|
|
181
|
-
* `shell: true`,等于把命令解释权交给 shell,风险面大于收益。真需要时请用户用
|
|
182
|
-
* `DSH_DESKTOP_STATUSBAR_PNPM` 直接指定(issue 里的绕过写法就是这么用的)。
|
|
183
|
-
*
|
|
184
|
-
* @param {{
|
|
185
|
-
* env?: {[key: string]: string|undefined},
|
|
186
|
-
* execPath?: string,
|
|
187
|
-
* resourcesPath?: string,
|
|
188
|
-
* exists?: (path: string) => boolean,
|
|
189
|
-
* electron?: boolean,
|
|
190
|
-
* platform?: string,
|
|
191
|
-
* }} [options] 全部可选;只有测试会传
|
|
192
|
-
* @returns {{
|
|
193
|
-
* ok: boolean,
|
|
194
|
-
* runner: {command: string, args: string[], electron: boolean}|null,
|
|
195
|
-
* source: 'custom-env'|'host-env'|'primary-runtime'|'runtime-legacy'|'asar-unpacked'|null,
|
|
196
|
-
* tried: Array<{source: string, path: string|null, note: string}>,
|
|
197
|
-
* }}
|
|
198
|
-
*/
|
|
199
|
-
export function resolvePnpmDetailed(options) {
|
|
200
|
-
const opts = options === null || typeof options !== 'object' ? {} : options
|
|
201
|
-
/** 环境变量来源;默认 process.env。 */
|
|
202
|
-
const env = opts.env === undefined ? process.env : (opts.env === null ? {} : opts.env)
|
|
203
|
-
/** 宿主可执行文件;空字符串表示「拿不到」。 */
|
|
204
|
-
const execPath = asText(opts.execPath, process.execPath)
|
|
205
|
-
/** 注入的 resources 目录;未传时才回落到 process.resourcesPath。 */
|
|
206
|
-
const resourcesPath = asText(opts.resourcesPath, process.resourcesPath)
|
|
207
|
-
/** 是否跑在 Electron 里 —— 只有退回宿主可执行文件当 node 用时才需要它。 */
|
|
208
|
-
const electron = opts.electron === undefined
|
|
209
|
-
? (typeof process.versions === 'object'
|
|
210
|
-
&& process.versions !== null
|
|
211
|
-
&& typeof process.versions.electron === 'string'
|
|
212
|
-
&& process.versions.electron.length > 0)
|
|
213
|
-
: opts.electron === true
|
|
214
|
-
/** 平台;只用来决定同套运行时里的 node 叫 node.exe 还是 node。 */
|
|
215
|
-
const platform = asText(opts.platform, process.platform)
|
|
216
|
-
|
|
217
|
-
/** 每次尝试的记录:命中的也记,失败时一并带回给调用方写日志。 */
|
|
218
|
-
const tried = []
|
|
219
|
-
/** 记一条命中并直接返回成功结果。 */
|
|
220
|
-
const hit = (source, runner, path) => {
|
|
221
|
-
tried.push({ source, path: typeof path === 'string' ? path : null, note: '命中' })
|
|
222
|
-
return { ok: true, runner, source, tried }
|
|
223
|
-
}
|
|
224
|
-
/** 记一条未命中,继续试下一个候选。 */
|
|
225
|
-
const miss = (source, path, note) => {
|
|
226
|
-
tried.push({ source, path: typeof path === 'string' ? path : null, note })
|
|
227
|
-
}
|
|
228
|
-
/** 全部落空。 */
|
|
229
|
-
const fail = () => ({ ok: false, runner: null, source: null, tried })
|
|
230
|
-
/** 文件存在性检查;注入的实现抛错也当成「不存在」,保证本函数永不抛。 */
|
|
231
|
-
const exists = (target) => {
|
|
232
|
-
if (typeof opts.exists === 'function') {
|
|
233
|
-
try { return opts.exists(target) === true } catch (error) { return false }
|
|
234
|
-
}
|
|
235
|
-
try { return existsSync(target) === true } catch (error) { return false }
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/* 1) 插件自己的覆盖口:完全不检查文件是否存在(用户显式指定的入口) */
|
|
239
|
-
const custom = asText(env.DSH_DESKTOP_STATUSBAR_PNPM, '')
|
|
240
|
-
if (custom.trim().length > 0) {
|
|
241
|
-
const text = custom.trim()
|
|
242
|
-
if (text.startsWith('[') === true) {
|
|
243
|
-
let parsed = null
|
|
244
|
-
try {
|
|
245
|
-
const value = JSON.parse(text)
|
|
246
|
-
if (Array.isArray(value) === true && value.length > 0 && value.every((one) => typeof one === 'string') === true) {
|
|
247
|
-
parsed = value
|
|
248
|
-
}
|
|
249
|
-
} catch (error) {
|
|
250
|
-
/* 不是合法数组,落到下面的单路径处理 */
|
|
251
|
-
}
|
|
252
|
-
if (parsed !== null) {
|
|
253
|
-
return hit('custom-env', { command: parsed[0], args: parsed.slice(1), electron: false }, custom)
|
|
254
|
-
}
|
|
255
|
-
/* `'[]'` 也走这里:空数组不够当命令用,退化成单路径(保持旧行为) */
|
|
256
|
-
miss('custom-env', custom, '不是合法 JSON 数组,按单路径处理')
|
|
257
|
-
}
|
|
258
|
-
return hit('custom-env', { command: text, args: [], electron: false }, custom)
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/* 2) 宿主自己的口径:只存在于开发模式,但插件照样优先认它 */
|
|
262
|
-
const hostEntry = asText(env.DSH_DESKTOP_PNPM_ENTRY, '')
|
|
263
|
-
if (hostEntry.trim().length > 0) {
|
|
264
|
-
const entry = hostEntry.trim()
|
|
265
|
-
if (execPath.length === 0) {
|
|
266
|
-
miss('host-env', hostEntry, '没有可用的宿主可执行文件')
|
|
267
|
-
} else if (exists(entry) === true) {
|
|
268
|
-
/* tried.path 记变量原值(契约口径),实际执行用去掉首尾空白的路径 */
|
|
269
|
-
return hit('host-env', { command: execPath, args: [entry], electron }, hostEntry)
|
|
270
|
-
} else {
|
|
271
|
-
miss('host-env', hostEntry, '不存在')
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/* 3) 按 resources 目录拼出来的文件候选 */
|
|
276
|
-
const resources = resolveResourcesDir(resourcesPath, execPath)
|
|
277
|
-
if (resources === null) {
|
|
278
|
-
miss('primary-runtime', null, '无法推断 resources 目录')
|
|
279
|
-
return fail()
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/* 3.1 当前宿主布局:优先用同一套运行时里的 node,不碰 ELECTRON_RUN_AS_NODE */
|
|
283
|
-
const primaryPnpm = join(resources, 'runtime', 'primary-runtime', 'dependencies', 'pnpm', 'bin', 'pnpm.mjs')
|
|
284
|
-
if (exists(primaryPnpm) === true) {
|
|
285
|
-
const primaryNode = join(resources, 'runtime', 'primary-runtime', 'dependencies', 'node', 'bin', platform === 'win32' ? 'node.exe' : 'node')
|
|
286
|
-
if (exists(primaryNode) === true) {
|
|
287
|
-
return hit('primary-runtime', { command: primaryNode, args: [primaryPnpm], electron: false }, primaryPnpm)
|
|
288
|
-
}
|
|
289
|
-
if (execPath.length > 0) {
|
|
290
|
-
return hit('primary-runtime', { command: execPath, args: [primaryPnpm], electron }, primaryPnpm)
|
|
291
|
-
}
|
|
292
|
-
miss('primary-runtime', primaryPnpm, '找到 pnpm,但同套运行时里的 node 与宿主可执行文件都不可用')
|
|
293
|
-
} else {
|
|
294
|
-
miss('primary-runtime', primaryPnpm, '不存在')
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/* 3.2 / 3.3 旧布局:向后兼容,用宿主可执行文件加 ELECTRON_RUN_AS_NODE 去跑 */
|
|
298
|
-
const legacyCandidates = [
|
|
299
|
-
{ source: 'runtime-legacy', file: join(resources, 'runtime', 'pnpm', 'bin', 'pnpm.mjs') },
|
|
300
|
-
{ source: 'asar-unpacked', file: join(resources, 'app.asar.unpacked', 'dsh', 'node_modules', 'pnpm', 'bin', 'pnpm.mjs') },
|
|
301
|
-
]
|
|
302
|
-
for (const candidate of legacyCandidates) {
|
|
303
|
-
if (exists(candidate.file) !== true) {
|
|
304
|
-
miss(candidate.source, candidate.file, '不存在')
|
|
305
|
-
continue
|
|
306
|
-
}
|
|
307
|
-
if (execPath.length === 0) {
|
|
308
|
-
miss(candidate.source, candidate.file, '找到 pnpm,但没有可用的宿主可执行文件')
|
|
309
|
-
continue
|
|
310
|
-
}
|
|
311
|
-
return hit(candidate.source, { command: execPath, args: [candidate.file], electron }, candidate.file)
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
return fail()
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* 定位 pnpm 可执行文件(向后兼容的薄包装)。
|
|
319
|
-
*
|
|
320
|
-
* 内部走 `resolvePnpmDetailed()`,只关心「最终能不能跑」,不关心试过哪些候选。
|
|
321
|
-
* 现有测试与 `verify-self-update.js --online` 用的都是这个签名。
|
|
322
|
-
*
|
|
323
|
-
* @param {object} [options] 透传给 resolvePnpmDetailed(测试注入用)
|
|
324
|
-
* @returns {{command: string, args: string[], electron: boolean}|null}
|
|
325
|
-
*/
|
|
326
|
-
export function resolvePnpm(options) {
|
|
327
|
-
const detail = resolvePnpmDetailed(options)
|
|
328
|
-
return detail.ok === true ? detail.runner : null
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
let
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
*
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
*
|
|
487
|
-
* @param {string}
|
|
488
|
-
* @returns {
|
|
489
|
-
*/
|
|
490
|
-
function
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
*
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
* @
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
*
|
|
623
|
-
*
|
|
624
|
-
*
|
|
625
|
-
*
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
const
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
:
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
const
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
result.
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
*
|
|
829
|
-
*
|
|
830
|
-
*
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 自更新:定位 pnpm → 放行发布年龄 → 装精确版本 → 核对结果。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不用 DSH 官方的插件安装接口(`ctx.remote.pluginManager.installBundle`):
|
|
5
|
+
* 1. 那个服务由**另一个插件**(插件管理器)提供。没装它的 profile 上 `ctx.remote`
|
|
6
|
+
* 根本取不到 —— DSH 的依赖注入对未声明的服务会同步抛错,异常从 onClick 冒出
|
|
7
|
+
* 没人接,界面表现就是「点了完全没反应」。
|
|
8
|
+
* 2. 它内部跑裸 `pnpm add pkg@latest`,在 pnpm 11 的 `minimumReleaseAge`
|
|
9
|
+
* (默认 1440 分钟 = 24 小时)下会**静默装成旧的成熟版**,而且退出码是 0。
|
|
10
|
+
*
|
|
11
|
+
* 本模块的修法:自己跑 pnpm,并在跑之前把目标版本写进 profile 的
|
|
12
|
+
* `minimumReleaseAgeExclude`(见 release-age.js),再用**精确版本**安装。
|
|
13
|
+
* 精确版本是关键 —— 同样的策略下 `@latest` 会静默降级,而 `pkg@x.y.z` 要么装上、
|
|
14
|
+
* 要么明确报 `ERR_PNPM_NO_MATURE_MATCHING_VERSION`,两种结果都能被程序分辨。
|
|
15
|
+
*
|
|
16
|
+
* 走 pnpm(而不是自己下载 tgz 替换文件)的另一个原因:`pnpm add` 会同时更新
|
|
17
|
+
* profile 的 `package.json` 与锁文件,插件目录和清单保持一致;只换 node_modules
|
|
18
|
+
* 里的文件会让下一次 `pnpm install` 把版本退回去。
|
|
19
|
+
*
|
|
20
|
+
* @module dsh-desktop-statusbar/self-update
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
24
|
+
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
25
|
+
import { basename, dirname, join } from 'node:path'
|
|
26
|
+
import { ensureReleaseAgeExclude, findProfileDir } from './release-age.js'
|
|
27
|
+
|
|
28
|
+
/** 本插件包名。 */
|
|
29
|
+
export const SELF_NAME = 'dsh-desktop-statusbar'
|
|
30
|
+
|
|
31
|
+
/** 一次安装的整体超时(毫秒)。pnpm 冷缓存下可能较慢。 */
|
|
32
|
+
const INSTALL_TIMEOUT_MS = 180000
|
|
33
|
+
|
|
34
|
+
/** 传给 pnpm 的输出上限,避免日志撑爆内存。 */
|
|
35
|
+
const OUTPUT_LIMIT = 64000
|
|
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
|
+
|
|
43
|
+
/** 单次更新的进行态;同一时刻只允许一个。 */
|
|
44
|
+
let running = null
|
|
45
|
+
|
|
46
|
+
/** 最近一次更新的结果(给界面与 GET 状态用)。 */
|
|
47
|
+
let lastResult = null
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 读取某个插件目录里 package.json 声明的版本。
|
|
51
|
+
* @param {string} selfDir
|
|
52
|
+
* @returns {string|null}
|
|
53
|
+
*/
|
|
54
|
+
export function installedVersion(selfDir) {
|
|
55
|
+
try {
|
|
56
|
+
const manifest = JSON.parse(readFileSync(join(selfDir, 'package.json'), 'utf8'))
|
|
57
|
+
return typeof manifest.version === 'string' ? manifest.version : null
|
|
58
|
+
} catch (error) {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 解析本插件自己的**包根目录**(含 package.json 的那一层)。
|
|
65
|
+
*
|
|
66
|
+
* 用 `import.meta.url` 反推,再向上找到 name 匹配的那一层 —— 比猜固定层数可靠:
|
|
67
|
+
* 插件可能装在任意 profile、任意 node_modules 布局(hoisted 直接放
|
|
68
|
+
* `<profile>/node_modules/<包名>`,isolated 会经过 `.pnpm/<包名>@<版本>/node_modules/`)。
|
|
69
|
+
*
|
|
70
|
+
* @param {string} [moduleUrl] 覆盖用(测试注入)。
|
|
71
|
+
* @returns {string}
|
|
72
|
+
*/
|
|
73
|
+
export function resolveSelfDir(moduleUrl) {
|
|
74
|
+
const here = moduleUrl === undefined || moduleUrl === null ? import.meta.url : moduleUrl
|
|
75
|
+
let path = String(here)
|
|
76
|
+
if (path.startsWith('file://')) {
|
|
77
|
+
path = path.slice('file://'.length)
|
|
78
|
+
if (/^\/[A-Za-z]:/.test(path)) path = path.slice(1)
|
|
79
|
+
try { path = decodeURIComponent(path) } catch (error) { /* 保持原样 */ }
|
|
80
|
+
}
|
|
81
|
+
let current = dirname(path)
|
|
82
|
+
for (let level = 0; level < 4; level += 1) {
|
|
83
|
+
try {
|
|
84
|
+
const manifest = JSON.parse(readFileSync(join(current, 'package.json'), 'utf8'))
|
|
85
|
+
if (manifest !== null && typeof manifest === 'object' && manifest.name === SELF_NAME) return current
|
|
86
|
+
} catch (error) {
|
|
87
|
+
/* 这一层没有可读的 package.json,继续往上 */
|
|
88
|
+
}
|
|
89
|
+
const parent = dirname(current)
|
|
90
|
+
if (parent === current) break
|
|
91
|
+
current = parent
|
|
92
|
+
}
|
|
93
|
+
/* 找不到就退回模块上一级;调用方读不到 package.json 会失败,不会误改别处 */
|
|
94
|
+
return dirname(dirname(path))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 取一个「可能是字符串」的注入值:类型不对时退回默认值。
|
|
99
|
+
* 传 `null` / 数字 / 对象都按「显式给了一个不可用的值」处理,不回退。
|
|
100
|
+
*
|
|
101
|
+
* @param {unknown} value 注入值
|
|
102
|
+
* @param {unknown} fallback 默认值
|
|
103
|
+
* @returns {string}
|
|
104
|
+
*/
|
|
105
|
+
function asText(value, fallback) {
|
|
106
|
+
if (value === undefined) return typeof fallback === 'string' ? fallback : ''
|
|
107
|
+
return typeof value === 'string' ? value : ''
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 判断一段文本看起来是不是绝对路径。
|
|
112
|
+
*
|
|
113
|
+
* 不用 `node:path` 的 `isAbsolute()`:它按**当前进程**的平台判断,而这里允许测试
|
|
114
|
+
* 注入 `options.platform`,两者可能对不上。两种平台的写法都认:
|
|
115
|
+
* `C:\x`(盘符)、`\\server\share`(UNC)、`/x`(POSIX 根)。
|
|
116
|
+
*
|
|
117
|
+
* @param {string} text
|
|
118
|
+
* @returns {boolean}
|
|
119
|
+
*/
|
|
120
|
+
function looksAbsolutePath(text) {
|
|
121
|
+
return /^[A-Za-z]:[\\/]/.test(text)
|
|
122
|
+
|| text.startsWith('\\\\')
|
|
123
|
+
|| text.startsWith('/')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 去掉路径结尾的分隔符;盘符根(`C:\`)原样保留 —— 去掉会变成 `C:`。
|
|
128
|
+
*
|
|
129
|
+
* @param {string} text
|
|
130
|
+
* @returns {string}
|
|
131
|
+
*/
|
|
132
|
+
function trimTrailingSeparators(text) {
|
|
133
|
+
let out = text
|
|
134
|
+
while (out.length > 1 && /[\\/]$/.test(out) === true) {
|
|
135
|
+
if (/^[A-Za-z]:[\\/]$/.test(out) === true) break
|
|
136
|
+
out = out.slice(0, -1)
|
|
137
|
+
}
|
|
138
|
+
return out
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 解析安装根下的 `resources` 目录。
|
|
143
|
+
*
|
|
144
|
+
* 优先级(与宿主自己的 `runtimeResources()` 取值顺序一致):
|
|
145
|
+
* 1. 显式注入的 `resourcesPath`(测试用,也可以是调用方认定的权威值);
|
|
146
|
+
* 2. `process.resourcesPath` —— Electron 打包版给的权威值,比推断更稳;
|
|
147
|
+
* 3. 由 `execPath` 推断:`dirname(execPath)/resources`(非 Electron 环境兜底)。
|
|
148
|
+
*
|
|
149
|
+
* @param {string} resourcesPath 已处理过默认值(可能是空字符串)
|
|
150
|
+
* @param {string} execPath 已处理过默认值(可能是空字符串)
|
|
151
|
+
* @returns {string|null} 推断不出来时返回 null
|
|
152
|
+
*/
|
|
153
|
+
function resolveResourcesDir(resourcesPath, execPath) {
|
|
154
|
+
if (resourcesPath.length > 0 && looksAbsolutePath(resourcesPath) === true) {
|
|
155
|
+
return trimTrailingSeparators(resourcesPath)
|
|
156
|
+
}
|
|
157
|
+
if (execPath.length > 0) return trimTrailingSeparators(join(dirname(execPath), 'resources'))
|
|
158
|
+
return null
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 定位 pnpm,返回**结构化结果**(永不抛异常),把「试过哪些候选」一并带回。
|
|
163
|
+
*
|
|
164
|
+
* 只做定位,不执行任何外部命令。
|
|
165
|
+
*
|
|
166
|
+
* 候选顺序(命中即返回,逐个记进 `tried`):
|
|
167
|
+
* 1. `DSH_DESKTOP_STATUSBAR_PNPM`:插件自己的覆盖口,优先级最高。可以是 JSON 数组
|
|
168
|
+
* (`["node","/path/pnpm.mjs"]`,用别的 node 跑打包的 pnpm 时用),也可以是
|
|
169
|
+
* 单个可执行文件路径。**不检查文件是否存在** —— 用户显式指定的入口就该照办。
|
|
170
|
+
* 2. `DSH_DESKTOP_PNPM_ENTRY`:宿主自己的口径(宿主 `runtimeResources()` 在开发
|
|
171
|
+
* 模式下读它)。排在文件候选之前,宿主以后挪位置也不会失配。
|
|
172
|
+
* 3. 宿主随包提供的 pnpm,按 `resources` 目录拼出来,三条候选依次尝试:
|
|
173
|
+
* - `runtime/primary-runtime/dependencies/pnpm/bin/pnpm.mjs`:**当前宿主布局**。
|
|
174
|
+
* 优先用同一套运行时里的 node(`runtime/primary-runtime/dependencies/node/bin/node.exe`)
|
|
175
|
+
* 去跑它,不依赖 Electron 的 `ELECTRON_RUN_AS_NODE`;那套 node 不在时退回宿主可执行文件
|
|
176
|
+
* (此时才需要 `ELECTRON_RUN_AS_NODE=1`)。
|
|
177
|
+
* - `runtime/pnpm/bin/pnpm.mjs`:更旧的宿主布局,保留兼容。
|
|
178
|
+
* - `app.asar.unpacked/dsh/node_modules/pnpm/bin/pnpm.mjs`:再旧的打包布局,保留兼容。
|
|
179
|
+
*
|
|
180
|
+
* 找不到时**不做 PATH 搜索**:Windows 上 PATH 里多半是 `pnpm.cmd`,spawn 它需要
|
|
181
|
+
* `shell: true`,等于把命令解释权交给 shell,风险面大于收益。真需要时请用户用
|
|
182
|
+
* `DSH_DESKTOP_STATUSBAR_PNPM` 直接指定(issue 里的绕过写法就是这么用的)。
|
|
183
|
+
*
|
|
184
|
+
* @param {{
|
|
185
|
+
* env?: {[key: string]: string|undefined},
|
|
186
|
+
* execPath?: string,
|
|
187
|
+
* resourcesPath?: string,
|
|
188
|
+
* exists?: (path: string) => boolean,
|
|
189
|
+
* electron?: boolean,
|
|
190
|
+
* platform?: string,
|
|
191
|
+
* }} [options] 全部可选;只有测试会传
|
|
192
|
+
* @returns {{
|
|
193
|
+
* ok: boolean,
|
|
194
|
+
* runner: {command: string, args: string[], electron: boolean}|null,
|
|
195
|
+
* source: 'custom-env'|'host-env'|'primary-runtime'|'runtime-legacy'|'asar-unpacked'|null,
|
|
196
|
+
* tried: Array<{source: string, path: string|null, note: string}>,
|
|
197
|
+
* }}
|
|
198
|
+
*/
|
|
199
|
+
export function resolvePnpmDetailed(options) {
|
|
200
|
+
const opts = options === null || typeof options !== 'object' ? {} : options
|
|
201
|
+
/** 环境变量来源;默认 process.env。 */
|
|
202
|
+
const env = opts.env === undefined ? process.env : (opts.env === null ? {} : opts.env)
|
|
203
|
+
/** 宿主可执行文件;空字符串表示「拿不到」。 */
|
|
204
|
+
const execPath = asText(opts.execPath, process.execPath)
|
|
205
|
+
/** 注入的 resources 目录;未传时才回落到 process.resourcesPath。 */
|
|
206
|
+
const resourcesPath = asText(opts.resourcesPath, process.resourcesPath)
|
|
207
|
+
/** 是否跑在 Electron 里 —— 只有退回宿主可执行文件当 node 用时才需要它。 */
|
|
208
|
+
const electron = opts.electron === undefined
|
|
209
|
+
? (typeof process.versions === 'object'
|
|
210
|
+
&& process.versions !== null
|
|
211
|
+
&& typeof process.versions.electron === 'string'
|
|
212
|
+
&& process.versions.electron.length > 0)
|
|
213
|
+
: opts.electron === true
|
|
214
|
+
/** 平台;只用来决定同套运行时里的 node 叫 node.exe 还是 node。 */
|
|
215
|
+
const platform = asText(opts.platform, process.platform)
|
|
216
|
+
|
|
217
|
+
/** 每次尝试的记录:命中的也记,失败时一并带回给调用方写日志。 */
|
|
218
|
+
const tried = []
|
|
219
|
+
/** 记一条命中并直接返回成功结果。 */
|
|
220
|
+
const hit = (source, runner, path) => {
|
|
221
|
+
tried.push({ source, path: typeof path === 'string' ? path : null, note: '命中' })
|
|
222
|
+
return { ok: true, runner, source, tried }
|
|
223
|
+
}
|
|
224
|
+
/** 记一条未命中,继续试下一个候选。 */
|
|
225
|
+
const miss = (source, path, note) => {
|
|
226
|
+
tried.push({ source, path: typeof path === 'string' ? path : null, note })
|
|
227
|
+
}
|
|
228
|
+
/** 全部落空。 */
|
|
229
|
+
const fail = () => ({ ok: false, runner: null, source: null, tried })
|
|
230
|
+
/** 文件存在性检查;注入的实现抛错也当成「不存在」,保证本函数永不抛。 */
|
|
231
|
+
const exists = (target) => {
|
|
232
|
+
if (typeof opts.exists === 'function') {
|
|
233
|
+
try { return opts.exists(target) === true } catch (error) { return false }
|
|
234
|
+
}
|
|
235
|
+
try { return existsSync(target) === true } catch (error) { return false }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* 1) 插件自己的覆盖口:完全不检查文件是否存在(用户显式指定的入口) */
|
|
239
|
+
const custom = asText(env.DSH_DESKTOP_STATUSBAR_PNPM, '')
|
|
240
|
+
if (custom.trim().length > 0) {
|
|
241
|
+
const text = custom.trim()
|
|
242
|
+
if (text.startsWith('[') === true) {
|
|
243
|
+
let parsed = null
|
|
244
|
+
try {
|
|
245
|
+
const value = JSON.parse(text)
|
|
246
|
+
if (Array.isArray(value) === true && value.length > 0 && value.every((one) => typeof one === 'string') === true) {
|
|
247
|
+
parsed = value
|
|
248
|
+
}
|
|
249
|
+
} catch (error) {
|
|
250
|
+
/* 不是合法数组,落到下面的单路径处理 */
|
|
251
|
+
}
|
|
252
|
+
if (parsed !== null) {
|
|
253
|
+
return hit('custom-env', { command: parsed[0], args: parsed.slice(1), electron: false }, custom)
|
|
254
|
+
}
|
|
255
|
+
/* `'[]'` 也走这里:空数组不够当命令用,退化成单路径(保持旧行为) */
|
|
256
|
+
miss('custom-env', custom, '不是合法 JSON 数组,按单路径处理')
|
|
257
|
+
}
|
|
258
|
+
return hit('custom-env', { command: text, args: [], electron: false }, custom)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/* 2) 宿主自己的口径:只存在于开发模式,但插件照样优先认它 */
|
|
262
|
+
const hostEntry = asText(env.DSH_DESKTOP_PNPM_ENTRY, '')
|
|
263
|
+
if (hostEntry.trim().length > 0) {
|
|
264
|
+
const entry = hostEntry.trim()
|
|
265
|
+
if (execPath.length === 0) {
|
|
266
|
+
miss('host-env', hostEntry, '没有可用的宿主可执行文件')
|
|
267
|
+
} else if (exists(entry) === true) {
|
|
268
|
+
/* tried.path 记变量原值(契约口径),实际执行用去掉首尾空白的路径 */
|
|
269
|
+
return hit('host-env', { command: execPath, args: [entry], electron }, hostEntry)
|
|
270
|
+
} else {
|
|
271
|
+
miss('host-env', hostEntry, '不存在')
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* 3) 按 resources 目录拼出来的文件候选 */
|
|
276
|
+
const resources = resolveResourcesDir(resourcesPath, execPath)
|
|
277
|
+
if (resources === null) {
|
|
278
|
+
miss('primary-runtime', null, '无法推断 resources 目录')
|
|
279
|
+
return fail()
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/* 3.1 当前宿主布局:优先用同一套运行时里的 node,不碰 ELECTRON_RUN_AS_NODE */
|
|
283
|
+
const primaryPnpm = join(resources, 'runtime', 'primary-runtime', 'dependencies', 'pnpm', 'bin', 'pnpm.mjs')
|
|
284
|
+
if (exists(primaryPnpm) === true) {
|
|
285
|
+
const primaryNode = join(resources, 'runtime', 'primary-runtime', 'dependencies', 'node', 'bin', platform === 'win32' ? 'node.exe' : 'node')
|
|
286
|
+
if (exists(primaryNode) === true) {
|
|
287
|
+
return hit('primary-runtime', { command: primaryNode, args: [primaryPnpm], electron: false }, primaryPnpm)
|
|
288
|
+
}
|
|
289
|
+
if (execPath.length > 0) {
|
|
290
|
+
return hit('primary-runtime', { command: execPath, args: [primaryPnpm], electron }, primaryPnpm)
|
|
291
|
+
}
|
|
292
|
+
miss('primary-runtime', primaryPnpm, '找到 pnpm,但同套运行时里的 node 与宿主可执行文件都不可用')
|
|
293
|
+
} else {
|
|
294
|
+
miss('primary-runtime', primaryPnpm, '不存在')
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/* 3.2 / 3.3 旧布局:向后兼容,用宿主可执行文件加 ELECTRON_RUN_AS_NODE 去跑 */
|
|
298
|
+
const legacyCandidates = [
|
|
299
|
+
{ source: 'runtime-legacy', file: join(resources, 'runtime', 'pnpm', 'bin', 'pnpm.mjs') },
|
|
300
|
+
{ source: 'asar-unpacked', file: join(resources, 'app.asar.unpacked', 'dsh', 'node_modules', 'pnpm', 'bin', 'pnpm.mjs') },
|
|
301
|
+
]
|
|
302
|
+
for (const candidate of legacyCandidates) {
|
|
303
|
+
if (exists(candidate.file) !== true) {
|
|
304
|
+
miss(candidate.source, candidate.file, '不存在')
|
|
305
|
+
continue
|
|
306
|
+
}
|
|
307
|
+
if (execPath.length === 0) {
|
|
308
|
+
miss(candidate.source, candidate.file, '找到 pnpm,但没有可用的宿主可执行文件')
|
|
309
|
+
continue
|
|
310
|
+
}
|
|
311
|
+
return hit(candidate.source, { command: execPath, args: [candidate.file], electron }, candidate.file)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return fail()
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* 定位 pnpm 可执行文件(向后兼容的薄包装)。
|
|
319
|
+
*
|
|
320
|
+
* 内部走 `resolvePnpmDetailed()`,只关心「最终能不能跑」,不关心试过哪些候选。
|
|
321
|
+
* 现有测试与 `verify-self-update.js --online` 用的都是这个签名。
|
|
322
|
+
*
|
|
323
|
+
* @param {object} [options] 透传给 resolvePnpmDetailed(测试注入用)
|
|
324
|
+
* @returns {{command: string, args: string[], electron: boolean}|null}
|
|
325
|
+
*/
|
|
326
|
+
export function resolvePnpm(options) {
|
|
327
|
+
const detail = resolvePnpmDetailed(options)
|
|
328
|
+
return detail.ok === true ? detail.runner : null
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 杀掉一条命令的**整棵进程树**。
|
|
333
|
+
*
|
|
334
|
+
* 为什么不能只 `child.kill()`:pnpm 是 Node 脚本,真正干活的是它再 spawn 出来的子进程;
|
|
335
|
+
* 在 Windows 上 `child.kill()` 只结束直接子进程,pnpm 的子孙进程会继续跑、继续占着文件句柄,
|
|
336
|
+
* 于是超时后下一次安装会撞上 `EPERM`/锁文件占用。用 `taskkill /T /F` 杀整棵树;
|
|
337
|
+
* 其它平台先 SIGTERM,2 秒后补 SIGKILL。
|
|
338
|
+
*
|
|
339
|
+
* `taskkill` 的失败**不会抛异常**:`spawnSync` 把失败放在 `result.error` / `result.status`
|
|
340
|
+
* 里(taskkill 不存在时 error=ENOENT,拒绝访问时 status≠0)。旧实现只 catch 异常,
|
|
341
|
+
* 失败分支永远不可达,进程树没被杀死也无人知道 —— 这里显式检查返回值,失败就回退
|
|
342
|
+
* `child.kill()`,至少把直接子进程收掉。
|
|
343
|
+
*
|
|
344
|
+
* @param {object} child spawn 返回的 ChildProcess
|
|
345
|
+
* @param {{spawnSync?: Function, platform?: string}} [deps] 测试注入(默认真实实现)
|
|
346
|
+
*/
|
|
347
|
+
export function killProcessTree(child, deps) {
|
|
348
|
+
if (child === null || child === undefined || typeof child.pid !== 'number') return
|
|
349
|
+
const spawnSyncFn = deps !== undefined && deps !== null && typeof deps.spawnSync === 'function' ? deps.spawnSync : spawnSync
|
|
350
|
+
const platform = deps !== undefined && deps !== null && typeof deps.platform === 'string' ? deps.platform : process.platform
|
|
351
|
+
if (platform === 'win32') {
|
|
352
|
+
let failed = true
|
|
353
|
+
try {
|
|
354
|
+
const result = spawnSyncFn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true })
|
|
355
|
+
failed = result === null || result === undefined
|
|
356
|
+
|| (result.error !== undefined && result.error !== null)
|
|
357
|
+
|| result.status !== 0
|
|
358
|
+
} catch (error) {
|
|
359
|
+
failed = true
|
|
360
|
+
}
|
|
361
|
+
if (failed === true) {
|
|
362
|
+
/* taskkill 不可用 / 失败时退回普通 kill(可能已退出) */
|
|
363
|
+
try { child.kill() } catch (nested) { /* 可能已退出 */ }
|
|
364
|
+
}
|
|
365
|
+
return
|
|
366
|
+
}
|
|
367
|
+
try { child.kill('SIGTERM') } catch (error) { /* 可能已退出 */ }
|
|
368
|
+
const timer = setTimeout(() => {
|
|
369
|
+
try { child.kill('SIGKILL') } catch (error) { /* 可能已退出 */ }
|
|
370
|
+
}, 2000)
|
|
371
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* 跑一条命令并收集输出。
|
|
376
|
+
* @param {string} command
|
|
377
|
+
* @param {string[]} args
|
|
378
|
+
* @param {{cwd: string, env?: object, timeoutMs?: number}} options
|
|
379
|
+
* @returns {Promise<{exitCode: number|null, stdout: string, stderr: string, timedOut: boolean}>}
|
|
380
|
+
*/
|
|
381
|
+
export function runCommand(command, args, options) {
|
|
382
|
+
return new Promise((resolve) => {
|
|
383
|
+
let child
|
|
384
|
+
try {
|
|
385
|
+
child = spawn(command, args, {
|
|
386
|
+
cwd: options.cwd,
|
|
387
|
+
env: options.env === undefined ? process.env : options.env,
|
|
388
|
+
windowsHide: true,
|
|
389
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
390
|
+
})
|
|
391
|
+
} catch (error) {
|
|
392
|
+
resolve({ exitCode: null, stdout: '', stderr: String(error && error.message), timedOut: false })
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
let stdout = ''
|
|
396
|
+
let stderr = ''
|
|
397
|
+
let settled = false
|
|
398
|
+
let timedOut = false
|
|
399
|
+
let timer = null
|
|
400
|
+
const finish = (exitCode) => {
|
|
401
|
+
if (settled) return
|
|
402
|
+
settled = true
|
|
403
|
+
if (timer !== null) clearTimeout(timer)
|
|
404
|
+
resolve({ exitCode, stdout, stderr, timedOut })
|
|
405
|
+
}
|
|
406
|
+
timer = setTimeout(() => {
|
|
407
|
+
timedOut = true
|
|
408
|
+
killProcessTree(child)
|
|
409
|
+
/* 被杀之后 close 未必触发,兜一个底 */
|
|
410
|
+
setTimeout(() => finish(null), 5000)
|
|
411
|
+
}, options.timeoutMs === undefined ? INSTALL_TIMEOUT_MS : options.timeoutMs)
|
|
412
|
+
|
|
413
|
+
child.stdout.on('data', (chunk) => {
|
|
414
|
+
if (stdout.length < OUTPUT_LIMIT) stdout += String(chunk)
|
|
415
|
+
})
|
|
416
|
+
child.stderr.on('data', (chunk) => {
|
|
417
|
+
if (stderr.length < OUTPUT_LIMIT) stderr += String(chunk)
|
|
418
|
+
})
|
|
419
|
+
child.on('error', (error) => {
|
|
420
|
+
stderr += String(error && error.message)
|
|
421
|
+
finish(null)
|
|
422
|
+
})
|
|
423
|
+
child.on('close', (code) => finish(code))
|
|
424
|
+
})
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* 判断 pnpm 的输出是不是「发布年龄」拦下的。
|
|
429
|
+
*
|
|
430
|
+
* pnpm 11 用 `ERR_PNPM_NO_MATURE_MATCHING_VERSION`;老版本用
|
|
431
|
+
* `ERR_PNPM_MINIMUM_RELEASE_AGE_VIOLATION`。两个都认,这样即使豁免没生效,
|
|
432
|
+
* 也能给出准确的原因而不是笼统的「更新失败」。
|
|
433
|
+
*
|
|
434
|
+
* @param {string} output
|
|
435
|
+
* @returns {boolean}
|
|
436
|
+
*/
|
|
437
|
+
export function isReleaseAgeBlocked(output) {
|
|
438
|
+
const text = String(output)
|
|
439
|
+
return text.includes('NO_MATURE_MATCHING_VERSION') || text.includes('MINIMUM_RELEASE_AGE_VIOLATION')
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* 把 pnpm 的输出归类成具体失败原因。
|
|
444
|
+
*
|
|
445
|
+
* **本轮只做框架,等真机错误码再校准**:用户机器上那次失败没留下日志,
|
|
446
|
+
* 所以下面这些正则来自「已知的 pnpm 错误族」而非实测样本。
|
|
447
|
+
* 命中不了就返回 'install-failed'(老值,语义不变)。
|
|
448
|
+
*
|
|
449
|
+
* 判定顺序由调用方保证(不许调换):timedOut → 'timeout';
|
|
450
|
+
* isReleaseAgeBlocked → 'release-age';否则才进这里。
|
|
451
|
+
*
|
|
452
|
+
* @param {string} output pnpm 的 stdout + stderr
|
|
453
|
+
* @returns {'outdated-lockfile'|'store-error'|'postinstall-failed'|'symlink-failed'|'disk-full'|'install-failed'}
|
|
454
|
+
*/
|
|
455
|
+
function classifyPnpmFailure(output) {
|
|
456
|
+
const text = String(output === null || output === undefined ? '' : output)
|
|
457
|
+
if (text.includes('ERR_PNPM_OUTDATED_LOCKFILE')) return 'outdated-lockfile'
|
|
458
|
+
if (/ERR_SQLITE_ERROR|store.*(open|lock)/i.test(text)) return 'store-error'
|
|
459
|
+
if (/spawn (EPERM|EACCES)/i.test(text) || /ELIFECYCLE/.test(text)) return 'postinstall-failed'
|
|
460
|
+
if (/\[EPERM\].*symlink|operation not permitted, symlink/i.test(text)) return 'symlink-failed'
|
|
461
|
+
if (/ENOSPC|no space left/i.test(text)) return 'disk-full'
|
|
462
|
+
return 'install-failed'
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* 把一段输出裁成可以直接交出去的大小。
|
|
467
|
+
*
|
|
468
|
+
* 为什么不再 `.slice(-600)`:用户机器上那次 `install-failed`,pnpm 的真正原因
|
|
469
|
+
* (哪一步、哪个包、什么错误码)全在 600 字符之外,界面上只剩「更新失败」。
|
|
470
|
+
* 保留**尾部**是因为 pnpm 的失败结论总在最后几行。
|
|
471
|
+
*
|
|
472
|
+
* @param {string} text
|
|
473
|
+
* @returns {string}
|
|
474
|
+
*/
|
|
475
|
+
function detailOf(text) {
|
|
476
|
+
const value = String(text === null || text === undefined ? '' : text)
|
|
477
|
+
if (value.length <= DETAIL_LIMIT) return value
|
|
478
|
+
return '[已截断,仅保留最后 ' + DETAIL_LIMIT + ' 字符]\n' + value.slice(-DETAIL_LIMIT)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* 把 profile 里的一个文件读成逐字节快照。
|
|
483
|
+
*
|
|
484
|
+
* 为什么用 Buffer 而不是 parse:回滚必须**逐字节**还原(含原有换行风格、键顺序、
|
|
485
|
+
* 末尾空行)。解析再序列化会改格式、产生无意义的 diff,用户看到一堆虚假改动更难排查。
|
|
486
|
+
*
|
|
487
|
+
* @param {string} file
|
|
488
|
+
* @returns {{ok: true, buf: Buffer}|{ok: false, missing: true}}
|
|
489
|
+
*/
|
|
490
|
+
function snapshotFile(file) {
|
|
491
|
+
try {
|
|
492
|
+
return { ok: true, buf: readFileSync(file) }
|
|
493
|
+
} catch (error) {
|
|
494
|
+
/* 文件不存在是正常的(例如某些 profile 没有 pnpm-workspace.yaml)—— 记成"原本不存在",
|
|
495
|
+
回滚时如果它被创建了,就删掉。 */
|
|
496
|
+
return { ok: false, missing: true }
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* 修复安装要装的版本号:读 profile 的 `package.json` 里本插件当前写的那个。
|
|
502
|
+
*
|
|
503
|
+
* 为什么读清单而不是读已装实物:修复的用途就是「声明与实物不一致」——
|
|
504
|
+
* 清单是**声明**,实物才是要修的那一头。拿清单里的版本重装一遍,四处自然对齐。
|
|
505
|
+
* 读不出来(文件没了 / 不是 JSON / 依赖段里没有本插件)就退回 `'latest'`:
|
|
506
|
+
* 那种情况说明清单里本来就没有它,让 pnpm 按最新版补上是对的。
|
|
507
|
+
*
|
|
508
|
+
* @param {string} profileDir
|
|
509
|
+
* @returns {string}
|
|
510
|
+
*/
|
|
511
|
+
function repairTargetVersion(profileDir) {
|
|
512
|
+
try {
|
|
513
|
+
const raw = readFileSync(join(profileDir, 'package.json'), 'utf8')
|
|
514
|
+
const parsed = JSON.parse(raw)
|
|
515
|
+
const deps = parsed !== null && typeof parsed === 'object' && typeof parsed.dependencies === 'object'
|
|
516
|
+
? parsed.dependencies
|
|
517
|
+
: null
|
|
518
|
+
const value = deps === null ? null : deps[SELF_NAME]
|
|
519
|
+
if (typeof value === 'string' && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(value.trim())) {
|
|
520
|
+
return value.trim()
|
|
521
|
+
}
|
|
522
|
+
} catch (error) {
|
|
523
|
+
/* 读不出来就走兜底 */
|
|
524
|
+
}
|
|
525
|
+
return 'latest'
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* 给三个文件做逐字节快照,键是绝对路径。
|
|
530
|
+
* @param {string} profileDir
|
|
531
|
+
* @returns {Map<string, {ok: true, buf: Buffer}|{ok: false, missing: true}>}
|
|
532
|
+
*/
|
|
533
|
+
function snapshotProfile(profileDir) {
|
|
534
|
+
const map = new Map()
|
|
535
|
+
for (const name of SNAPSHOT_FILES) {
|
|
536
|
+
const file = join(profileDir, name)
|
|
537
|
+
map.set(file, snapshotFile(file))
|
|
538
|
+
}
|
|
539
|
+
return map
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* 把快照还原到盘上,**只动真正变过的文件**。
|
|
544
|
+
*
|
|
545
|
+
* 为什么只动变过的:内容没变却重写一遍,会让 mtime 无意义地跳一下 ——
|
|
546
|
+
* 用户/别的工具看到「刚刚改过」,排查时反而多一条假线索。
|
|
547
|
+
*
|
|
548
|
+
* @param {Map<string, {ok: true, buf: Buffer}|{ok: false, missing: true}>} snapshot
|
|
549
|
+
* @returns {{ok: boolean, restored: string[], removed: string[], error: string|null}}
|
|
550
|
+
*/
|
|
551
|
+
function rollbackProfile(snapshot) {
|
|
552
|
+
const restored = []
|
|
553
|
+
const removed = []
|
|
554
|
+
try {
|
|
555
|
+
for (const [file, saved] of snapshot) {
|
|
556
|
+
const current = snapshotFile(file)
|
|
557
|
+
if (saved.ok === true) {
|
|
558
|
+
if (current.ok === true && Buffer.compare(current.buf, saved.buf) === 0) continue
|
|
559
|
+
writeFileSync(file, saved.buf)
|
|
560
|
+
restored.push(file)
|
|
561
|
+
} else if (current.ok === true) {
|
|
562
|
+
/* 原本不存在、现在被创建了 —— 删掉,才算真的回到原样 */
|
|
563
|
+
unlinkSync(file)
|
|
564
|
+
removed.push(file)
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return { ok: true, restored, removed, error: null }
|
|
568
|
+
} catch (error) {
|
|
569
|
+
return {
|
|
570
|
+
ok: false,
|
|
571
|
+
restored,
|
|
572
|
+
removed,
|
|
573
|
+
error: String(error && error.message ? error.message : error),
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* 回滚失败的说明文本:先把回滚的错摆在最前面,再把原本那次 pnpm 的输出附在后面。
|
|
580
|
+
*
|
|
581
|
+
* 两段都不能丢:只看回滚错误会不知道「用户本来在干什么」,
|
|
582
|
+
* 只看 pnpm 输出会不知道「环境为什么还是脏的」。
|
|
583
|
+
*
|
|
584
|
+
* @param {string} rollbackError
|
|
585
|
+
* @param {string} output pnpm 的原始输出
|
|
586
|
+
* @returns {string}
|
|
587
|
+
*/
|
|
588
|
+
function rollbackFailureDetail(rollbackError, output) {
|
|
589
|
+
const head = '[回滚失败] ' + rollbackError
|
|
590
|
+
const tail = String(output === null || output === undefined ? '' : output)
|
|
591
|
+
return detailOf(tail.length > 0 ? head + '\n\n--- pnpm 输出 ---\n' + tail : head)
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* 单次命令用的「发布年龄归零」开关。
|
|
596
|
+
*
|
|
597
|
+
* 为什么需要它,而不是只靠 `minimumReleaseAgeExclude` 放行目标版本:
|
|
598
|
+
* 被年龄策略拦下的不只是目标包本身,还有它的**依赖**。一旦插件新版本引入了
|
|
599
|
+
* 一个刚发布不满 24 小时的依赖,只放行插件自己仍然装不上
|
|
600
|
+
* (实测:门槛拉高时 `zod@4.4.0` 会连同插件一起被拒)。
|
|
601
|
+
*
|
|
602
|
+
* 写法用「烤肉串」形式而不是驼峰:pnpm 12.3.0 起(原生 CLI)会**静默忽略**
|
|
603
|
+
* `--config.minimumReleaseAge=0`(不报未知选项,直接当没传),于是重试仍会失败;
|
|
604
|
+
* `--config.minimum-release-age=0` 在 pnpm 10 / 11 / 12 上都被认。
|
|
605
|
+
* 这是这个键特有的行为,不能推广到别的 `--config.*`。
|
|
606
|
+
*
|
|
607
|
+
* 作用域只有这一条命令:不写进任何配置文件,所以不会永久改变用户的策略。
|
|
608
|
+
*/
|
|
609
|
+
export const RELEASE_AGE_OVERRIDE = '--config.minimum-release-age=0'
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* 一次 pnpm 调用的参数。
|
|
613
|
+
*
|
|
614
|
+
* 带上下面的开关:
|
|
615
|
+
* - `--save-exact`:清单里写死精确版本,避免 `^` 范围日后漂移。
|
|
616
|
+
* - 发布年龄归零:见 RELEASE_AGE_OVERRIDE。
|
|
617
|
+
* - `--ignore-scripts`:不跑依赖的生命周期脚本(取舍见下)。
|
|
618
|
+
* - `--reporter=append-only`:非交互输出(不刷进度条、不重绘行)。
|
|
619
|
+
* - `--config.confirmModulesPurge=false`:永不因清空 node_modules 停下来问人。
|
|
620
|
+
*
|
|
621
|
+
* 不加 `--no-frozen-lockfile`:pnpm 11 的 `add` 不认这个选项
|
|
622
|
+
* (实测报 `Unknown option: 'frozen-lockfile'`;宿主当前自带 pnpm 11.28.5,同样如此)。
|
|
623
|
+
* `add` 本来就会更新锁文件,不需要它。
|
|
624
|
+
*
|
|
625
|
+
* `--ignore-scripts` 的取舍:这个 add 命令会连带重装 profile 里的其它依赖,
|
|
626
|
+
* 加上它会让 @dataiku/uv / @google/genai / protobufjs 等包的 postinstall 不跑。
|
|
627
|
+
* 那些脚本与本插件无关,跳过它们**大概率是好事**(少一个失败点、少一份供应面)。
|
|
628
|
+
* 代价:如果将来本插件自己需要 postinstall/preinstall/prepare,这里必须重新评估 ——
|
|
629
|
+
* 本插件当前没有这三个钩子(已核对 package.json)。
|
|
630
|
+
*
|
|
631
|
+
* 修复模式(`mode === 'repair'`)与更新模式的差别,每一条都有理由:
|
|
632
|
+
* 1. **同样是 `add`,不是 `install`**:用户原话「你更新都只更新你自己,你修复你还为什么
|
|
633
|
+
* 还要管别人?」—— `pnpm install` 会把 profile 里**全部**依赖重装一遍(十几个包),
|
|
634
|
+
* 任何一个包的 postinstall 失败都会让整条命令失败(实测 `@dataiku/uv` 就抛过
|
|
635
|
+
* `spawn EPERM`)。修复是兜底动作,不该因为别人的包而失败。
|
|
636
|
+
* 2. **目标版本取「清单里当前写的那个」**:修复不装新版本,只是让 pnpm 按现有清单
|
|
637
|
+
* 把**本插件**重新对齐。所以调用方要把 `package.json` 里现存的版本传进来;
|
|
638
|
+
* 传空就退回 `@latest`(那种情况说明清单里本来就没有它)。
|
|
639
|
+
* 3. **带 `--ignore-scripts`**:与更新模式一致。本插件没有 postinstall/preinstall/
|
|
640
|
+
* prepare(已核对 package.json),跳过脚本不会留下装不完整的自己,
|
|
641
|
+
* 却能避开别人的生命周期脚本。
|
|
642
|
+
*
|
|
643
|
+
* @param {string[]} base pnpm 自身的参数(例如打包 pnpm 的 .mjs 路径)
|
|
644
|
+
* @param {string} target 安装目标 `包名@版本`
|
|
645
|
+
* @param {'update'|'repair'} [mode] 两种模式命令相同,只有 target 的来源不同
|
|
646
|
+
* @returns {string[]}
|
|
647
|
+
*/
|
|
648
|
+
export function updateArgs(base, target, mode) {
|
|
649
|
+
/* 修复与更新用的是同一条命令:都只 `add` 本插件,只差 target 从哪来。
|
|
650
|
+
`mode` 保留在签名里是为了调用方读得懂意图,也方便将来真要分叉时有落点。 */
|
|
651
|
+
void mode
|
|
652
|
+
return [
|
|
653
|
+
...base,
|
|
654
|
+
'add',
|
|
655
|
+
'--save-exact',
|
|
656
|
+
'--ignore-scripts',
|
|
657
|
+
'--reporter=append-only',
|
|
658
|
+
'--config.confirmModulesPurge=false',
|
|
659
|
+
RELEASE_AGE_OVERRIDE,
|
|
660
|
+
target,
|
|
661
|
+
]
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* 执行一次自更新(或一次修复安装)。全程不抛异常,失败以结构化结果返回。
|
|
666
|
+
*
|
|
667
|
+
* `options.mode`:
|
|
668
|
+
* - `'update'`(默认):装一个**精确版本**,装完回读版本号核对。
|
|
669
|
+
* - `'repair'`:不装新版本,让 pnpm 按现有清单把 node_modules 对齐。
|
|
670
|
+
* 用途是「用户机器的 package.json / pnpm-lock.yaml / .modules.yaml / 实物
|
|
671
|
+
* 四处状态互相矛盾」(升级失败后残留)。它与 update 的差别见 updateArgs 的注释;
|
|
672
|
+
* 这里另外三处必须跟着变:
|
|
673
|
+
* 1. 不调 ensureReleaseAgeExclude —— 没有新版本需要放行,写豁免只会污染用户配置;
|
|
674
|
+
* 2. 跳过版本回读校验 —— repair 不改版本,拿 options.version 去比必然对不上;
|
|
675
|
+
* 3. 成功判据 = 退出码 0 且未超时。
|
|
676
|
+
*
|
|
677
|
+
* `result.to` 在 repair 模式下是 **null**:这次操作没有"目标版本",
|
|
678
|
+
* 拿 `options.version` 填进去会让人误以为修复成功就是装成了那个版本。
|
|
679
|
+
*
|
|
680
|
+
* @param {{selfDir: string, version?: string, mode?: 'update'|'repair', logger?: Function, timeoutMs?: number}} options
|
|
681
|
+
* @returns {Promise<object>}
|
|
682
|
+
*/
|
|
683
|
+
export async function runSelfUpdate(options) {
|
|
684
|
+
const selfDir = options.selfDir
|
|
685
|
+
const log = typeof options.logger === 'function' ? options.logger : () => {}
|
|
686
|
+
/** 更新还是修复;任何其它值一律按更新处理(保持旧行为)。 */
|
|
687
|
+
const mode = options.mode === 'repair' ? 'repair' : 'update'
|
|
688
|
+
const from = installedVersion(selfDir)
|
|
689
|
+
/* restartRequired 一律为 true:在 DSH 里替换已安装插件的版本**必然**需要重启,
|
|
690
|
+
刷新页面不算数 —— 插件包元数据(含 client.js 的 revision)在启动时缓存、直到重启才更新
|
|
691
|
+
(见 @deepseek-ai/dsh-hmr 的已知限制与 dsh-client-modules 的说明)。
|
|
692
|
+
不用「要不要重启」去猜,直接如实告诉界面。repair 重写的是 node_modules 里的实物,
|
|
693
|
+
同样要重启才生效,所以两种模式都是 true。 */
|
|
694
|
+
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: [] }
|
|
695
|
+
|
|
696
|
+
/* 注意:必须在找 profile 之前就报 pnpm-not-found —— 找不到 pnpm 时 profile
|
|
697
|
+
目录是否存在无关紧要,先告诉用户真正的卡点。 */
|
|
698
|
+
const pnpm = resolvePnpmDetailed()
|
|
699
|
+
if (pnpm.ok !== true) {
|
|
700
|
+
result.reason = 'pnpm-not-found'
|
|
701
|
+
/* 把试过的候选写进结果和日志:issue 里那次排查全靠手工逐条 Test-Path,
|
|
702
|
+
以后用户/开发者看日志就能定位,不用再猜插件找的是哪条路径。 */
|
|
703
|
+
result.tried = pnpm.tried
|
|
704
|
+
.slice(0, 12)
|
|
705
|
+
.map((one) => {
|
|
706
|
+
const where = one.path === null ? '—' : one.path
|
|
707
|
+
const line = one.source + ': ' + where + '(' + one.note + ')'
|
|
708
|
+
return line.length > 300 ? line.slice(0, 300) + '…' : line
|
|
709
|
+
})
|
|
710
|
+
log('update: pnpm-not-found,尝试过:')
|
|
711
|
+
for (const line of result.tried) log(' - ' + line)
|
|
712
|
+
return result
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
const runner = pnpm.runner
|
|
716
|
+
log('update: pnpm 来源 ' + pnpm.source + ' → ' + runner.command + ' ' + runner.args.join(' ') + (runner.electron === true ? '(ELECTRON_RUN_AS_NODE)' : ''))
|
|
717
|
+
|
|
718
|
+
const profileDir = findProfileDir(selfDir)
|
|
719
|
+
result.profileDir = profileDir
|
|
720
|
+
if (profileDir === null) {
|
|
721
|
+
result.reason = 'profile-not-found'
|
|
722
|
+
return result
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/* M1:快照必须在这里 —— **在 ensureReleaseAgeExclude 之前、调 pnpm 之前**。
|
|
726
|
+
为什么必须在豁免写入之前:豁免表就是 ensureReleaseAgeExclude 改的,
|
|
727
|
+
快照放在它之后,那次改动就落在快照「内侧」,回滚永远还原不掉它 ——
|
|
728
|
+
实测会留下 `dsh-desktop-statusbar@9.9.9` 这行残留,正是用户抱怨的「状态矛盾」之一。
|
|
729
|
+
放在它之前,回滚目标 = 「还没动过的用户原始状态」:pnpm 改的 package.json /
|
|
730
|
+
pnpm-lock.yaml 被还原,本次新增的豁免被还原,而用户原有的豁免本就在快照里,一行不动。 */
|
|
731
|
+
const snapshot = snapshotProfile(profileDir)
|
|
732
|
+
|
|
733
|
+
/* repair 模式**不写豁免表**:那条豁免是为了"放行某个刚发布的新版本",
|
|
734
|
+
而修复安装压根不装新版本 —— 写进去只会在用户的 pnpm-workspace.yaml 里
|
|
735
|
+
留下一条无意义的版本号,正是这个功能想清理的那种"状态矛盾"。 */
|
|
736
|
+
if (mode !== 'repair') {
|
|
737
|
+
/* 先把目标版本写进豁免表:这是 pnpm 自己的豁免机制,写进去后目标版本无论多新
|
|
738
|
+
都算「已放行」。写不成也继续 —— 下面那次运行还带着一次性归零开关兜底。 */
|
|
739
|
+
const ensured = ensureReleaseAgeExclude(profileDir, options.version)
|
|
740
|
+
result.changedExclude = ensured.changed === true
|
|
741
|
+
if (ensured.ok !== true) {
|
|
742
|
+
log('release-age: 豁免写入未成功(' + String(ensured.reason) + '),继续尝试安装')
|
|
743
|
+
} else if (ensured.changed === true) {
|
|
744
|
+
log('release-age: 已放行 ' + SELF_NAME + '@' + options.version)
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/* 精确版本,不用 @latest:后者在发布年龄策略下会静默装成旧的成熟版。
|
|
749
|
+
repair 模式的目标是「清单里当前写着的那个版本」—— 修复不装新版本,只把本插件
|
|
750
|
+
重新对齐一次;清单里读不到(还没被 pnpm 写过)就退回 @latest 兜底。 */
|
|
751
|
+
const target = mode === 'repair'
|
|
752
|
+
? SELF_NAME + '@' + repairTargetVersion(profileDir)
|
|
753
|
+
: SELF_NAME + '@' + options.version
|
|
754
|
+
const env = { ...process.env }
|
|
755
|
+
if (runner.electron === true) env.ELECTRON_RUN_AS_NODE = '1'
|
|
756
|
+
/* 这里**刻意不设 CI=true**:CI 模式下 pnpm 默认走 frozen-lockfile,
|
|
757
|
+
而本场景恰恰是「先改清单、再让 pnpm 同步锁文件」,两者天然冲突 ——
|
|
758
|
+
设了它,pnpm 会因为锁文件与清单不一致直接报错退出。
|
|
759
|
+
非交互能力改由 updateArgs 里的 --reporter=append-only 与
|
|
760
|
+
--config.confirmModulesPurge=false 显式提供,不靠 CI 环境变量。 */
|
|
761
|
+
const args = updateArgs(runner.args, target, mode)
|
|
762
|
+
|
|
763
|
+
log(mode === 'repair' ? 'update: 开始修复安装(只重装本插件,不碰其它依赖):' + target : 'update: 开始安装 ' + target)
|
|
764
|
+
const run = await runCommand(runner.command, args, {
|
|
765
|
+
cwd: profileDir,
|
|
766
|
+
env,
|
|
767
|
+
timeoutMs: options.timeoutMs,
|
|
768
|
+
})
|
|
769
|
+
const output = (run.stdout + '\n' + run.stderr).trim()
|
|
770
|
+
|
|
771
|
+
/* 判定顺序固定,不许调换:
|
|
772
|
+
1. timedOut → 'timeout'(输出不完整,按文本猜会误判)
|
|
773
|
+
2. isReleaseAgeBlocked → 'release-age'(有准确语义,优先于泛化分类)
|
|
774
|
+
3. 其余 → classifyPnpmFailure(),它内部兜底 'install-failed'
|
|
775
|
+
老的 reason 值语义一个都没动,新分类只是把 'install-failed' 拆细。 */
|
|
776
|
+
if (run.timedOut === true) {
|
|
777
|
+
result.reason = 'timeout'
|
|
778
|
+
} else if (run.exitCode !== 0) {
|
|
779
|
+
result.reason = isReleaseAgeBlocked(output) ? 'release-age' : classifyPnpmFailure(output)
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
if (result.reason !== null) {
|
|
783
|
+
result.detail = detailOf(output)
|
|
784
|
+
return finishFailure(result, { snapshot, run, output, args, runner, profileDir, log })
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/* repair 不改版本,没有"装没装上某个版本"可核对 —— 成功判据就是退出码 0 且未超时
|
|
788
|
+
(上面两个分支已经把超时与非零退出拦掉了)。修复本来就可能什么都没改
|
|
789
|
+
(四处状态已经一致),这时 pnpm 一样退出 0,也算成功。 */
|
|
790
|
+
if (mode === 'repair') {
|
|
791
|
+
result.ok = true
|
|
792
|
+
log('update: 修复安装完成(退出码 0)')
|
|
793
|
+
return result
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/* 不能只看退出码:发布年龄策略下 pnpm 可能「成功」地装了旧版。
|
|
797
|
+
必须回读盘上的实际版本,这是唯一可信的成功判据。 */
|
|
798
|
+
const now = installedVersion(selfDir)
|
|
799
|
+
if (now === null) {
|
|
800
|
+
result.reason = 'verify-failed'
|
|
801
|
+
return finishFailure(result, { snapshot, run, output, args, runner, profileDir, log })
|
|
802
|
+
}
|
|
803
|
+
if (now !== options.version) {
|
|
804
|
+
result.reason = 'version-mismatch'
|
|
805
|
+
result.detail = detailOf('期望 ' + options.version + ',实际 ' + now)
|
|
806
|
+
return finishFailure(result, { snapshot, run, output, args, runner, profileDir, log })
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
result.ok = true
|
|
810
|
+
log('update: 已更新到 ' + now)
|
|
811
|
+
return result
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* 失败收尾:回滚那三份文件,再把结构化失败信息挂到 result 上。
|
|
816
|
+
*
|
|
817
|
+
* 为什么单独抽一个函数:失败有四个出口(超时 / 非零退出 / verify-failed /
|
|
818
|
+
* version-mismatch),回滚与 failureReport 必须在**每一个**出口都发生,
|
|
819
|
+
* 抄四遍迟早漏一处。这里是唯一出口。
|
|
820
|
+
*
|
|
821
|
+
* 关于 failureReport 的分工:runSelfUpdate 是 lib 层、**不能发 HTTP**
|
|
822
|
+
* (宿主那边负责路由与上报)。这里只把结构化信息放进 result,
|
|
823
|
+
* 由宿主的 index.js 读取后去 POST。本函数里不许出现任何网络调用。
|
|
824
|
+
*
|
|
825
|
+
* @param {object} result
|
|
826
|
+
* @param {{
|
|
827
|
+
* snapshot: Map<string, object>, run: object, output: string,
|
|
828
|
+
* args: string[], runner: object, profileDir: string, log: Function,
|
|
829
|
+
* }} ctx
|
|
830
|
+
* @returns {object} 同一个 result(原地改完返回,方便调用方直接 return)
|
|
831
|
+
*/
|
|
832
|
+
function finishFailure(result, ctx) {
|
|
833
|
+
const rollback = rollbackProfile(ctx.snapshot)
|
|
834
|
+
result.rolledBack = rollback.ok === true
|
|
835
|
+
|
|
836
|
+
if (rollback.ok !== true) {
|
|
837
|
+
/* 回滚自身抛错:reason 换成 rollback-failed,但原 pnpm 输出必须留在 detail 里,
|
|
838
|
+
否则「环境为什么脏」和「本来在干什么」这两条线索会丢一条。 */
|
|
839
|
+
result.reason = 'rollback-failed'
|
|
840
|
+
result.detail = rollbackFailureDetail(rollback.error, ctx.output)
|
|
841
|
+
} else if (rollback.restored.length > 0 || rollback.removed.length > 0) {
|
|
842
|
+
ctx.log('update: 已回滚 ' + rollback.restored.map((one) => basename(one)).join(', ')
|
|
843
|
+
+ (rollback.removed.length > 0
|
|
844
|
+
? ';已删除 ' + rollback.removed.map((one) => basename(one)).join(', ')
|
|
845
|
+
: ''))
|
|
846
|
+
} else {
|
|
847
|
+
ctx.log('update: 无需回滚(三份文件与快照逐字节一致)')
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
result.failureReport = {
|
|
851
|
+
stage: 'host-install',
|
|
852
|
+
/* mode 一并带出去:界面要在"更新失败"和"修复失败"之间区分文案,
|
|
853
|
+
宿主的日志也要能分清这次到底跑的是哪条命令。 */
|
|
854
|
+
mode: result.mode,
|
|
855
|
+
reason: result.reason,
|
|
856
|
+
from: result.from,
|
|
857
|
+
to: result.to,
|
|
858
|
+
exitCode: ctx.run.exitCode,
|
|
859
|
+
timedOut: ctx.run.timedOut === true,
|
|
860
|
+
command: ctx.runner.command,
|
|
861
|
+
args: ctx.args,
|
|
862
|
+
cwd: ctx.profileDir,
|
|
863
|
+
stdout: ctx.run.stdout,
|
|
864
|
+
stderr: ctx.run.stderr,
|
|
865
|
+
rolledBack: result.rolledBack === true,
|
|
866
|
+
}
|
|
867
|
+
return result
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* 带并发保护地执行自更新(或修复安装):同一时刻只跑一个。
|
|
872
|
+
*
|
|
873
|
+
* `options.mode` 原样透传给 runSelfUpdate(见那里的说明)。
|
|
874
|
+
*
|
|
875
|
+
* @param {{selfDir: string, version?: string, mode?: 'update'|'repair', logger?: Function, timeoutMs?: number}} options
|
|
876
|
+
* @returns {Promise<object>}
|
|
877
|
+
*/
|
|
878
|
+
export async function selfUpdateOnce(options) {
|
|
879
|
+
const mode = options.mode === 'repair' ? 'repair' : 'update'
|
|
880
|
+
if (running !== null) return { ok: false, mode, reason: 'busy', busy: true }
|
|
881
|
+
const task = (async () => {
|
|
882
|
+
try {
|
|
883
|
+
const value = await runSelfUpdate(options)
|
|
884
|
+
lastResult = value
|
|
885
|
+
return value
|
|
886
|
+
} catch (error) {
|
|
887
|
+
const failure = {
|
|
888
|
+
ok: false,
|
|
889
|
+
mode,
|
|
890
|
+
from: null,
|
|
891
|
+
to: mode === 'repair' ? null : options.version,
|
|
892
|
+
reason: 'unexpected',
|
|
893
|
+
detail: String(error && error.message),
|
|
894
|
+
}
|
|
895
|
+
lastResult = failure
|
|
896
|
+
return failure
|
|
897
|
+
} finally {
|
|
898
|
+
running = null
|
|
899
|
+
}
|
|
900
|
+
})()
|
|
901
|
+
running = task
|
|
902
|
+
return task
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/** 更新是否正在进行。 */
|
|
906
|
+
export function isUpdating() {
|
|
907
|
+
return running !== null
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/** 最近一次更新的结果;没跑过就是 null。 */
|
|
911
|
+
export function lastUpdateResult() {
|
|
912
|
+
return lastResult
|
|
913
|
+
}
|