dsh-config-sync-plugin 0.3.70 → 0.3.71

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 CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 dsh-config-sync contributors
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 dsh-config-sync contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.en.md CHANGED
@@ -19,7 +19,7 @@ The package name is **`dsh-config-sync-plugin`** (the `dsh-config-sync` name on
19
19
 
20
20
  | Way | Command / entry | Notes |
21
21
  |---|---|---|
22
- | npm | `npm i dsh-config-sync-plugin` | Use it as a dependency, or add `"dsh-config-sync-plugin": "^0.3.70"` to the profile's `package.json` and run `pnpm i`. The name matches the `cordis.patch.yml` insert name and the install folder `dsh-config-sync-plugin-<ver>` |
22
+ | npm | `npm i dsh-config-sync-plugin` | Use it as a dependency, or add `"dsh-config-sync-plugin": "^0.3.71"` to the profile's `package.json` and run `pnpm i`. The name matches the `cordis.patch.yml` insert name and the install folder `dsh-config-sync-plugin-<ver>` |
23
23
  | Marketplace | client → Plugins → Discover → “Config Sync” | Installs from the git commit registered in `market-index.json` |
24
24
  | Offline tgz | `releases\dsh-config-sync-plugin-<ver>.tgz` | GitHub Release asset for air-gapped machines; the panel's Update button downloads the same file |
25
25
 
package/README.md CHANGED
@@ -15,7 +15,7 @@ DSH 插件:左侧栏多一行「配置同步」,点开是整页体检面板
15
15
 
16
16
  | 方式 | 命令 / 入口 | 说明 |
17
17
  |---|---|---|
18
- | npm | `npm i dsh-config-sync-plugin` | 适合把它当依赖管理,或在 profile 的 `package.json` 里写 `"dsh-config-sync-plugin": "^0.3.70"` 后 `pnpm i`;包名与 `cordis.patch.yml` 里注册的名字、插件目录 `dsh-config-sync-plugin-<ver>` 一致 |
18
+ | npm | `npm i dsh-config-sync-plugin` | 适合把它当依赖管理,或在 profile 的 `package.json` 里写 `"dsh-config-sync-plugin": "^0.3.71"` 后 `pnpm i`;包名与 `cordis.patch.yml` 里注册的名字、插件目录 `dsh-config-sync-plugin-<ver>` 一致 |
19
19
  | 插件市场 | 客户端「插件市场 → 发现」搜「配置同步」 | 走 `market-index.json` 登记的 git 提交,版本与 README 对齐 |
20
20
  | 离线 tgz | `releases\dsh-config-sync-plugin-<ver>.tgz` | GitHub Release 附件,适合内网机器;面板「更新」按钮下载的也是它 |
21
21
 
package/client.js CHANGED
@@ -3212,7 +3212,7 @@ window.__ModuleLoader__.load({
3212
3212
  } catch { /* 插件路由还没起好就静默跳过,面板里仍会显示同一结果 */ }
3213
3213
  }
3214
3214
 
3215
- // ==================== 0.3.70:会话缓存守卫(客户端半身) ====================
3215
+ // ==================== 0.3.71:会话缓存守卫(客户端半身) ====================
3216
3216
  // 宿主侧没法拦 agent-busy(它是 sessionController 内部抛的 RemoteError),
3217
3217
  // 所以在这里包一层 sessions.create:撞上就上报、清缓存、去掉旧 sessionId 重试一次。
3218
3218
  const BUSY_RE = /session\/agent-busy|is owned by subagent routing/i
@@ -3309,7 +3309,7 @@ window.__ModuleLoader__.load({
3309
3309
  // 界面还没完全起来时 fetch 可能扑空,所以稍等一会儿再查;只查一次,不轮询。
3310
3310
  try { setTimeout(announceLastResult, 1500) } catch { }
3311
3311
 
3312
- // 0.3.70:给客户端的 sessions 服务套上 agent-busy 自愈钩子。
3312
+ // 0.3.71:给客户端的 sessions 服务套上 agent-busy 自愈钩子。
3313
3313
  // 拿不到 sessions(老版本宿主 / 服务还没起来)就静默跳过,面板功能不受影响。
3314
3314
  try {
3315
3315
  if (typeof ctx.get === 'function') {
package/index.js CHANGED
@@ -24,7 +24,7 @@ import { closeSync, existsSync, mkdirSync, mkdtempSync, openSync, readdirSync, r
24
24
  import { homedir, hostname, tmpdir } from 'node:os'
25
25
  import { basename, dirname, isAbsolute, join, relative } from 'node:path'
26
26
  import { fileURLToPath } from 'node:url'
27
- // 0.3.70:会话投影缓存守卫(agent-busy 自动清缓存,让缓存按日志重建)
27
+ // 0.3.71:会话投影缓存守卫(agent-busy 自动清缓存,让缓存按日志重建)
28
28
  import { runGuard, clearSessionCaches, appendGuardLog } from './runtime/tools/session-cache-guard.mjs'
29
29
 
30
30
  const pluginRequire = createRequire(import.meta.url)
@@ -39,7 +39,7 @@ const ROUTE_CONFLICT = '/config-sync/conflict'
39
39
  // 0.3.30:跨机路径确认(每台机器把自己的根目录报给插件一次)与问题反馈(落仓库 feedback\ 再推 GitHub)
40
40
  const ROUTE_ROOTS = '/config-sync/roots'
41
41
  const ROUTE_FEEDBACK = '/config-sync/feedback'
42
- // 0.3.70:会话投影缓存守卫(GET 只读扫描 / POST scan|sweep|clear|busy)
42
+ // 0.3.71:会话投影缓存守卫(GET 只读扫描 / POST scan|sweep|clear|busy)
43
43
  const ROUTE_CACHE_GUARD = '/config-sync/cache-guard'
44
44
  const SETTINGS_NAME = 'dsh-config-sync.json'
45
45
  /** 本插件自己的安装目录(插件根,含 runtime\ 与 index.js)—— 插件备份还原时的目标。 */
@@ -237,7 +237,7 @@ function readSettings(dshHome) {
237
237
  // 同步完成重启前把 DSH 的「默认工作区」校正到谁:
238
238
  // 留空 = 自动找名字/路径是 default-workspace 的工作区;可填工作区路径或 ID;填 off 关闭。
239
239
  defaultWorkspace: typeof raw.defaultWorkspace === 'string' ? raw.defaultWorkspace : '',
240
- // 0.3.70:会话投影缓存守卫,默认开。关掉后既不定时扫描,也不理会客户端的 agent-busy 上报。
240
+ // 0.3.71:会话投影缓存守卫,默认开。关掉后既不定时扫描,也不理会客户端的 agent-busy 上报。
241
241
  cacheGuard: raw.cacheGuard !== false,
242
242
  }
243
243
  }
@@ -1180,7 +1180,7 @@ async function inspect(config) {
1180
1180
  const pending = c.items.filter((it) => !it.choice).length
1181
1181
  return { exists: true, total: c.items.length, pending, at: c.at || '', backupDir: c.backupDir || '' }
1182
1182
  })(),
1183
- // 0.3.70:会话投影缓存守卫状态(面板「会话缓存守卫」卡用)
1183
+ // 0.3.71:会话投影缓存守卫状态(面板「会话缓存守卫」卡用)
1184
1184
  cacheGuard: cacheGuardSnapshot(config),
1185
1185
  }
1186
1186
  }
@@ -1962,7 +1962,7 @@ async function handleSettings(req, res, config) {
1962
1962
  const autoPull = has('autoPull') ? body.autoPull === true : before.autoPull
1963
1963
  const autoPush = has('autoPush') ? body.autoPush === true : before.autoPush
1964
1964
  const autoRestart = has('autoRestart') ? body.autoRestart !== false : before.autoRestart
1965
- // 0.3.70:会话投影缓存守卫开关,字段没给就沿用原值
1965
+ // 0.3.71:会话投影缓存守卫开关,字段没给就沿用原值
1966
1966
  const cacheGuard = has('cacheGuard') ? body.cacheGuard !== false : before.cacheGuard
1967
1967
 
1968
1968
  if (!has('repoPath') && !has('remoteUrl') && !has('sshKey') &&
@@ -4022,7 +4022,7 @@ function newIssueUrl(repo, title, text, config) {
4022
4022
  return `https://github.com/${repo}/issues/new?${q}`
4023
4023
  }
4024
4024
 
4025
- // ============================ 0.3.70:会话投影缓存守卫 ============================
4025
+ // ============================ 0.3.71:会话投影缓存守卫 ============================
4026
4026
  // 背景:UI 点「新建会话」时会挑一条 summary.blank===true 的会话复用,挑中了 origin=subagent
4027
4027
  // 的子会话就会被宿主拒绝(session/agent-busy:is owned by subagent routing)。
4028
4028
  // 起因是投影缓存停在 session/created 那一刻(blank=true、seq=-1),而会话日志早就推进过,
@@ -4430,7 +4430,7 @@ export function apply(ctx, config) {
4430
4430
  path: ROUTE_FEEDBACK,
4431
4431
  handler: withLang(handleFeedback),
4432
4432
  }))
4433
- // 0.3.70:会话投影缓存守卫(GET 只读扫描 / POST scan|sweep|clear|busy)
4433
+ // 0.3.71:会话投影缓存守卫(GET 只读扫描 / POST scan|sweep|clear|busy)
4434
4434
  scoped.effect(() => scoped.webServer.register({
4435
4435
  kind: 'exact',
4436
4436
  path: ROUTE_CACHE_GUARD,
@@ -4443,7 +4443,7 @@ export function apply(ctx, config) {
4443
4443
  // status 不用再为首屏那 2.5 秒的 ls-remote 埋单。
4444
4444
  typeof setTimeout === 'function' ? setTimeout(() => { try { warmRemoteProbe(config) } catch { } }, 300) : null
4445
4445
  installQuitWhenAllWindowsClosed(ctx)
4446
- // 0.3.70:会话投影缓存守卫的定时扫描(默认开,设置里可关)
4446
+ // 0.3.71:会话投影缓存守卫的定时扫描(默认开,设置里可关)
4447
4447
  scheduleCacheGuard(config)
4448
4448
  } catch (error) {
4449
4449
  // 不抛:缺 webServer 的 profile 里这个插件静默不激活即可
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-config-sync-plugin",
3
- "version": "0.3.70",
3
+ "version": "0.3.71",
4
4
  "type": "module",
5
5
  "description": "在 DSH 设置里查看 dsh-config 仓库与本机的同步状态,并生成退出后执行的 push/pull 脚本。同步动作本身不在应用内执行。面板与文案跟随客户端语言(中文/English)。",
6
6
  "main": "./index.js",
package/runtime/apply.ps1 CHANGED
@@ -898,26 +898,10 @@ if ($dropped.Count -gt 0) { Ok "已剔除 $($dropped.Count) 个实验性 bundle
898
898
  $localPluginMap = [ordered]@{
899
899
  'dsh-our-free-model-2.8.0' = 'dsh-our-free-model'
900
900
  'dsh-purge-1.4.0' = 'dsh-purge'
901
- 'dsh-config-sync-plugin-0.3.69' = 'dsh-config-sync-plugin'
902
- 'dsh-config-sync-plugin-0.3.70' = 'dsh-config-sync-plugin'
903
- 'dsh-config-sync-plugin-0.3.68' = 'dsh-config-sync-plugin'
904
- 'dsh-config-sync-plugin-0.3.67' = 'dsh-config-sync-plugin'
905
- 'dsh-config-sync-plugin-0.3.66' = 'dsh-config-sync-plugin'
906
- 'dsh-config-sync-plugin-0.3.65' = 'dsh-config-sync-plugin'
907
- 'dsh-config-sync-plugin-0.3.64' = 'dsh-config-sync-plugin'
908
- 'dsh-config-sync-plugin-0.3.63' = 'dsh-config-sync-plugin'
909
- 'dsh-config-sync-plugin-0.3.62' = 'dsh-config-sync-plugin'
910
- 'dsh-config-sync-plugin-0.3.61' = 'dsh-config-sync-plugin'
911
- 'dsh-config-sync-plugin-0.3.59' = 'dsh-config-sync-plugin'
912
- 'dsh-config-sync-plugin-0.3.58' = 'dsh-config-sync-plugin'
913
- 'dsh-config-sync-plugin-0.3.57' = 'dsh-config-sync-plugin'
914
- 'dsh-config-sync-plugin-0.3.56' = 'dsh-config-sync-plugin'
901
+ 'dsh-config-sync-plugin-0.3.70' = 'dsh-config-sync-plugin'
902
+ 'dsh-config-sync-plugin-0.3.71' = 'dsh-config-sync-plugin'
915
903
  'dsh-config-sync-0.1.0' = 'dsh-config-sync'
916
904
  }
917
- foreach ($dirName in $localPluginMap.Keys) {
918
- $pattern = 'link:[^"]*' + [regex]::Escape($dirName)
919
- $pkgRaw = [regex]::Replace($pkgRaw, $pattern, ('link:' + $appRootFwd + '/' + $dirName), 'IgnoreCase')
920
- }
921
905
  Write-TextPreserveBom (Join-Path $ProfileDir 'package.json') $pkgRaw
922
906
  Ok "已写入 package.json(link 路径指向 $appRootFwd)"
923
907
  } else { Warn '跳过(未勾选同步):profile 配置(package.json / lock / cordis 保持本机现状)' }
@@ -1283,7 +1267,7 @@ foreach ($dirName in $localPluginMap.Keys) {
1283
1267
  }
1284
1268
  $hasNewVersion = $false
1285
1269
  if ((Test-Path -LiteralPath $arch) -and $archVer -and $depVer) {
1286
- $hasNewVersion = ($archVer -ne $depVer)
1270
+ $hasNewVersion = ([version]$archVer -gt [version]$depVer)
1287
1271
  } elseif (Test-Path -LiteralPath $arch) {
1288
1272
  # 读不到版本(老归档、结构异常)时退回 mtime,但要求比已部署目录新 10 秒以上,避免每轮误判
1289
1273
  $archTime = (Get-Item -LiteralPath $arch).LastWriteTime
@@ -1,179 +1,179 @@
1
- <#
2
- 运行中把插件新版本装进插件目录(**不退出** DSH)。
3
-
4
- 由宿主的 install-live 动作调用:
5
- powershell -NoProfile -ExecutionPolicy Bypass -File install-plugin.ps1 `
6
- -PluginDir <AppRoot>\dsh-config-sync-<ver> -Version 0.3.4 -Sha256 <hex> `
7
- -TgzUrls "https://cdn.jsdelivr.net/.../0.3.4.tgz|https://raw.githubusercontent.com/.../0.3.4.tgz" `
8
- -DshHome C:\Users\xx\.dsh
9
-
10
- 要点
11
- - 下载地址是**候选列表**(| 分隔),按顺序试,哪个先成功就用哪个:国内直连 raw.githubusercontent.com
12
- 经常不通,CDN 兜在前面才不至于"检查能过、下载过不去"。
13
- - 校验 sha256 → 解包 → 比对包里的 version 与清单一致,三步都过才动插件目录。
14
- - 动手前整目录备份到 <DshHome>\backups\plugin-<旧版本>-<时间戳>;覆盖失败就把它拷回去。
15
- - 不重启、不关 DSH:覆盖的是磁盘文件,进程里跑的还是旧代码,所以要提示"重启生效"。
16
- - 结果固定打一行 RESULT:{json} 给宿主解析(ok / version / backup / log / error)。
17
- #>
18
- param(
19
- [Parameter(Mandatory = $true)][string]$PluginDir,
20
- [string]$Version = '',
21
- [string]$Sha256 = '',
22
- # 候选下载地址,用 | 分隔
23
- [string]$TgzUrls = '',
24
- # 已经下好的本地包(有就不用下载)
25
- [string]$TgzFile = '',
26
- [string]$DshHome = $(Join-Path $env:USERPROFILE '.dsh'),
27
- [string]$LogFile = ''
28
- )
29
-
30
- $ErrorActionPreference = 'Continue'
31
- $stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
32
-
33
- if (-not $LogFile) {
34
- $logDir = Join-Path $DshHome 'logs'
35
- if (-not (Test-Path -LiteralPath $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }
36
- $LogFile = Join-Path $logDir ("plugin-install-{0}.log" -f $stamp)
37
- }
38
- function Log([string]$text) {
39
- $line = "[{0}] {1}" -f (Get-Date -Format 'HH:mm:ss'), $text
40
- try { Add-Content -LiteralPath $LogFile -Value $line -Encoding utf8 } catch { }
41
- }
42
-
43
- function EmitResult([bool]$ok, [string]$errorText, [string]$backupPath) {
44
- $obj = [ordered]@{
45
- ok = $ok
46
- version = $Version
47
- backup = $backupPath
48
- log = $LogFile
49
- error = $errorText
50
- }
51
- # 宿主只认这一行,别把它混进进度输出里
52
- Write-Output ("RESULT:" + ($obj | ConvertTo-Json -Compress -Depth 3))
53
- exit 0
54
- }
55
-
56
- # 失败时用备份把插件目录恢复成动手前的样子
57
- function Restore-Backup([string]$backup, [string]$target) {
58
- if (-not $backup -or -not (Test-Path -LiteralPath $backup)) { return $false }
59
- try {
60
- foreach ($it in @(Get-ChildItem -LiteralPath $target -Force)) {
61
- Remove-Item -LiteralPath $it.FullName -Recurse -Force -ErrorAction SilentlyContinue
62
- }
63
- foreach ($it in @(Get-ChildItem -LiteralPath $backup -Force)) {
64
- Copy-Item -LiteralPath $it.FullName -Destination $target -Recurse -Force
65
- }
66
- return $true
67
- } catch {
68
- Log ("回滚失败:{0}" -f $_.Exception.Message)
69
- return $false
70
- }
71
- }
72
-
73
- Log ("==== 安装插件 {0} 到 {1} ====" -f $Version, $PluginDir)
74
-
75
- if (-not (Test-Path -LiteralPath $PluginDir)) { EmitResult $false ("找不到插件目录:" + $PluginDir) '' }
76
-
77
- $currentVersion = ''
78
- try { $currentVersion = (Get-Content -LiteralPath (Join-Path $PluginDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version } catch { }
79
- Log ("当前版本:{0}" -f $currentVersion)
80
-
81
- $tmp = Join-Path $env:TEMP ("dsh-plugin-live-{0}-{1}.tgz" -f $Version, $stamp)
82
- $pkgPath = ''
83
- if ($TgzFile -and (Test-Path -LiteralPath $TgzFile)) {
84
- $pkgPath = $TgzFile
85
- Log ("使用本地包:{0}" -f $pkgPath)
86
- } else {
87
- $urls = @($TgzUrls -split '\|' | Where-Object { $_ -and $_.Trim() })
88
- if ($urls.Count -eq 0) { EmitResult $false '没有给下载地址,也没有本地包' '' }
89
- $errors = @()
90
- foreach ($u in $urls) {
91
- Log ("尝试下载:{0}" -f $u)
92
- try {
93
- Invoke-WebRequest -Uri $u.Trim() -OutFile $tmp -UseBasicParsing -TimeoutSec 90
94
- if ((Test-Path -LiteralPath $tmp) -and ((Get-Item -LiteralPath $tmp).Length -gt 0)) {
95
- $pkgPath = $tmp
96
- Log ("下载成功:{0:N0} 字节" -f (Get-Item -LiteralPath $tmp).Length)
97
- break
98
- }
99
- $errors += ("{0}:文件没有落盘" -f $u)
100
- } catch {
101
- $errors += ("{0}:{1}" -f $u, $_.Exception.Message)
102
- Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
103
- }
104
- }
105
- if (-not $pkgPath) {
106
- $msg = '下载安装包失败(每个地址都试过了):' + ($errors -join ';')
107
- Log $msg
108
- EmitResult $false $msg ''
109
- }
110
- }
111
-
112
- if ($Sha256) {
113
- $hash = (Get-FileHash -LiteralPath $pkgPath -Algorithm SHA256).Hash.ToLower()
114
- Log ("sha256:{0}" -f $hash)
115
- if ($hash -ne $Sha256.ToLower()) {
116
- Log 'sha256 与清单不一致,放弃安装'
117
- Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
118
- EmitResult $false '安装包 sha256 校验不通过(没下载全或被改动)' ''
119
- }
120
- Log 'sha256 校验通过'
121
- }
122
-
123
- $tar = Join-Path $env:SystemRoot 'System32\tar.exe'
124
- if (-not (Test-Path -LiteralPath $tar)) { $tar = 'tar.exe' }
125
- $stage = Join-Path $env:TEMP ("dsh-plugin-live-stage-" + $stamp)
126
- $payload = ''
127
- try {
128
- if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force }
129
- New-Item -ItemType Directory -Path $stage -Force | Out-Null
130
- & $tar -xzf $pkgPath -C $stage *> $null
131
- if ($LASTEXITCODE -ne 0) { throw ("解包失败,退出码 {0}" -f $LASTEXITCODE) }
132
- $payload = Join-Path $stage 'package'
133
- if (-not (Test-Path -LiteralPath $payload)) { throw '安装包结构不对(里面没有 package 目录)' }
134
- $newVersion = (Get-Content -LiteralPath (Join-Path $payload 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version
135
- Log ("包内版本:{0}" -f $newVersion)
136
- if ($Version -and $newVersion -ne $Version) { throw ("安装包里的版本是 {0},与清单 {1} 不一致" -f $newVersion, $Version) }
137
- } catch {
138
- Log ("解包/校验失败:{0}" -f $_.Exception.Message)
139
- if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
140
- Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
141
- EmitResult $false ('安装包有问题:' + $_.Exception.Message) ''
142
- }
143
-
144
- # 整目录备份:写坏了能手动拷回去,也能自动回滚
145
- $backup = Join-Path (Join-Path $DshHome 'backups') ("plugin-{0}-{1}" -f $(if ($currentVersion) { $currentVersion } else { 'unknown' }), $stamp)
146
- $backupOk = $false
147
- try {
148
- New-Item -ItemType Directory -Path (Split-Path -Parent $backup) -Force | Out-Null
149
- Copy-Item -LiteralPath $PluginDir -Destination $backup -Recurse -Force
150
- $backupOk = $true
151
- Log ("旧版已备份:{0}" -f $backup)
152
- } catch {
153
- Log ("备份失败(继续安装,出问题就没有自动回滚了):{0}" -f $_.Exception.Message)
154
- }
155
-
156
- Log '覆盖安装…'
157
- $installError = ''
158
- try {
159
- foreach ($it in @(Get-ChildItem -LiteralPath $payload -Force)) {
160
- Copy-Item -LiteralPath $it.FullName -Destination $PluginDir -Recurse -Force
161
- }
162
- $installed = (Get-Content -LiteralPath (Join-Path $PluginDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version
163
- Log ("安装完成,插件目录现在指向版本 {0}" -f $installed)
164
- } catch {
165
- $installError = $_.Exception.Message
166
- }
167
-
168
- if ($installError) {
169
- $restored = Restore-Backup $(if ($backupOk) { $backup } else { '' }) $PluginDir
170
- $note = if ($restored) { '已用备份回滚到安装前的状态' } else { '没能自动回滚,手动把 ' + $backup + ' 拷回去' }
171
- Log ("写入失败:{0}({1})" -f $installError, $note)
172
- if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
173
- Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
174
- EmitResult $false ("写入失败:" + $installError + '(' + $note + ')') $(if ($backupOk) { $backup } else { '' })
175
- }
176
-
177
- if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
178
- Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
179
- EmitResult $true '' $(if ($backupOk) { $backup } else { '' })
1
+ <#
2
+ 运行中把插件新版本装进插件目录(**不退出** DSH)。
3
+
4
+ 由宿主的 install-live 动作调用:
5
+ powershell -NoProfile -ExecutionPolicy Bypass -File install-plugin.ps1 `
6
+ -PluginDir <AppRoot>\dsh-config-sync-<ver> -Version 0.3.4 -Sha256 <hex> `
7
+ -TgzUrls "https://cdn.jsdelivr.net/.../0.3.4.tgz|https://raw.githubusercontent.com/.../0.3.4.tgz" `
8
+ -DshHome C:\Users\xx\.dsh
9
+
10
+ 要点
11
+ - 下载地址是**候选列表**(| 分隔),按顺序试,哪个先成功就用哪个:国内直连 raw.githubusercontent.com
12
+ 经常不通,CDN 兜在前面才不至于"检查能过、下载过不去"。
13
+ - 校验 sha256 → 解包 → 比对包里的 version 与清单一致,三步都过才动插件目录。
14
+ - 动手前整目录备份到 <DshHome>\backups\plugin-<旧版本>-<时间戳>;覆盖失败就把它拷回去。
15
+ - 不重启、不关 DSH:覆盖的是磁盘文件,进程里跑的还是旧代码,所以要提示"重启生效"。
16
+ - 结果固定打一行 RESULT:{json} 给宿主解析(ok / version / backup / log / error)。
17
+ #>
18
+ param(
19
+ [Parameter(Mandatory = $true)][string]$PluginDir,
20
+ [string]$Version = '',
21
+ [string]$Sha256 = '',
22
+ # 候选下载地址,用 | 分隔
23
+ [string]$TgzUrls = '',
24
+ # 已经下好的本地包(有就不用下载)
25
+ [string]$TgzFile = '',
26
+ [string]$DshHome = $(Join-Path $env:USERPROFILE '.dsh'),
27
+ [string]$LogFile = ''
28
+ )
29
+
30
+ $ErrorActionPreference = 'Continue'
31
+ $stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
32
+
33
+ if (-not $LogFile) {
34
+ $logDir = Join-Path $DshHome 'logs'
35
+ if (-not (Test-Path -LiteralPath $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }
36
+ $LogFile = Join-Path $logDir ("plugin-install-{0}.log" -f $stamp)
37
+ }
38
+ function Log([string]$text) {
39
+ $line = "[{0}] {1}" -f (Get-Date -Format 'HH:mm:ss'), $text
40
+ try { Add-Content -LiteralPath $LogFile -Value $line -Encoding utf8 } catch { }
41
+ }
42
+
43
+ function EmitResult([bool]$ok, [string]$errorText, [string]$backupPath) {
44
+ $obj = [ordered]@{
45
+ ok = $ok
46
+ version = $Version
47
+ backup = $backupPath
48
+ log = $LogFile
49
+ error = $errorText
50
+ }
51
+ # 宿主只认这一行,别把它混进进度输出里
52
+ Write-Output ("RESULT:" + ($obj | ConvertTo-Json -Compress -Depth 3))
53
+ exit 0
54
+ }
55
+
56
+ # 失败时用备份把插件目录恢复成动手前的样子
57
+ function Restore-Backup([string]$backup, [string]$target) {
58
+ if (-not $backup -or -not (Test-Path -LiteralPath $backup)) { return $false }
59
+ try {
60
+ foreach ($it in @(Get-ChildItem -LiteralPath $target -Force)) {
61
+ Remove-Item -LiteralPath $it.FullName -Recurse -Force -ErrorAction SilentlyContinue
62
+ }
63
+ foreach ($it in @(Get-ChildItem -LiteralPath $backup -Force)) {
64
+ Copy-Item -LiteralPath $it.FullName -Destination $target -Recurse -Force
65
+ }
66
+ return $true
67
+ } catch {
68
+ Log ("回滚失败:{0}" -f $_.Exception.Message)
69
+ return $false
70
+ }
71
+ }
72
+
73
+ Log ("==== 安装插件 {0} 到 {1} ====" -f $Version, $PluginDir)
74
+
75
+ if (-not (Test-Path -LiteralPath $PluginDir)) { EmitResult $false ("找不到插件目录:" + $PluginDir) '' }
76
+
77
+ $currentVersion = ''
78
+ try { $currentVersion = (Get-Content -LiteralPath (Join-Path $PluginDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version } catch { }
79
+ Log ("当前版本:{0}" -f $currentVersion)
80
+
81
+ $tmp = Join-Path $env:TEMP ("dsh-plugin-live-{0}-{1}.tgz" -f $Version, $stamp)
82
+ $pkgPath = ''
83
+ if ($TgzFile -and (Test-Path -LiteralPath $TgzFile)) {
84
+ $pkgPath = $TgzFile
85
+ Log ("使用本地包:{0}" -f $pkgPath)
86
+ } else {
87
+ $urls = @($TgzUrls -split '\|' | Where-Object { $_ -and $_.Trim() })
88
+ if ($urls.Count -eq 0) { EmitResult $false '没有给下载地址,也没有本地包' '' }
89
+ $errors = @()
90
+ foreach ($u in $urls) {
91
+ Log ("尝试下载:{0}" -f $u)
92
+ try {
93
+ Invoke-WebRequest -Uri $u.Trim() -OutFile $tmp -UseBasicParsing -TimeoutSec 90
94
+ if ((Test-Path -LiteralPath $tmp) -and ((Get-Item -LiteralPath $tmp).Length -gt 0)) {
95
+ $pkgPath = $tmp
96
+ Log ("下载成功:{0:N0} 字节" -f (Get-Item -LiteralPath $tmp).Length)
97
+ break
98
+ }
99
+ $errors += ("{0}:文件没有落盘" -f $u)
100
+ } catch {
101
+ $errors += ("{0}:{1}" -f $u, $_.Exception.Message)
102
+ Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
103
+ }
104
+ }
105
+ if (-not $pkgPath) {
106
+ $msg = '下载安装包失败(每个地址都试过了):' + ($errors -join ';')
107
+ Log $msg
108
+ EmitResult $false $msg ''
109
+ }
110
+ }
111
+
112
+ if ($Sha256) {
113
+ $hash = (Get-FileHash -LiteralPath $pkgPath -Algorithm SHA256).Hash.ToLower()
114
+ Log ("sha256:{0}" -f $hash)
115
+ if ($hash -ne $Sha256.ToLower()) {
116
+ Log 'sha256 与清单不一致,放弃安装'
117
+ Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
118
+ EmitResult $false '安装包 sha256 校验不通过(没下载全或被改动)' ''
119
+ }
120
+ Log 'sha256 校验通过'
121
+ }
122
+
123
+ $tar = Join-Path $env:SystemRoot 'System32\tar.exe'
124
+ if (-not (Test-Path -LiteralPath $tar)) { $tar = 'tar.exe' }
125
+ $stage = Join-Path $env:TEMP ("dsh-plugin-live-stage-" + $stamp)
126
+ $payload = ''
127
+ try {
128
+ if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force }
129
+ New-Item -ItemType Directory -Path $stage -Force | Out-Null
130
+ & $tar -xzf $pkgPath -C $stage *> $null
131
+ if ($LASTEXITCODE -ne 0) { throw ("解包失败,退出码 {0}" -f $LASTEXITCODE) }
132
+ $payload = Join-Path $stage 'package'
133
+ if (-not (Test-Path -LiteralPath $payload)) { throw '安装包结构不对(里面没有 package 目录)' }
134
+ $newVersion = (Get-Content -LiteralPath (Join-Path $payload 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version
135
+ Log ("包内版本:{0}" -f $newVersion)
136
+ if ($Version -and $newVersion -ne $Version) { throw ("安装包里的版本是 {0},与清单 {1} 不一致" -f $newVersion, $Version) }
137
+ } catch {
138
+ Log ("解包/校验失败:{0}" -f $_.Exception.Message)
139
+ if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
140
+ Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
141
+ EmitResult $false ('安装包有问题:' + $_.Exception.Message) ''
142
+ }
143
+
144
+ # 整目录备份:写坏了能手动拷回去,也能自动回滚
145
+ $backup = Join-Path (Join-Path $DshHome 'backups') ("plugin-{0}-{1}" -f $(if ($currentVersion) { $currentVersion } else { 'unknown' }), $stamp)
146
+ $backupOk = $false
147
+ try {
148
+ New-Item -ItemType Directory -Path (Split-Path -Parent $backup) -Force | Out-Null
149
+ Copy-Item -LiteralPath $PluginDir -Destination $backup -Recurse -Force
150
+ $backupOk = $true
151
+ Log ("旧版已备份:{0}" -f $backup)
152
+ } catch {
153
+ Log ("备份失败(继续安装,出问题就没有自动回滚了):{0}" -f $_.Exception.Message)
154
+ }
155
+
156
+ Log '覆盖安装…'
157
+ $installError = ''
158
+ try {
159
+ foreach ($it in @(Get-ChildItem -LiteralPath $payload -Force)) {
160
+ Copy-Item -LiteralPath $it.FullName -Destination $PluginDir -Recurse -Force
161
+ }
162
+ $installed = (Get-Content -LiteralPath (Join-Path $PluginDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version
163
+ Log ("安装完成,插件目录现在指向版本 {0}" -f $installed)
164
+ } catch {
165
+ $installError = $_.Exception.Message
166
+ }
167
+
168
+ if ($installError) {
169
+ $restored = Restore-Backup $(if ($backupOk) { $backup } else { '' }) $PluginDir
170
+ $note = if ($restored) { '已用备份回滚到安装前的状态' } else { '没能自动回滚,手动把 ' + $backup + ' 拷回去' }
171
+ Log ("写入失败:{0}({1})" -f $installError, $note)
172
+ if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
173
+ Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
174
+ EmitResult $false ("写入失败:" + $installError + '(' + $note + ')') $(if ($backupOk) { $backup } else { '' })
175
+ }
176
+
177
+ if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
178
+ Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
179
+ EmitResult $true '' $(if ($backupOk) { $backup } else { '' })