dsh-aupdate 0.6.6 → 0.6.8
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/README.md +34 -1
- package/client.js +164 -13
- package/index.js +206 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,6 +63,19 @@ https://github.com/CalmSun/dsh-aupdate
|
|
|
63
63
|
| `maxPerCycle` | `0` | 一轮最多装几个,0 表示不限 |
|
|
64
64
|
| `sessionIdleMinutes` | `2` | 会话静默多少分钟才算闲,0 表示只看子任务 |
|
|
65
65
|
| `notify` | `true` | 发现可更新版本时在右下角提醒,列出插件与版本 |
|
|
66
|
+
| `historyRetentionDays` | `3` | 更新记录保留天数,0 = 永久 |
|
|
67
|
+
| `logRetentionDays` | `3` | 运行日志保留天数,0 = 永久 |
|
|
68
|
+
|
|
69
|
+
## 定期清理
|
|
70
|
+
|
|
71
|
+
更新记录和运行日志默认只留 **3 天**,每次定时检查前清一次,DSH 启动时也清一次;
|
|
72
|
+
设置页另有「立即清理」。两个期限可分别选 1 / 3 / 7 / 30 / 90 天或「永久保留」。
|
|
73
|
+
|
|
74
|
+
**回滚记录和它撤销的那条更新必须同进同退**:回滚判定是从回滚记录反推哪些更新已消费的,
|
|
75
|
+
如果只按时间删、恰好删掉回滚记录而留下对应更新,那次已经回滚过的降级就会重新变得可回滚。
|
|
76
|
+
所以清理按「更新 + 撤销它的回滚」分组,以组内最新的时间决定去留,整组一起删或一起留。
|
|
77
|
+
|
|
78
|
+
日志按行首的 ISO 时间清理;解析不出时间的行一律保留,不因为格式不认识就删掉现场。
|
|
66
79
|
|
|
67
80
|
## 什么时候动手
|
|
68
81
|
|
|
@@ -177,6 +190,10 @@ mtime 为准:那是磁盘事实。会话目录整个读不到时返回「不
|
|
|
177
190
|
发现可更新版本时,界面右下角浮一条提醒,列出插件名和「当前 → 新」版本;超过 8 条
|
|
178
191
|
时余下给一句「还有 N 个」,15 秒后自动消失。
|
|
179
192
|
|
|
193
|
+
提醒逐条列出**包名**和「当前版本 → 新版本」,包名过长时省略包名那一侧、不动版本;
|
|
194
|
+
超过 6 条在框内滚动。设置页的「上一次」卡片也会列出同一份清单,不用只靠一句
|
|
195
|
+
「4 个可更新」猜是哪几个。
|
|
196
|
+
|
|
180
197
|
同一批更新只提醒一次:这批更新的签名记在 `localStorage` 里,刷新页面不会重复弹。
|
|
181
198
|
关掉「发现更新时提醒我」就完全不弹,日志和设置页照常记录——提醒只是别让它装完你
|
|
182
199
|
还不知道,不是唯一的告知渠道。
|
|
@@ -291,6 +308,20 @@ A successful install clears the record.
|
|
|
291
308
|
| `maxPerCycle` | `0` | Installs per cycle; 0 means no limit |
|
|
292
309
|
| `sessionIdleMinutes` | `2` | Session must be quiet this long to count as idle; 0 judges by subagents only |
|
|
293
310
|
| `notify` | `true` | Announce available updates in the bottom-right corner, with plugin names and versions |
|
|
311
|
+
| `historyRetentionDays` | `3` | History retention in days; 0 = forever |
|
|
312
|
+
| `logRetentionDays` | `3` | Log retention in days; 0 = forever |
|
|
313
|
+
|
|
314
|
+
## Retention
|
|
315
|
+
|
|
316
|
+
Update history and the run log keep **3 days** by default. Cleanup runs before every
|
|
317
|
+
scheduled check and once at startup; the settings page also has an immediate Clean now
|
|
318
|
+
button. Each retention can be set to 1 / 3 / 7 / 30 / 90 days or Keep forever.
|
|
319
|
+
|
|
320
|
+
A rollback and the update it reverses are always removed together. Rollback eligibility is
|
|
321
|
+
derived from rollback records — deleting only the rollback would make an already-reverted
|
|
322
|
+
downgrade roll back again — so pruning groups them and decides by the newest timestamp in
|
|
323
|
+
the group. Log lines are filtered by their leading ISO timestamp; lines without a parseable
|
|
324
|
+
one are kept rather than lost.
|
|
294
325
|
|
|
295
326
|
### When it acts
|
|
296
327
|
|
|
@@ -383,7 +414,9 @@ in the same language. Wording only — it changes no update behaviour.
|
|
|
383
414
|
|
|
384
415
|
When a check finds updates, a notice appears in the bottom-right corner listing each
|
|
385
416
|
plugin and its current → new version; past eight it adds "and N more", and it closes
|
|
386
|
-
itself after 15 seconds.
|
|
417
|
+
itself after 15 seconds. Long package names are truncated on the name side so the
|
|
418
|
+
versions stay visible, and past six entries the list scrolls. The Last run card on the
|
|
419
|
+
settings page lists the same plugins and version pairs. Each batch is announced once — the signature is kept in
|
|
387
420
|
`localStorage` — and turning "Tell me when updates are found" off silences it entirely
|
|
388
421
|
while the log and settings page keep recording.
|
|
389
422
|
|
package/client.js
CHANGED
|
@@ -103,6 +103,17 @@ window.__ModuleLoader__.load({
|
|
|
103
103
|
toastTitle: '{n} 个插件可以更新',
|
|
104
104
|
toastMore: '还有 {n} 个',
|
|
105
105
|
toastDismiss: '知道了',
|
|
106
|
+
keepTitle: '保留期限',
|
|
107
|
+
keepHint: '到期自动清掉旧的更新记录与运行日志;0 表示一直保留。回滚记录会和对应更新一起保留',
|
|
108
|
+
keepHistory: '更新记录保留',
|
|
109
|
+
keepLog: '运行日志保留',
|
|
110
|
+
keepDay: '天',
|
|
111
|
+
keepForever: '永久保留',
|
|
112
|
+
cleanNow: '立即清理',
|
|
113
|
+
cleaning: '清理中…',
|
|
114
|
+
cleanDone: '已清理 {n} 条',
|
|
115
|
+
cleanNothing: '没有需要清理的',
|
|
116
|
+
cleanFail: '清理失败',
|
|
106
117
|
history: '更新记录',
|
|
107
118
|
historyEmpty: '还没有安装过任何更新',
|
|
108
119
|
rollback: '回滚',
|
|
@@ -208,6 +219,17 @@ window.__ModuleLoader__.load({
|
|
|
208
219
|
toastTitle: '{n} plugin(s) can update',
|
|
209
220
|
toastMore: 'and {n} more',
|
|
210
221
|
toastDismiss: 'Got it',
|
|
222
|
+
keepTitle: 'Retention',
|
|
223
|
+
keepHint: 'Older update records and log lines are removed automatically; 0 means keep forever. A rollback is kept together with the update it reverses',
|
|
224
|
+
keepHistory: 'History retention',
|
|
225
|
+
keepLog: 'Log retention',
|
|
226
|
+
keepDay: 'days',
|
|
227
|
+
keepForever: 'Keep forever',
|
|
228
|
+
cleanNow: 'Clean now',
|
|
229
|
+
cleaning: 'Cleaning…',
|
|
230
|
+
cleanDone: 'Removed {n} entries',
|
|
231
|
+
cleanNothing: 'Nothing to clean',
|
|
232
|
+
cleanFail: 'Cleanup failed',
|
|
211
233
|
history: 'Update history',
|
|
212
234
|
historyEmpty: 'No update has been installed yet',
|
|
213
235
|
rollback: 'Roll back',
|
|
@@ -372,6 +394,17 @@ window.__ModuleLoader__.load({
|
|
|
372
394
|
return response.json()
|
|
373
395
|
}
|
|
374
396
|
|
|
397
|
+
/** 手动清理:服务端按当前保留期限删,回报删了多少。 */
|
|
398
|
+
async function cleanup() {
|
|
399
|
+
const response = await fetch(endpoint('cleanup') + langQuery(), {
|
|
400
|
+
method: 'POST',
|
|
401
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
402
|
+
body: '{}',
|
|
403
|
+
})
|
|
404
|
+
if (!response.ok) throw new Error('cleanup ' + response.status)
|
|
405
|
+
return response.json()
|
|
406
|
+
}
|
|
407
|
+
|
|
375
408
|
async function rollback(name) {
|
|
376
409
|
const response = await fetch(endpoint('rollback') + langQuery(), {
|
|
377
410
|
method: 'POST',
|
|
@@ -617,6 +650,8 @@ window.__ModuleLoader__.load({
|
|
|
617
650
|
const [rolling, setRolling] = useState('')
|
|
618
651
|
const [rollHint, setRollHint] = useState('')
|
|
619
652
|
const [confirming, setConfirming] = useState('')
|
|
653
|
+
const [cleaning, setCleaning] = useState(false)
|
|
654
|
+
const [cleanHint, setCleanHint] = useState('')
|
|
620
655
|
|
|
621
656
|
const load = useCallback(async () => {
|
|
622
657
|
setPhase('loading')
|
|
@@ -715,6 +750,25 @@ window.__ModuleLoader__.load({
|
|
|
715
750
|
}
|
|
716
751
|
}, [running])
|
|
717
752
|
|
|
753
|
+
const doCleanup = useCallback(async () => {
|
|
754
|
+
if (cleaning) return
|
|
755
|
+
setCleaning(true)
|
|
756
|
+
setCleanHint('')
|
|
757
|
+
try {
|
|
758
|
+
const result = await cleanup()
|
|
759
|
+
const total = Number(result?.history ?? 0) + Number(result?.log ?? 0)
|
|
760
|
+
setCleanHint(total > 0 ? T.cleanDone.replace('{n}', String(total)) : T.cleanNothing)
|
|
761
|
+
try {
|
|
762
|
+
const next = await getState(LANG)
|
|
763
|
+
setState(next)
|
|
764
|
+
} catch {}
|
|
765
|
+
} catch {
|
|
766
|
+
setCleanHint(T.cleanFail)
|
|
767
|
+
} finally {
|
|
768
|
+
setCleaning(false)
|
|
769
|
+
}
|
|
770
|
+
}, [cleaning])
|
|
771
|
+
|
|
718
772
|
const doRollback = useCallback(async (target) => {
|
|
719
773
|
if (rolling !== '' || running) return
|
|
720
774
|
// 回滚会换掉正在跑的代码,属于不可轻点的操作,先要一次确认。
|
|
@@ -832,6 +886,40 @@ window.__ModuleLoader__.load({
|
|
|
832
886
|
h(SwitchRow, { label: T.allowMajor, hint: T.allowMajorHint, value: draft.allowMajor, onChange: (v) => patch('allowMajor', v) }),
|
|
833
887
|
h(SwitchRow, { label: T.allowPrerelease, hint: T.allowPrereleaseHint, value: draft.allowPrerelease, onChange: (v) => patch('allowPrerelease', v) }),
|
|
834
888
|
),
|
|
889
|
+
h('div', { style: S.card },
|
|
890
|
+
h('span', { style: S.label }, T.keepTitle),
|
|
891
|
+
h('span', { style: S.hint }, T.keepHint),
|
|
892
|
+
h(SelectRow, {
|
|
893
|
+
label: T.keepHistory,
|
|
894
|
+
value: String(draft.historyRetentionDays ?? 3),
|
|
895
|
+
onChange: (v) => patch('historyRetentionDays', Number(v)),
|
|
896
|
+
options: [
|
|
897
|
+
{ value: '1', label: '1 ' + T.keepDay },
|
|
898
|
+
{ value: '3', label: '3 ' + T.keepDay },
|
|
899
|
+
{ value: '7', label: '7 ' + T.keepDay },
|
|
900
|
+
{ value: '30', label: '30 ' + T.keepDay },
|
|
901
|
+
{ value: '90', label: '90 ' + T.keepDay },
|
|
902
|
+
{ value: '0', label: T.keepForever },
|
|
903
|
+
],
|
|
904
|
+
}),
|
|
905
|
+
h(SelectRow, {
|
|
906
|
+
label: T.keepLog,
|
|
907
|
+
value: String(draft.logRetentionDays ?? 3),
|
|
908
|
+
onChange: (v) => patch('logRetentionDays', Number(v)),
|
|
909
|
+
options: [
|
|
910
|
+
{ value: '1', label: '1 ' + T.keepDay },
|
|
911
|
+
{ value: '3', label: '3 ' + T.keepDay },
|
|
912
|
+
{ value: '7', label: '7 ' + T.keepDay },
|
|
913
|
+
{ value: '30', label: '30 ' + T.keepDay },
|
|
914
|
+
{ value: '90', label: '90 ' + T.keepDay },
|
|
915
|
+
{ value: '0', label: T.keepForever },
|
|
916
|
+
],
|
|
917
|
+
}),
|
|
918
|
+
h('div', { style: S.actions },
|
|
919
|
+
h('button', { type: 'button', style: S.btn, disabled: cleaning, onClick: () => void doCleanup() }, cleaning ? T.cleaning : T.cleanNow),
|
|
920
|
+
cleanHint ? h('span', { style: S.status }, cleanHint) : null,
|
|
921
|
+
),
|
|
922
|
+
),
|
|
835
923
|
h('div', { style: S.card },
|
|
836
924
|
// 按钮和它动作的对象放在一起:读「上一次」的人最想做的事就是再跑一次。
|
|
837
925
|
h('div', { style: S.row },
|
|
@@ -848,6 +936,23 @@ window.__ModuleLoader__.load({
|
|
|
848
936
|
: T.upToDate),
|
|
849
937
|
h('span', null, last.reason || last.status || ''),
|
|
850
938
|
),
|
|
939
|
+
// 只报「4 个可更新」不够:要看得出是哪几个、从哪版到哪版。
|
|
940
|
+
// 列表封高度、内部滚动,30 个包也不会把这张卡拉成一屏多。
|
|
941
|
+
(() => {
|
|
942
|
+
const rows = (Array.isArray(last?.updates) ? last.updates : [])
|
|
943
|
+
.map((item) => describeUpdate(item))
|
|
944
|
+
.filter((row) => row !== null)
|
|
945
|
+
if (rows.length === 0) return null
|
|
946
|
+
return h('div', { style: S.scrollList },
|
|
947
|
+
rows.map((row, index) => h('div', {
|
|
948
|
+
key: row.name + ':' + index,
|
|
949
|
+
style: { display: 'flex', alignItems: 'baseline', gap: '8px' },
|
|
950
|
+
},
|
|
951
|
+
h('span', { style: S.pkgName, title: row.name }, row.name),
|
|
952
|
+
row.versions === '' ? null : h('span', { style: S.pkgVer }, row.versions),
|
|
953
|
+
)),
|
|
954
|
+
)
|
|
955
|
+
})(),
|
|
851
956
|
runHint ? h('span', { style: S.status }, runHint) : null,
|
|
852
957
|
),
|
|
853
958
|
// 更新记录:谁、从哪到哪、什么时候、成没成,以及回滚入口。
|
|
@@ -1012,40 +1117,85 @@ window.__ModuleLoader__.load({
|
|
|
1012
1117
|
|
|
1013
1118
|
let toastOpen = false
|
|
1014
1119
|
|
|
1120
|
+
/**
|
|
1121
|
+
* 把一条「可更新」记录归一成 { name, versions }。
|
|
1122
|
+
*
|
|
1123
|
+
* 后端 0.6.8 起存 { name, from, to };更早的版本存的是成品句子
|
|
1124
|
+
* 「名字 1.0 -> 1.1」。两种都要能显示——旧数据不该因为升级就变成
|
|
1125
|
+
* undefined。之前正是只按对象读,句子条目全是 undefined ? → ?。
|
|
1126
|
+
*/
|
|
1127
|
+
function describeUpdate(item) {
|
|
1128
|
+
if (typeof item === 'string') {
|
|
1129
|
+
const text = item.trim()
|
|
1130
|
+
if (text === '') return null
|
|
1131
|
+
const match = /^(.*) (\S+) -> (\S+)$/u.exec(text)
|
|
1132
|
+
if (match === null) return { name: text, versions: '' }
|
|
1133
|
+
return { name: match[1], versions: match[2] + ' → ' + match[3] }
|
|
1134
|
+
}
|
|
1135
|
+
if (item === null || typeof item !== 'object') return null
|
|
1136
|
+
const name = typeof item.name === 'string' ? item.name : ''
|
|
1137
|
+
if (name === '') return null
|
|
1138
|
+
const from = typeof item.from === 'string' && item.from !== '' ? item.from : '?'
|
|
1139
|
+
const to = typeof item.to === 'string' && item.to !== '' ? item.to : '?'
|
|
1140
|
+
return { name, versions: from + ' → ' + to }
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
function updateSignature(updates) {
|
|
1144
|
+
return (Array.isArray(updates) ? updates : [])
|
|
1145
|
+
.map((item) => describeUpdate(item))
|
|
1146
|
+
.filter((row) => row !== null)
|
|
1147
|
+
.map((row) => row.name + '@' + row.versions)
|
|
1148
|
+
.sort()
|
|
1149
|
+
.join('|')
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1015
1152
|
function showUpdateToast(updates) {
|
|
1016
1153
|
if (toastOpen) return
|
|
1017
1154
|
const doc = typeof document === 'undefined' ? null : document
|
|
1018
1155
|
if (doc === null || doc.body === null) return
|
|
1156
|
+
const rows = (Array.isArray(updates) ? updates : []).map((item) => describeUpdate(item)).filter((row) => row !== null)
|
|
1157
|
+
if (rows.length === 0) return
|
|
1019
1158
|
toastOpen = true
|
|
1020
1159
|
const box = doc.createElement('div')
|
|
1021
1160
|
box.setAttribute('data-dsh-aupdate', 'toast')
|
|
1022
1161
|
box.style.cssText = [
|
|
1023
1162
|
'position:fixed', 'right:16px', 'bottom:16px', 'z-index:2147483000',
|
|
1024
|
-
'max-width:
|
|
1163
|
+
'width:320px', 'max-width:calc(100vw - 32px)', 'padding:12px 14px', 'border-radius:10px',
|
|
1025
1164
|
'background:rgba(28,28,30,0.97)', 'color:#f4f4f5',
|
|
1026
1165
|
'box-shadow:0 8px 28px rgba(0,0,0,0.38)', 'border:1px solid rgba(255,255,255,0.14)',
|
|
1027
1166
|
'font:13px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif',
|
|
1028
|
-
'display:flex', 'flex-direction:column', 'gap:
|
|
1167
|
+
'display:flex', 'flex-direction:column', 'gap:9px',
|
|
1029
1168
|
].join(';')
|
|
1030
1169
|
|
|
1031
1170
|
const title = doc.createElement('div')
|
|
1032
|
-
title.textContent = T.toastTitle.replace('{n}', String(
|
|
1033
|
-
title.style.cssText = 'font-weight:600'
|
|
1171
|
+
title.textContent = T.toastTitle.replace('{n}', String(rows.length))
|
|
1172
|
+
title.style.cssText = 'font-weight:600;display:flex;align-items:center;gap:6px'
|
|
1034
1173
|
box.appendChild(title)
|
|
1035
1174
|
|
|
1036
1175
|
const list = doc.createElement('div')
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1176
|
+
// 超过 6 条就在框内滚动:提醒不该占掉半屏。
|
|
1177
|
+
list.style.cssText = 'display:flex;flex-direction:column;gap:2px;max-height:190px;overflow:auto;margin:0 -4px'
|
|
1178
|
+
for (const row of rows.slice(0, 12)) {
|
|
1179
|
+
const line = doc.createElement('div')
|
|
1180
|
+
line.style.cssText = 'display:flex;align-items:baseline;gap:8px;padding:2px 4px;border-radius:4px;background:rgba(255,255,255,0.05)'
|
|
1181
|
+
const nameEl = doc.createElement('span')
|
|
1182
|
+
nameEl.textContent = row.name
|
|
1183
|
+
// 包名可能很长(@scope/name),压缩它而不是压缩版本。
|
|
1184
|
+
nameEl.style.cssText = 'flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap'
|
|
1185
|
+
line.appendChild(nameEl)
|
|
1186
|
+
if (row.versions !== '') {
|
|
1187
|
+
const verEl = doc.createElement('span')
|
|
1188
|
+
verEl.textContent = row.versions
|
|
1189
|
+
verEl.style.cssText = 'flex:0 0 auto;font-size:11.5px;opacity:0.72;font-variant-numeric:tabular-nums'
|
|
1190
|
+
line.appendChild(verEl)
|
|
1191
|
+
}
|
|
1192
|
+
list.appendChild(line)
|
|
1043
1193
|
}
|
|
1044
1194
|
box.appendChild(list)
|
|
1045
1195
|
|
|
1046
|
-
if (
|
|
1196
|
+
if (rows.length > 12) {
|
|
1047
1197
|
const more = doc.createElement('div')
|
|
1048
|
-
more.textContent = T.toastMore.replace('{n}', String(
|
|
1198
|
+
more.textContent = T.toastMore.replace('{n}', String(rows.length - 12))
|
|
1049
1199
|
more.style.cssText = 'font-size:11.5px;opacity:0.6'
|
|
1050
1200
|
box.appendChild(more)
|
|
1051
1201
|
}
|
|
@@ -1075,7 +1225,8 @@ window.__ModuleLoader__.load({
|
|
|
1075
1225
|
if (state?.settings?.notify !== false) {
|
|
1076
1226
|
const updates = Array.isArray(state?.lastRun?.updates) ? state.lastRun.updates : []
|
|
1077
1227
|
if (updates.length > 0) {
|
|
1078
|
-
|
|
1228
|
+
// 签名算的是「包名+版本变化」,旧格式也能算出来,不会重复弹。
|
|
1229
|
+
const sig = updateSignature(updates)
|
|
1079
1230
|
if (sig !== notifiedMark()) {
|
|
1080
1231
|
markNotified(sig)
|
|
1081
1232
|
showUpdateToast(updates)
|
package/index.js
CHANGED
|
@@ -125,6 +125,7 @@ const LOG_FIELD_LABELS = {
|
|
|
125
125
|
allowMajor: '跨大版本', allowPrerelease: '预发布', scheduleMode: '调度方式',
|
|
126
126
|
dailyAt: '每天时刻', everyHours: '每几小时', everyDays: '每几天',
|
|
127
127
|
suppressHours: '失败抑制', maxPerCycle: '单轮上限', sessionIdleMinutes: '会话静默',
|
|
128
|
+
historyRetentionDays: '更新记录保留', logRetentionDays: '运行日志保留',
|
|
128
129
|
},
|
|
129
130
|
en: {
|
|
130
131
|
lang: 'display language', enabled: 'scheduled check', autoApply: 'auto-install',
|
|
@@ -134,6 +135,7 @@ const LOG_FIELD_LABELS = {
|
|
|
134
135
|
scheduleMode: 'schedule', dailyAt: 'time of day', everyHours: 'every N hours',
|
|
135
136
|
everyDays: 'every N days', suppressHours: 'failure suppression', maxPerCycle: 'per-cycle cap',
|
|
136
137
|
sessionIdleMinutes: 'session idle',
|
|
138
|
+
historyRetentionDays: 'history retention', logRetentionDays: 'log retention',
|
|
137
139
|
},
|
|
138
140
|
}
|
|
139
141
|
|
|
@@ -187,6 +189,11 @@ function makeLogTemplates(lang) {
|
|
|
187
189
|
toolScopeEnded: (msg) => (zh ? `工具作用域已结束:${msg}` : `Tool scope ended: ${msg}`),
|
|
188
190
|
disabledBySettings: () => (zh ? '设置里已关闭自动更新,定时器待命' : 'Auto-update is off in settings; scheduler parked'),
|
|
189
191
|
disabledByConfig: () => (zh ? '配置里已关闭自动更新,定时器待命,设置页仍然可用' : 'Auto-update is off in config; scheduler parked, settings page stays live'),
|
|
192
|
+
// 清理日志按对象报数,几个数字一处说完,免得刷三行。
|
|
193
|
+
cleaned: (kind, rows, days) => (zh
|
|
194
|
+
? `清理${kind}:删掉 ${rows} 条超过 ${days} 天的`
|
|
195
|
+
: `Cleaned ${kind}: removed ${rows} entr${rows === 1 ? 'y' : 'ies'} older than ${days} day(s)`),
|
|
196
|
+
cleanSkipped: (kind) => (zh ? `${kind}没有需要清理的` : `Nothing to clean in ${kind}`),
|
|
190
197
|
ready: (minutes, autoApply, hotReload) => (zh
|
|
191
198
|
? `已就绪:每 ${minutes} 分钟检查一次,自动安装=${onOff(autoApply)},就地生效=${onOff(hotReload)}`
|
|
192
199
|
: `Ready: every ${minutes} min, autoApply=${onOff(autoApply)}, hotReload=${onOff(hotReload)}`),
|
|
@@ -475,11 +482,27 @@ const OVERRIDEABLE = [
|
|
|
475
482
|
'lang',
|
|
476
483
|
// 效率闸门:一轮最多装几个、会话多静才算闲。
|
|
477
484
|
'maxPerCycle', 'sessionIdleMinutes',
|
|
485
|
+
// 更新记录 / 运行日志 / 快照的保留期限(天)。0 = 永不清理。
|
|
486
|
+
'historyRetentionDays', 'logRetentionDays',
|
|
478
487
|
]
|
|
479
488
|
|
|
480
489
|
/** 四种调度模式。minutes 是原来的「间隔 N 分钟」,其余照抄竞品。 */
|
|
481
490
|
const SCHEDULE_MODES = ['minutes', 'hours', 'days', 'daily']
|
|
482
491
|
|
|
492
|
+
/**
|
|
493
|
+
* 保留期限。默认 3 天——记录主要是「刚才装坏了什么」的现场,放太久只会占空间、
|
|
494
|
+
* 还会让更新记录栏翻很久。0 表示永不自动清理。
|
|
495
|
+
*
|
|
496
|
+
* 这是天数不是毫秒:设置页传 3 就是 3 天,写入时钳到 [0, 3650]。
|
|
497
|
+
*/
|
|
498
|
+
const RETENTION_CHOICES = [1, 3, 7, 30, 90, 0]
|
|
499
|
+
|
|
500
|
+
function retentionOf(config, key, fallback) {
|
|
501
|
+
const value = Number(config?.[key])
|
|
502
|
+
if (!Number.isFinite(value)) return fallback
|
|
503
|
+
return Math.min(Math.max(Math.round(value), 0), 3650)
|
|
504
|
+
}
|
|
505
|
+
|
|
483
506
|
/**
|
|
484
507
|
* 界面语言。auto = 跟随系统(中文环境给中文,其余给英文)。
|
|
485
508
|
*
|
|
@@ -502,6 +525,8 @@ const NUMBER_BOUNDS = {
|
|
|
502
525
|
suppressHours: [0, 720],
|
|
503
526
|
maxPerCycle: [0, 50],
|
|
504
527
|
sessionIdleMinutes: [0, 120],
|
|
528
|
+
historyRetentionDays: [0, 3650],
|
|
529
|
+
logRetentionDays: [0, 3650],
|
|
505
530
|
}
|
|
506
531
|
|
|
507
532
|
let overrideCache = { stamp: null, value: {} }
|
|
@@ -664,6 +689,9 @@ function settingsOf(config) {
|
|
|
664
689
|
maxPerCycle: numberSetting(config, 'maxPerCycle', 0, 0, 50),
|
|
665
690
|
// 会话目录多少分钟内没有写入才算「闲」。0 = 不看会话写入,只看子任务。
|
|
666
691
|
sessionIdleMinutes: numberSetting(config, 'sessionIdleMinutes', SESSION_IDLE_MINUTES, 0, 120),
|
|
692
|
+
// 更新记录 / 运行日志的保留天数。0 = 永不清理,默认 3 天。
|
|
693
|
+
historyRetentionDays: retentionOf(config, 'historyRetentionDays', 3),
|
|
694
|
+
logRetentionDays: retentionOf(config, 'logRetentionDays', 3),
|
|
667
695
|
// 说明:这两个键不进 Config schema(见 OVERRIDEABLE 上的注释),
|
|
668
696
|
// 所以只能通过设置页/覆盖层改,改坏了不会影响插件加载。
|
|
669
697
|
}
|
|
@@ -756,11 +784,38 @@ function readLastRun() {
|
|
|
756
784
|
if (raw === null || typeof raw !== 'object') return null
|
|
757
785
|
if (typeof raw.at !== 'string' || raw.at === '') return null
|
|
758
786
|
const textList = (value) => (Array.isArray(value) ? value.filter((item) => typeof item === 'string') : [])
|
|
787
|
+
/**
|
|
788
|
+
* 待更新清单。
|
|
789
|
+
*
|
|
790
|
+
* 0.6.7 及更早把这一项存成「名字 1.0 -> 1.1」的句子,界面只能整行照搬;
|
|
791
|
+
* 现在存 { name, from, to },提醒和设置页才能把包名和版本分开排版。
|
|
792
|
+
* 旧数据在这里解析回来,不需要用户清文件。
|
|
793
|
+
*/
|
|
794
|
+
const updateList = (value) => {
|
|
795
|
+
if (!Array.isArray(value)) return []
|
|
796
|
+
const out = []
|
|
797
|
+
for (const item of value) {
|
|
798
|
+
if (item !== null && typeof item === 'object') {
|
|
799
|
+
if (typeof item.name !== 'string' || item.name === '') continue
|
|
800
|
+
out.push({
|
|
801
|
+
name: item.name,
|
|
802
|
+
from: typeof item.from === 'string' ? item.from : '',
|
|
803
|
+
to: typeof item.to === 'string' ? item.to : '',
|
|
804
|
+
})
|
|
805
|
+
continue
|
|
806
|
+
}
|
|
807
|
+
if (typeof item !== 'string' || item === '') continue
|
|
808
|
+
const match = /^(.*) (\S+) -> (\S+)$/u.exec(item)
|
|
809
|
+
if (match === null) out.push({ name: item, from: '', to: '' })
|
|
810
|
+
else out.push({ name: match[1], from: match[2], to: match[3] })
|
|
811
|
+
}
|
|
812
|
+
return out
|
|
813
|
+
}
|
|
759
814
|
return {
|
|
760
815
|
status: typeof raw.status === 'string' ? raw.status : 'unknown',
|
|
761
816
|
at: raw.at,
|
|
762
817
|
checked: Number.isFinite(raw.checked) ? raw.checked : 0,
|
|
763
|
-
updates:
|
|
818
|
+
updates: updateList(raw.updates),
|
|
764
819
|
installed: textList(raw.installed),
|
|
765
820
|
reload: typeof raw.reload === 'string' ? raw.reload : 'none',
|
|
766
821
|
// 0.6.2 及更早存的是句子;0.6.3 起是 { code, args },两种都要能读。
|
|
@@ -1376,6 +1431,111 @@ function pruneSnapshots() {
|
|
|
1376
1431
|
} catch {}
|
|
1377
1432
|
}
|
|
1378
1433
|
|
|
1434
|
+
/**
|
|
1435
|
+
* 按保留天数清理更新记录。
|
|
1436
|
+
*
|
|
1437
|
+
* 不能简单地「按 at 删老行」:pickRollbackTarget 是从回滚记录反推哪些更新已消费的。
|
|
1438
|
+
* 若把回滚记录删了、留下对应更新行,那次已经回滚过的降级就会重新变得可回滚——
|
|
1439
|
+
* 用户点一下,等于把一个已经被撤销的更新又执行一次。
|
|
1440
|
+
*
|
|
1441
|
+
* 所以按「一组」判新旧:一条更新 + 所有撤销它的回滚记录算一组,组内最新的时间
|
|
1442
|
+
* 决定这一组去留。同时删,就不会留下半组。
|
|
1443
|
+
*/
|
|
1444
|
+
function pruneHistory(days, now) {
|
|
1445
|
+
if (!(days > 0)) return 0
|
|
1446
|
+
const rows = readHistory()
|
|
1447
|
+
if (rows.length === 0) return 0
|
|
1448
|
+
const cutoff = now.getTime() - days * 86400_000
|
|
1449
|
+
const stampOf = (row) => {
|
|
1450
|
+
const at = Date.parse(String(row?.at ?? ''))
|
|
1451
|
+
return Number.isFinite(at) ? at : 0
|
|
1452
|
+
}
|
|
1453
|
+
const keyOf = (row) => [row?.name ?? '', row?.from ?? '', row?.to ?? '', row?.profile ?? ''].join('\u0000')
|
|
1454
|
+
|
|
1455
|
+
// 组 key 用「被更新的那次」的 (包, from, to, profile)。
|
|
1456
|
+
// 回滚行的 from/to 是反的(from=新版本, to=旧版本),换回来就落到同一个 key。
|
|
1457
|
+
// 这样一条更新和撤销它的回滚一定同进同退,不会出现半组。
|
|
1458
|
+
const groups = new Map()
|
|
1459
|
+
for (const row of rows) {
|
|
1460
|
+
const isRollback = row?.tag === 'rollback'
|
|
1461
|
+
const key = keyOf(isRollback
|
|
1462
|
+
? { name: row.name, from: row.to, to: row.from, profile: row.profile }
|
|
1463
|
+
: row)
|
|
1464
|
+
const group = groups.get(key) ?? { rows: [], newest: 0 }
|
|
1465
|
+
group.rows.push(row)
|
|
1466
|
+
group.newest = Math.max(group.newest, stampOf(row))
|
|
1467
|
+
groups.set(key, group)
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
const survivors = []
|
|
1471
|
+
for (const group of groups.values()) {
|
|
1472
|
+
if (group.newest >= cutoff) survivors.push(...group.rows)
|
|
1473
|
+
}
|
|
1474
|
+
const removed = rows.length - survivors.length
|
|
1475
|
+
if (removed > 0) {
|
|
1476
|
+
// 保持原有顺序(新→旧),组被打散过,按时间重排一次即可。
|
|
1477
|
+
survivors.sort((a, b) => stampOf(b) - stampOf(a))
|
|
1478
|
+
saveHistory(survivors)
|
|
1479
|
+
}
|
|
1480
|
+
return removed
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
/**
|
|
1484
|
+
* 按保留天数清理运行日志。
|
|
1485
|
+
*
|
|
1486
|
+
* 日志是「一行一条」的纯文本,每行以 [ISO 时间] 开头。逐行解析时间,只保留
|
|
1487
|
+
* cutoff 之后的行;解析不出时间的行(旧格式里偶有)一律保留——宁可多留,
|
|
1488
|
+
* 不因为格式不认识就删掉用户的现场。
|
|
1489
|
+
*
|
|
1490
|
+
* 写之前先 flush,否则待写队列里的新行还没落盘,写完被旧内容覆盖。
|
|
1491
|
+
*/
|
|
1492
|
+
function pruneLog(days, now) {
|
|
1493
|
+
if (!(days > 0)) return 0
|
|
1494
|
+
try {
|
|
1495
|
+
flushLogNow()
|
|
1496
|
+
} catch {}
|
|
1497
|
+
const file = logPath()
|
|
1498
|
+
let raw
|
|
1499
|
+
try {
|
|
1500
|
+
raw = readFileSync(file, 'utf8')
|
|
1501
|
+
} catch {
|
|
1502
|
+
return 0
|
|
1503
|
+
}
|
|
1504
|
+
const cutoff = now.getTime() - days * 86400_000
|
|
1505
|
+
const lines = raw.split('\n')
|
|
1506
|
+
if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
|
|
1507
|
+
const kept = []
|
|
1508
|
+
let removed = 0
|
|
1509
|
+
for (const line of lines) {
|
|
1510
|
+
const match = /^\[([^\]]+)\]/u.exec(line)
|
|
1511
|
+
if (match === null) {
|
|
1512
|
+
kept.push(line)
|
|
1513
|
+
continue
|
|
1514
|
+
}
|
|
1515
|
+
const at = Date.parse(match[1])
|
|
1516
|
+
if (Number.isFinite(at) && at < cutoff) removed += 1
|
|
1517
|
+
else kept.push(line)
|
|
1518
|
+
}
|
|
1519
|
+
if (removed === 0) return 0
|
|
1520
|
+
try {
|
|
1521
|
+
writeFileSync(file, kept.length > 0 ? kept.join('\n') + '\n' : '', 'utf8')
|
|
1522
|
+
} catch {
|
|
1523
|
+
return 0
|
|
1524
|
+
}
|
|
1525
|
+
return removed
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
/** 一次清理:记录、日志、快照目录。按各自设置独立开关,0 表示不动。 */
|
|
1529
|
+
function pruneAll(settings, now) {
|
|
1530
|
+
const historyDays = settings.historyRetentionDays
|
|
1531
|
+
const logDays = settings.logRetentionDays
|
|
1532
|
+
if (!(historyDays > 0) && !(logDays > 0)) return
|
|
1533
|
+
const removedHistory = pruneHistory(historyDays, now)
|
|
1534
|
+
const removedLog = pruneLog(logDays, now)
|
|
1535
|
+
if (removedHistory > 0) writeLog('cleaned', ['更新记录', removedHistory, historyDays])
|
|
1536
|
+
if (removedLog > 0) writeLog('cleaned', ['运行日志', removedLog, logDays])
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1379
1539
|
function profileLockPath(profileDir) {
|
|
1380
1540
|
return join(profileDir, '.aupdate-install.lock')
|
|
1381
1541
|
}
|
|
@@ -1650,7 +1810,8 @@ async function runCycle(mode, ctxRef) {
|
|
|
1650
1810
|
status: report.status,
|
|
1651
1811
|
at: report.finishedAt,
|
|
1652
1812
|
checked: report.checked,
|
|
1653
|
-
|
|
1813
|
+
// 结构化存:界面要分开排「包名」和「版本」,句子做不到。
|
|
1814
|
+
updates: report.updates.map((item) => ({ name: item.name, from: item.current ?? '', to: item.target })),
|
|
1654
1815
|
installed: report.installed.map((item) => item.name + '@' + item.version + (item.ok ? '' : ' !failed')),
|
|
1655
1816
|
reload: report.reload?.status ?? 'none',
|
|
1656
1817
|
reason: report.reason,
|
|
@@ -2021,6 +2182,11 @@ function settingsSnapshot(config, ctxRef, lang) {
|
|
|
2021
2182
|
failurePath: failurePath(),
|
|
2022
2183
|
// 动手前留的现场:设置页把它显示出来,出问题时有地方可查。
|
|
2023
2184
|
snapshotPath: snapshotRoot(),
|
|
2185
|
+
// 保留期限也放在 state 里:设置页显示当前选择,并且按它算出「下次会清掉什么」。
|
|
2186
|
+
retention: {
|
|
2187
|
+
historyDays: settings.historyRetentionDays,
|
|
2188
|
+
logDays: settings.logRetentionDays,
|
|
2189
|
+
},
|
|
2024
2190
|
// 为什么这一轮没动手:设置页把这两个直接显示出来,用户不用翻日志猜。
|
|
2025
2191
|
// 尤其 sessionIdleMinutes —— 它太短会挡住大半夜的更新,太长又不该挡,
|
|
2026
2192
|
// 得让人一眼看到「现在到底算不算闲」。
|
|
@@ -2103,6 +2269,25 @@ function registerRoutes(scope, ctxRef) {
|
|
|
2103
2269
|
sendJson(res, outcome.ok ? 200 : 409, { ...outcome, reason: renderReason(outcome.reason, url.searchParams.get('lang')) })
|
|
2104
2270
|
return
|
|
2105
2271
|
}
|
|
2272
|
+
if (suffix === '/cleanup') {
|
|
2273
|
+
// 用户手动点「立即清理」:用当前设置跑一遍,回报删了多少条。
|
|
2274
|
+
const live = settingsOf(ctxRef.config)
|
|
2275
|
+
const now = new Date()
|
|
2276
|
+
const removedHistory = pruneHistory(live.historyRetentionDays, now)
|
|
2277
|
+
const removedLog = pruneLog(live.logRetentionDays, now)
|
|
2278
|
+
if (removedHistory > 0) writeLog('cleaned', ['更新记录', removedHistory, live.historyRetentionDays])
|
|
2279
|
+
if (removedLog > 0) writeLog('cleaned', ['运行日志', removedLog, live.logRetentionDays])
|
|
2280
|
+
const lang = logLangOf(url.searchParams.get('lang'))
|
|
2281
|
+
sendJson(res, 200, {
|
|
2282
|
+
history: removedHistory,
|
|
2283
|
+
log: removedLog,
|
|
2284
|
+
days: { history: live.historyRetentionDays, log: live.logRetentionDays },
|
|
2285
|
+
message: removedHistory === 0 && removedLog === 0
|
|
2286
|
+
? LOG_TEMPLATES[lang].cleanSkipped('更新记录 / 运行日志')
|
|
2287
|
+
: LOG_TEMPLATES[lang].cleaned(removedHistory + removedLog > 0 ? '记录与日志' : '', removedHistory + removedLog, Math.max(live.historyRetentionDays, live.logRetentionDays)),
|
|
2288
|
+
})
|
|
2289
|
+
return
|
|
2290
|
+
}
|
|
2106
2291
|
if (suffix === '/run') {
|
|
2107
2292
|
const body = await readBody(req)
|
|
2108
2293
|
const mode = body?.mode === 'check' ? 'check' : 'update'
|
|
@@ -2140,7 +2325,7 @@ function registerRoutes(scope, ctxRef) {
|
|
|
2140
2325
|
}
|
|
2141
2326
|
}
|
|
2142
2327
|
|
|
2143
|
-
export { compareVersions, upgradeVerdict, pickRollbackTarget, readHistory, historyPath, distTagFor, cliRunner, dependencyList, installedVersion, registryTags, hotApply, runCycle, settingsOf, readOverrides, writeOverrides, takeProfileLock, releaseProfileLock, sessionAgeMinutes, activityReasons, settingsSnapshot, snapshotProfile, snapshotRoot, pruneSnapshots, nextDelayMinutes, readLogTail, logPath }
|
|
2328
|
+
export { compareVersions, upgradeVerdict, pickRollbackTarget, readHistory, historyPath, distTagFor, cliRunner, dependencyList, installedVersion, registryTags, hotApply, runCycle, settingsOf, readOverrides, writeOverrides, takeProfileLock, releaseProfileLock, sessionAgeMinutes, activityReasons, settingsSnapshot, snapshotProfile, snapshotRoot, pruneSnapshots, nextDelayMinutes, readLogTail, logPath, pruneAll, pruneHistory, pruneLog }
|
|
2144
2329
|
|
|
2145
2330
|
export function apply(ctx, config) {
|
|
2146
2331
|
const ctxRef = { ctx, config }
|
|
@@ -2217,6 +2402,23 @@ export function apply(ctx, config) {
|
|
|
2217
2402
|
void go()
|
|
2218
2403
|
}
|
|
2219
2404
|
|
|
2405
|
+
/**
|
|
2406
|
+
* 定期清理。
|
|
2407
|
+
*
|
|
2408
|
+
* 放在每次定时器触发前,而不是另起一个定时器:调度器本来就按当前设置走,
|
|
2409
|
+
* 清理跟着它走就不会出现「两个节奏各跑各的、改了设置只有一个生效」。
|
|
2410
|
+
* 启动时也跑一次(见下面的 pruneAll(settings, new Date())),保证长时间不开
|
|
2411
|
+
* DSH 的用户一进来就看不到过期记录。
|
|
2412
|
+
*/
|
|
2413
|
+
const runCleanup = () => {
|
|
2414
|
+
try {
|
|
2415
|
+
pruneAll(settingsOf(ctxRef.config), new Date())
|
|
2416
|
+
} catch (error) {
|
|
2417
|
+
writeLog('routeFailed', [oneLine(error?.message ?? error, 200)])
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
runCleanup()
|
|
2421
|
+
|
|
2220
2422
|
/**
|
|
2221
2423
|
* 定时器改成「每次重排」,而不是一次性 setInterval。
|
|
2222
2424
|
*
|
|
@@ -2258,6 +2460,7 @@ export function apply(ctx, config) {
|
|
|
2258
2460
|
ctxRef.pendingMinutes = minutes
|
|
2259
2461
|
const handle = setTimeout(() => {
|
|
2260
2462
|
scheduled = null
|
|
2463
|
+
runCleanup()
|
|
2261
2464
|
runSafely('update')
|
|
2262
2465
|
scheduleNext()
|
|
2263
2466
|
}, Math.max(minutes, 0) * 60000)
|
package/package.json
CHANGED