dsh-pick 0.4.2 → 0.4.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/lib/remote.js +22 -3
- package/package.json +1 -1
- package/plugin/index.js +11 -5
package/lib/remote.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
//
|
|
8
8
|
// 镜像按顺序试:jsDelivr 国内一般能直连;raw.githubusercontent.com 兜底。
|
|
9
9
|
|
|
10
|
-
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
10
|
+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
|
|
11
11
|
import { dirname, join } from 'node:path'
|
|
12
12
|
import { cachePath, dshHome, loadCatalog, newerCatalog, reloadCatalog } from './engine.js'
|
|
13
13
|
|
|
@@ -30,11 +30,30 @@ async function readState(env) {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
// Windows 上目标文件被别的进程短暂打开(杀毒软件扫描、DSH 正在读)时,替换会报这几种错;
|
|
34
|
+
// 等一下通常就好了
|
|
35
|
+
const BUSY = new Set(['EPERM', 'EBUSY', 'EACCES'])
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 先写临时文件再改名替换:替换是原子的,读的一方要么看到旧文件、要么看到完整的新文件。
|
|
39
|
+
* 被占用就退避重试几次;最后还是不行,删掉临时文件、把错抛出去(这一轮不换,下一轮再试)
|
|
40
|
+
*/
|
|
41
|
+
export async function writeAtomic(file, text, { tries = 5, wait = 150, renameImpl = rename } = {}) {
|
|
34
42
|
await mkdir(dirname(file), { recursive: true })
|
|
35
43
|
const tmp = `${file}.${process.pid}.tmp`
|
|
36
44
|
await writeFile(tmp, text, 'utf8')
|
|
37
|
-
|
|
45
|
+
for (let i = 1; ; i++) {
|
|
46
|
+
try {
|
|
47
|
+
await renameImpl(tmp, file)
|
|
48
|
+
return
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (!BUSY.has(e && e.code) || i >= tries) {
|
|
51
|
+
await rm(tmp, { force: true }).catch(() => {})
|
|
52
|
+
throw e
|
|
53
|
+
}
|
|
54
|
+
await new Promise(r => setTimeout(r, wait * 2 ** (i - 1)))
|
|
55
|
+
}
|
|
56
|
+
}
|
|
38
57
|
}
|
|
39
58
|
|
|
40
59
|
async function getJson(fetchImpl, url, timeoutMs) {
|
package/package.json
CHANGED
package/plugin/index.js
CHANGED
|
@@ -108,9 +108,15 @@ function managerCode(result) {
|
|
|
108
108
|
* 官方桌面版(0.2.1-rc.2 实测)的插件管理器不能在运行中升级已装的插件:installBundle 一律答
|
|
109
109
|
* already-installed(插件市场的「更新」也栽在这里)。只能退出 DSH、在终端用 dsh plugin 装
|
|
110
110
|
*/
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
'
|
|
111
|
+
export function alreadyInstalledHint(platform = process.platform) {
|
|
112
|
+
// 「完全退出」各系统做法不一样:关窗口不算,DSH 还在后台跑
|
|
113
|
+
const quit = platform === 'darwin' ? '按 Cmd+Q,不是关窗口'
|
|
114
|
+
: platform === 'win32' ? '在任务栏右下角的托盘图标上右键 → 退出,不是关窗口'
|
|
115
|
+
: '关掉所有窗口,并确认 DSH 进程已经结束'
|
|
116
|
+
const terminal = platform === 'win32' ? 'PowerShell 或命令提示符' : '终端'
|
|
117
|
+
return `这个版本的 DSH 不能在运行中升级已装的插件。请完全退出 DSH(${quit}),` +
|
|
118
|
+
`在${terminal}里执行下面的命令,再重新打开 DSH。如果提示版本太新(发布不满 1 天),过一天再执行。`
|
|
119
|
+
}
|
|
114
120
|
|
|
115
121
|
function managerError(result) {
|
|
116
122
|
if (!(result && (result.application === 'failed' || result.application === 'cancelled' || result.error))) return ''
|
|
@@ -315,12 +321,12 @@ export function apply(ctx) {
|
|
|
315
321
|
}
|
|
316
322
|
try {
|
|
317
323
|
const result = await pluginManager.installBundle(`${PACKAGE}@${latest}`, { enabled: true })
|
|
318
|
-
if (managerCode(result) === 'already-installed') return sendJson(res, 200, { ok: false, message:
|
|
324
|
+
if (managerCode(result) === 'already-installed') return sendJson(res, 200, { ok: false, message: alreadyInstalledHint(), command })
|
|
319
325
|
const err = managerError(result)
|
|
320
326
|
if (err) return sendJson(res, 200, { ok: false, message: `更新失败(${err})`, command })
|
|
321
327
|
return sendJson(res, 200, { ok: true, version: latest })
|
|
322
328
|
} catch (e) {
|
|
323
|
-
if (e && e.code === 'already-installed') return sendJson(res, 200, { ok: false, message:
|
|
329
|
+
if (e && e.code === 'already-installed') return sendJson(res, 200, { ok: false, message: alreadyInstalledHint(), command })
|
|
324
330
|
return sendJson(res, 200, { ok: false, message: `更新失败:${e.message}`, command })
|
|
325
331
|
}
|
|
326
332
|
},
|