dsh-pick 0.2.2 → 0.2.4
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 +4 -3
- package/package.json +1 -1
- package/plugin/client.js +162 -8
- package/plugin/index.js +152 -0
- package/web/panel.html +11 -1
package/README.md
CHANGED
|
@@ -38,7 +38,8 @@ npm 官网和各类 stat 站能看数字,但看数字回答不了「我该装
|
|
|
38
38
|
dsh plugin --profile web add dsh-pick
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
装完重启 `dsh web
|
|
41
|
+
装完重启 `dsh web`,在**右侧栏**的引导页点「选插件」,就是完整的选插件面板(**设置 → dsh-pick** 里也暂时保留一份)。
|
|
42
|
+
面板顶部一行:插件名、当前版本号、更新按钮,最右侧是卸载按钮——更新和卸载都交给 DSH 自己的插件管理器,完成后重启 DSH 生效。
|
|
42
43
|
|
|
43
44
|
首次使用要先生成目录(在 dsh-pick 的安装目录下,或任意目录):
|
|
44
45
|
|
|
@@ -95,8 +96,8 @@ dsh-pick/
|
|
|
95
96
|
├── data/catalog.json 目录产物
|
|
96
97
|
├── plugin/
|
|
97
98
|
│ ├── index.js DSH 宿主端:挂 /api-dsh-pick/* 路由
|
|
98
|
-
│ ├── client.js 设置页面板(iframe 复用 web/panel.html)
|
|
99
|
-
│ └── cordis.patch.yml
|
|
99
|
+
│ ├── client.js 右侧栏页签 + 设置页面板(iframe 复用 web/panel.html)
|
|
100
|
+
│ └── cordis.patch.yml 插进 DSH
|
|
100
101
|
└── web/
|
|
101
102
|
├── panel.html 选插件面板(独立调试用,也是插件内嵌的同一份)
|
|
102
103
|
└── panel.js 面板逻辑
|
package/package.json
CHANGED
package/plugin/client.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// plugin/client.js — dsh-pick 的设置页
|
|
3
3
|
//
|
|
4
4
|
// 手写单文件 bundle,没有构建链:由 dsh web 客户端的 ModuleLoader 注入
|
|
5
|
-
// (写法照 dsh-model / dsh-vps-manager
|
|
5
|
+
// (写法照 dsh-model / dsh-vps-manager)。挂两处:DSH 右侧栏的一个页签(主入口),
|
|
6
|
+
// 和「设置 → dsh-pick」(暂时保留,内容相同)。
|
|
6
7
|
//
|
|
7
8
|
// 界面本身就是一个 iframe,指向宿主路由 /api-dsh-pick/panel —— 面板那套 UI
|
|
8
9
|
// 是独立调试过的(dsh-pick serve 直接能开),这里原样复用,不再用 React 重写一遍。
|
|
@@ -29,12 +30,121 @@ window.__ModuleLoader__.load({
|
|
|
29
30
|
sub: 'var(--dsw-alias-label-secondary, #888)',
|
|
30
31
|
faint: 'var(--dsw-alias-label-tertiary, #999)',
|
|
31
32
|
border: 'var(--dsw-alias-border-l3, rgba(127,127,127,.25))',
|
|
33
|
+
accent: '#2f6bff',
|
|
34
|
+
danger: '#c94040',
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
// 和宿主通信:会改东西的请求用 JSON POST(宿主只收同源 JSON)
|
|
38
|
+
async function api(path, method = 'GET') {
|
|
39
|
+
const r = await fetch('/api-dsh-pick/' + path, {
|
|
40
|
+
method,
|
|
41
|
+
credentials: 'same-origin',
|
|
42
|
+
headers: method === 'POST' ? { 'content-type': 'application/json' } : undefined,
|
|
43
|
+
body: method === 'POST' ? '{}' : undefined,
|
|
44
|
+
})
|
|
45
|
+
return r.json()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function Btn({ onClick, disabled, danger, primary, title, children }) {
|
|
49
|
+
return h('button', {
|
|
50
|
+
type: 'button', onClick, disabled, title,
|
|
51
|
+
style: {
|
|
52
|
+
appearance: 'none', fontFamily: 'inherit', fontSize: 12, lineHeight: '18px',
|
|
53
|
+
padding: '2px 10px', borderRadius: 6, cursor: disabled ? 'default' : 'pointer',
|
|
54
|
+
opacity: disabled ? 0.6 : 1, whiteSpace: 'nowrap',
|
|
55
|
+
border: '1px solid ' + (danger ? C.danger : primary ? C.accent : C.border),
|
|
56
|
+
background: primary ? C.accent : 'transparent',
|
|
57
|
+
color: primary ? '#fff' : danger ? C.danger : C.text,
|
|
58
|
+
},
|
|
59
|
+
}, children)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 标题行:dsh-pick · 版本号 · 更新 ……(最右)卸载 */
|
|
63
|
+
function HeaderBar() {
|
|
64
|
+
const [info, setInfo] = useState(null)
|
|
65
|
+
const [phase, setPhase] = useState('checking') // checking | idle | updating | updated | removing | removed
|
|
66
|
+
const [confirm, setConfirm] = useState(false)
|
|
67
|
+
const [msg, setMsg] = useState(null) // { text, command? }
|
|
68
|
+
|
|
69
|
+
const check = (force) => {
|
|
70
|
+
setPhase('checking')
|
|
71
|
+
api('update' + (force ? '?force=1' : ''))
|
|
72
|
+
.then(j => { setInfo(j); setPhase('idle') })
|
|
73
|
+
.catch(() => setPhase('idle'))
|
|
74
|
+
}
|
|
75
|
+
useEffect(() => { check(false) }, [])
|
|
76
|
+
|
|
77
|
+
const update = async () => {
|
|
78
|
+
setPhase('updating'); setMsg(null)
|
|
79
|
+
try {
|
|
80
|
+
const j = await api('update', 'POST')
|
|
81
|
+
if (j.ok) { setPhase('updated'); return }
|
|
82
|
+
setMsg({ text: j.message, command: j.command })
|
|
83
|
+
} catch (e) {
|
|
84
|
+
setMsg({ text: L('更新请求失败:', 'Update failed: ') + e.message })
|
|
85
|
+
}
|
|
86
|
+
setPhase('idle')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const uninstall = async () => {
|
|
90
|
+
setConfirm(false); setPhase('removing'); setMsg(null)
|
|
91
|
+
try {
|
|
92
|
+
const j = await api('uninstall', 'POST')
|
|
93
|
+
if (j.ok) { setPhase('removed'); return }
|
|
94
|
+
setMsg({ text: j.message, command: j.command })
|
|
95
|
+
setPhase('idle')
|
|
96
|
+
} catch {
|
|
97
|
+
// DSH 卸下插件时自己的接口也随之消失,请求多半就是这么断的
|
|
98
|
+
setPhase('removed')
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let upd
|
|
103
|
+
if (phase === 'updating') upd = { label: L('正在更新…', 'Updating…'), disabled: true }
|
|
104
|
+
else if (phase === 'updated') upd = { label: L('已更新,重启 DSH 生效', 'Updated; restart DSH'), disabled: true }
|
|
105
|
+
else if (info && info.restartPending) upd = { label: L(`v${info.restartPending} 已装好,重启 DSH 生效`, `v${info.restartPending} installed; restart DSH`), disabled: true }
|
|
106
|
+
else if (info && info.available) upd = { label: L(`更新到 v${info.latest}`, `Update to v${info.latest}`), primary: true, onClick: update }
|
|
107
|
+
else if (phase === 'checking') upd = { label: L('检查更新…', 'Checking…'), disabled: true }
|
|
108
|
+
else upd = {
|
|
109
|
+
label: info && info.latest ? L('已是最新', 'Up to date') : L('检查更新', 'Check for updates'),
|
|
110
|
+
onClick: () => check(true),
|
|
111
|
+
title: L('点一下重新检查', 'Click to check again'),
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const busy = phase === 'updating' || phase === 'removing' || phase === 'removed'
|
|
115
|
+
|
|
116
|
+
return h('div', { style: { marginBottom: 8 } },
|
|
117
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' } },
|
|
118
|
+
h('span', { style: { fontSize: 15, fontWeight: 650 } }, 'dsh-pick'),
|
|
119
|
+
info && info.running ? h('span', { style: { fontSize: 12, color: C.sub, fontVariantNumeric: 'tabular-nums' } }, 'v' + info.running) : null,
|
|
120
|
+
h(Btn, { onClick: upd.onClick, disabled: upd.disabled || busy, primary: upd.primary, title: upd.title }, upd.label),
|
|
121
|
+
h('span', { style: { flex: 1 } }),
|
|
122
|
+
phase === 'removed'
|
|
123
|
+
? h('span', { style: { fontSize: 12, color: C.sub } }, L('已卸载,重启 DSH 后彻底移除', 'Uninstalled; restart DSH to finish'))
|
|
124
|
+
: phase === 'removing'
|
|
125
|
+
? h('span', { style: { fontSize: 12, color: C.sub } }, L('正在卸载…', 'Uninstalling…'))
|
|
126
|
+
: confirm
|
|
127
|
+
? h('span', { style: { display: 'inline-flex', gap: 6, alignItems: 'center' } },
|
|
128
|
+
h('span', { style: { fontSize: 12, color: C.sub } }, L('确定卸载?', 'Uninstall?')),
|
|
129
|
+
h(Btn, { danger: true, onClick: uninstall }, L('确认卸载', 'Confirm')),
|
|
130
|
+
h(Btn, { onClick: () => setConfirm(false) }, L('取消', 'Cancel')))
|
|
131
|
+
: h(Btn, { danger: true, disabled: busy, onClick: () => setConfirm(true) }, L('卸载', 'Uninstall')),
|
|
132
|
+
),
|
|
133
|
+
msg ? h('div', { style: { fontSize: 12, color: C.danger, marginTop: 6, lineHeight: 1.6 } },
|
|
134
|
+
msg.text,
|
|
135
|
+
msg.command ? h('code', { style: { display: 'block', marginTop: 4, color: C.text, userSelect: 'all' } }, msg.command) : null,
|
|
136
|
+
) : null,
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 面板本体。fill = 放在右侧栏里:占满栏高,iframe 自己滚动;
|
|
142
|
+
* 否则(设置页)iframe 跟着内容长高,由设置页滚动
|
|
143
|
+
*/
|
|
144
|
+
function PickPanel({ fill }) {
|
|
35
145
|
const frame = useRef(null)
|
|
36
146
|
const [meta, setMeta] = useState(null)
|
|
37
|
-
const [
|
|
147
|
+
const [frameH, setFrameH] = useState(720)
|
|
38
148
|
|
|
39
149
|
useEffect(() => {
|
|
40
150
|
let alive = true
|
|
@@ -46,18 +156,19 @@ window.__ModuleLoader__.load({
|
|
|
46
156
|
}, [])
|
|
47
157
|
|
|
48
158
|
useEffect(() => {
|
|
159
|
+
if (fill) return undefined
|
|
49
160
|
// iframe 里改不了高度(跨文档),用一个粗策略:内容高差太多就跟着长
|
|
50
161
|
const t = setInterval(() => {
|
|
51
162
|
try {
|
|
52
163
|
const doc = frame.current && frame.current.contentDocument
|
|
53
164
|
if (doc && doc.body) {
|
|
54
165
|
const need = Math.max(720, doc.body.scrollHeight + 40)
|
|
55
|
-
if (Math.abs(need -
|
|
166
|
+
if (Math.abs(need - frameH) > 40) setFrameH(need)
|
|
56
167
|
}
|
|
57
168
|
} catch { /* 跨域就不管,保持默认高度 */ }
|
|
58
169
|
}, 1500)
|
|
59
170
|
return () => clearInterval(t)
|
|
60
|
-
}, [
|
|
171
|
+
}, [frameH, fill])
|
|
61
172
|
|
|
62
173
|
const tip = !meta
|
|
63
174
|
? L('正在读取目录…', 'Loading catalog…')
|
|
@@ -67,7 +178,12 @@ window.__ModuleLoader__.load({
|
|
|
67
178
|
`${meta.total.toLocaleString()} plugins · ${meta.deepCount} with denoised metrics · as of ${meta.anchor}`)
|
|
68
179
|
: L('目录未生成:在 dsh-pick 目录跑一次 dsh-pick build', 'Catalog not built yet: run `dsh-pick build`')
|
|
69
180
|
|
|
70
|
-
return h('div', {
|
|
181
|
+
return h('div', {
|
|
182
|
+
style: fill
|
|
183
|
+
? { color: C.text, height: '100%', display: 'flex', flexDirection: 'column', padding: '10px 12px', boxSizing: 'border-box' }
|
|
184
|
+
: { color: C.text },
|
|
185
|
+
},
|
|
186
|
+
h(HeaderBar),
|
|
71
187
|
h('div', {
|
|
72
188
|
style: {
|
|
73
189
|
fontSize: 12, color: C.faint, margin: '2px 0 10px', lineHeight: 1.6,
|
|
@@ -80,7 +196,9 @@ window.__ModuleLoader__.load({
|
|
|
80
196
|
title: 'dsh-pick',
|
|
81
197
|
style: {
|
|
82
198
|
width: '100%',
|
|
83
|
-
height:
|
|
199
|
+
height: fill ? 'auto' : frameH + 'px',
|
|
200
|
+
flex: fill ? '1 1 0' : undefined,
|
|
201
|
+
minHeight: fill ? 0 : undefined,
|
|
84
202
|
border: '1px solid ' + C.border,
|
|
85
203
|
borderRadius: '10px',
|
|
86
204
|
background: 'transparent',
|
|
@@ -89,7 +207,7 @@ window.__ModuleLoader__.load({
|
|
|
89
207
|
}),
|
|
90
208
|
|
|
91
209
|
h('div', {
|
|
92
|
-
style: { fontSize: 11, color: C.faint, marginTop: 8, lineHeight: 1.7 },
|
|
210
|
+
style: { fontSize: 11, color: C.faint, marginTop: 8, lineHeight: 1.7, display: fill ? 'none' : undefined },
|
|
93
211
|
},
|
|
94
212
|
L(
|
|
95
213
|
'排名按周下载;日常基线已扣掉版本发布日与次日的拉取虚高,比原始下载量更接近真实使用量。' +
|
|
@@ -100,8 +218,44 @@ window.__ModuleLoader__.load({
|
|
|
100
218
|
)
|
|
101
219
|
}
|
|
102
220
|
|
|
221
|
+
// 右侧栏页签(DSH 0.2 起有右侧栏;老版本没有这个服务就只在设置页里)
|
|
222
|
+
const SIDEBAR_TAB_ID = 'dsh-pick/panel'
|
|
223
|
+
const SIDEBAR_KIND = 'dsh-pick'
|
|
224
|
+
const SidebarBody = () => h(PickPanel, { fill: true })
|
|
225
|
+
|
|
226
|
+
function PickIcon({ size = 16 }) {
|
|
227
|
+
return h('svg', { width: size, height: size, viewBox: '0 0 32 32', fill: 'none' },
|
|
228
|
+
h('path', { d: 'M6 22l6-7 5 5 9-11', stroke: 'currentColor', strokeWidth: 2.6, strokeLinecap: 'round', strokeLinejoin: 'round' }))
|
|
229
|
+
}
|
|
230
|
+
|
|
103
231
|
const injectSlots = ['slots']
|
|
104
232
|
function apply(ctx) {
|
|
233
|
+
try {
|
|
234
|
+
ctx.inject && ctx.inject(['sidebarRight', 'sidebarRightTabs'], (scope) => {
|
|
235
|
+
const tabs = (scope.get && scope.get('sidebarRightTabs')) || scope.sidebarRightTabs
|
|
236
|
+
if (!tabs || typeof tabs.register !== 'function') return
|
|
237
|
+
const effect = (fn, label) => (scope.effect ? scope.effect(fn, label) : fn())
|
|
238
|
+
effect(() => tabs.register({
|
|
239
|
+
id: SIDEBAR_TAB_ID,
|
|
240
|
+
kind: SIDEBAR_KIND,
|
|
241
|
+
priority: 'extension',
|
|
242
|
+
keepMounted: true,
|
|
243
|
+
title: () => 'dsh-pick',
|
|
244
|
+
guide: [{
|
|
245
|
+
id: 'dsh-pick',
|
|
246
|
+
order: 70,
|
|
247
|
+
title: () => L('选插件', 'Pick a plugin'),
|
|
248
|
+
description: () => L('说需求 → 找候选 → 多维比较 → 出结论', 'Describe a need, compare candidates, get a verdict'),
|
|
249
|
+
icon: PickIcon,
|
|
250
|
+
}],
|
|
251
|
+
}), 'dsh-pick: sidebar type')
|
|
252
|
+
const slots = scope.slots || ctx.slots
|
|
253
|
+
const body = () => slots.register({ name: 'sidebar.right.pane.tab', key: SIDEBAR_TAB_ID }, SidebarBody)
|
|
254
|
+
effect(() => (slots.inject ? slots.inject('sidebar.right.pane.tab', body) : body()), 'dsh-pick: sidebar body')
|
|
255
|
+
})
|
|
256
|
+
} catch (error) {
|
|
257
|
+
console.warn('[dsh-pick] 右侧栏注册失败', error)
|
|
258
|
+
}
|
|
105
259
|
try {
|
|
106
260
|
ctx.slots.inject('settings.section', () =>
|
|
107
261
|
ctx.slots.register(
|
package/plugin/index.js
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
// GET /api-dsh-pick/words.js 搜索词典
|
|
10
10
|
// GET /api-dsh-pick/catalog.json 目录(含排名、链接、去噪指标)
|
|
11
11
|
// GET /api-dsh-pick/meta 目录概要(更新时间、包数)
|
|
12
|
+
// GET /api-dsh-pick/update 本插件版本 + npm 上的最新版本
|
|
13
|
+
// POST /api-dsh-pick/update 更新到 npm 最新版(交给 DSH 的插件管理器)
|
|
14
|
+
// POST /api-dsh-pick/uninstall 从 DSH 卸载本插件
|
|
12
15
|
//
|
|
13
16
|
// 写法照 dsh-model(同机参考)与 dsh-vps-manager(同作者)的 plugin/index.js。
|
|
14
17
|
|
|
@@ -38,7 +41,82 @@ async function readPkgJson() {
|
|
|
38
41
|
return JSON.parse(await readFile(join(PKG_ROOT, 'package.json'), 'utf8'))
|
|
39
42
|
} catch {
|
|
40
43
|
return {}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const PACKAGE = 'dsh-pick'
|
|
48
|
+
// npm 官方源连不上(国内常见)就问镜像
|
|
49
|
+
const NPM_REGISTRIES = ['https://registry.npmjs.org', 'https://registry.npmmirror.com']
|
|
50
|
+
const VERSION_RE = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
|
|
51
|
+
|
|
52
|
+
/** a 比 b 新返回正数(x.y.z,预发布版比同号正式版旧) */
|
|
53
|
+
function compareVersions(a, b) {
|
|
54
|
+
const parse = v => {
|
|
55
|
+
const [core, pre = ''] = String(v || '').replace(/^v/, '').split('-', 2)
|
|
56
|
+
return { nums: core.split('.').map(n => parseInt(n, 10) || 0), pre }
|
|
57
|
+
}
|
|
58
|
+
const x = parse(a), y = parse(b)
|
|
59
|
+
for (let i = 0; i < 3; i++) {
|
|
60
|
+
const d = (x.nums[i] || 0) - (y.nums[i] || 0)
|
|
61
|
+
if (d) return d
|
|
62
|
+
}
|
|
63
|
+
if (x.pre === y.pre) return 0
|
|
64
|
+
if (!x.pre) return 1
|
|
65
|
+
if (!y.pre) return -1
|
|
66
|
+
return x.pre < y.pre ? -1 : 1
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** npm 上 latest 是哪个版本;10 分钟内复用上次的结果 */
|
|
70
|
+
let npmCache = null
|
|
71
|
+
async function latestOnNpm(force) {
|
|
72
|
+
if (!force && npmCache && Date.now() - npmCache.at < 10 * 60 * 1000) return npmCache.version
|
|
73
|
+
for (const registry of NPM_REGISTRIES) {
|
|
74
|
+
try {
|
|
75
|
+
const r = await fetch(`${registry}/${PACKAGE}/latest`, { signal: AbortSignal.timeout(6000) })
|
|
76
|
+
if (!r.ok) continue
|
|
77
|
+
const { version } = await r.json()
|
|
78
|
+
if (VERSION_RE.test(String(version || ''))) {
|
|
79
|
+
npmCache = { version, at: Date.now() }
|
|
80
|
+
return version
|
|
81
|
+
}
|
|
82
|
+
} catch { /* 换下一个源 */ }
|
|
83
|
+
}
|
|
84
|
+
return ''
|
|
41
85
|
}
|
|
86
|
+
|
|
87
|
+
/** 给用户自己在终端执行的命令:DSH 0.2 起 --profile 必填,且要写在 add / remove 前面 */
|
|
88
|
+
function pluginCommand(args, profile) {
|
|
89
|
+
return /^[A-Za-z0-9._-]+$/.test(profile || '') ? `dsh plugin --profile ${profile} ${args}` : `dsh plugin ${args}`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 插件管理器的失败结果 → 一行人话(取诊断输出的最后几行) */
|
|
93
|
+
function managerError(result) {
|
|
94
|
+
if (!(result && (result.application === 'failed' || result.application === 'cancelled' || result.error))) return ''
|
|
95
|
+
const code = (result.error && result.error.code) || result.application || 'failed'
|
|
96
|
+
const detail = String((result.error && result.error.diagnostic) || (result.packageResult && result.packageResult.output) || '')
|
|
97
|
+
.trim().split('\n').slice(-3).join(' ').slice(-300)
|
|
98
|
+
return detail ? `${code}:${detail}` : code
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function sendJson(res, status, body) {
|
|
102
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
103
|
+
res.end(JSON.stringify(body))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 会改东西的请求(更新、卸载)只收同源的 JSON POST:
|
|
108
|
+
* 别的网站发不出带 application/json 的跨站请求(要预检),Origin 也对不上
|
|
109
|
+
*/
|
|
110
|
+
function isSafePost(req) {
|
|
111
|
+
if (req.method !== 'POST') return false
|
|
112
|
+
if (!String(req.headers['content-type'] || '').startsWith('application/json')) return false
|
|
113
|
+
const origin = req.headers.origin
|
|
114
|
+
if (!origin) return true
|
|
115
|
+
try {
|
|
116
|
+
return new URL(origin).host === req.headers.host
|
|
117
|
+
} catch {
|
|
118
|
+
return false
|
|
119
|
+
}
|
|
42
120
|
}
|
|
43
121
|
|
|
44
122
|
/** 目录概要:给面板顶部显示「更新时间 / 包数」用 */
|
|
@@ -61,6 +139,21 @@ export const name = 'dsh-pick'
|
|
|
61
139
|
export const inject = []
|
|
62
140
|
|
|
63
141
|
export function apply(ctx) {
|
|
142
|
+
// 启动时加载的版本;磁盘上的可能更新(更新了还没重启)
|
|
143
|
+
let running = ''
|
|
144
|
+
readPkgJson().then(p => { running = p.version || '' })
|
|
145
|
+
|
|
146
|
+
// DSH 自己的插件管理器(和 DSH「插件」页同一套:带锁跑 pnpm、失败自动还原)。老版本 DSH 没有
|
|
147
|
+
let pluginManager
|
|
148
|
+
ctx.inject(['pluginManager'], (pmCtx) => {
|
|
149
|
+
pluginManager = pmCtx.pluginManager || (pmCtx.get && pmCtx.get('pluginManager'))
|
|
150
|
+
})
|
|
151
|
+
// 当前 profile(官方桌面版 desktop,dsh web 是 web),只用来拼给用户手动执行的命令
|
|
152
|
+
let profile
|
|
153
|
+
try {
|
|
154
|
+
profile = ctx.get && ctx.get('profileContext') && ctx.get('profileContext').name
|
|
155
|
+
} catch { profile = undefined }
|
|
156
|
+
|
|
64
157
|
ctx.inject(['webServer'], (webCtx) => {
|
|
65
158
|
const ws = webCtx.webServer
|
|
66
159
|
if (!ws || typeof ws.register !== 'function') return
|
|
@@ -101,6 +194,65 @@ export function apply(ctx) {
|
|
|
101
194
|
},
|
|
102
195
|
}))
|
|
103
196
|
|
|
197
|
+
// 版本与更新
|
|
198
|
+
track(ws.register({
|
|
199
|
+
kind: 'exact',
|
|
200
|
+
path: '/api-dsh-pick/update',
|
|
201
|
+
handler: async (req, res) => {
|
|
202
|
+
if (req.method === 'GET') {
|
|
203
|
+
const force = /[?&]force=1/.test(req.url || '')
|
|
204
|
+
const [pkg, latest] = await Promise.all([readPkgJson(), latestOnNpm(force)])
|
|
205
|
+
const installed = pkg.version || ''
|
|
206
|
+
const current = compareVersions(installed, running) > 0 ? installed : running
|
|
207
|
+
return sendJson(res, 200, {
|
|
208
|
+
running,
|
|
209
|
+
restartPending: installed && running && compareVersions(installed, running) > 0 ? installed : '',
|
|
210
|
+
latest,
|
|
211
|
+
available: Boolean(latest) && compareVersions(latest, current) > 0,
|
|
212
|
+
canInstall: Boolean(pluginManager && typeof pluginManager.installBundle === 'function'),
|
|
213
|
+
command: pluginCommand(`add ${PACKAGE}@${latest || 'latest'}`, profile),
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
if (!isSafePost(req)) return sendJson(res, 403, { ok: false, message: '只接受本页面发出的请求' })
|
|
217
|
+
const latest = await latestOnNpm(true)
|
|
218
|
+
const command = pluginCommand(`add ${PACKAGE}@${latest || 'latest'}`, profile)
|
|
219
|
+
if (!latest) return sendJson(res, 200, { ok: false, message: '查不到 npm 上的最新版本,检查网络后再试', command })
|
|
220
|
+
if (compareVersions(latest, running) <= 0) return sendJson(res, 200, { ok: false, message: `已经是最新版 v${running}` })
|
|
221
|
+
if (!pluginManager || typeof pluginManager.installBundle !== 'function') {
|
|
222
|
+
return sendJson(res, 200, { ok: false, message: '这个版本的 DSH 不能在这里装插件,请在终端执行命令', command })
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
const result = await pluginManager.installBundle(`${PACKAGE}@${latest}`, { enabled: true })
|
|
226
|
+
const err = managerError(result)
|
|
227
|
+
if (err) return sendJson(res, 200, { ok: false, message: `更新失败(${err})`, command })
|
|
228
|
+
return sendJson(res, 200, { ok: true, version: latest })
|
|
229
|
+
} catch (e) {
|
|
230
|
+
return sendJson(res, 200, { ok: false, message: `更新失败:${e.message}`, command })
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
}))
|
|
234
|
+
|
|
235
|
+
// 卸载本插件
|
|
236
|
+
track(ws.register({
|
|
237
|
+
kind: 'exact',
|
|
238
|
+
path: '/api-dsh-pick/uninstall',
|
|
239
|
+
handler: async (req, res) => {
|
|
240
|
+
if (!isSafePost(req)) return sendJson(res, 403, { ok: false, message: '只接受本页面发出的请求' })
|
|
241
|
+
const command = pluginCommand(`remove ${PACKAGE}`, profile)
|
|
242
|
+
if (!pluginManager || typeof pluginManager.removeBundle !== 'function') {
|
|
243
|
+
return sendJson(res, 200, { ok: false, message: '这里没法直接卸载:完全退出 DSH 后在终端执行命令', command })
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const result = await pluginManager.removeBundle(PACKAGE)
|
|
247
|
+
const err = managerError(result)
|
|
248
|
+
if (err) return sendJson(res, 200, { ok: false, message: `卸载失败(${err}),可完全退出 DSH 后在终端执行命令`, command })
|
|
249
|
+
return sendJson(res, 200, { ok: true })
|
|
250
|
+
} catch (e) {
|
|
251
|
+
return sendJson(res, 200, { ok: false, message: `卸载失败:${e.message}`, command })
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
}))
|
|
255
|
+
|
|
104
256
|
// 面板是内嵌在 dsh 设置页里的 iframe,同源即可,不需要额外令牌:
|
|
105
257
|
// 这些路由全都先过 dsh 的登录校验(挂在 dsh 自己的 webServer 上)。
|
|
106
258
|
})
|
package/web/panel.html
CHANGED
|
@@ -77,7 +77,7 @@ h1{margin:0;font-size:19px;font-weight:650;letter-spacing:-.2px}
|
|
|
77
77
|
.section .cap{margin:0 0 15px;font-size:12px;color:var(--t3)}
|
|
78
78
|
|
|
79
79
|
/* ---------- 候选卡 ---------- */
|
|
80
|
-
.cands{display:grid;grid-template-columns:repeat(auto-fill,minmax(330px,1fr));gap:11px}
|
|
80
|
+
.cands{display:grid;grid-template-columns:repeat(auto-fill,minmax(min(330px,100%),1fr));gap:11px}
|
|
81
81
|
.cand{border:1px solid var(--line);border-radius:11px;padding:13px 14px;background:#fcfcfd;
|
|
82
82
|
display:flex;gap:11px;cursor:pointer;transition:border-color .15s,background .15s;position:relative}
|
|
83
83
|
.cand:hover{border-color:#c6d4f5}
|
|
@@ -154,6 +154,16 @@ td.down{color:var(--bad);font-weight:600}
|
|
|
154
154
|
.meta{margin-left:0;text-align:left;width:100%}
|
|
155
155
|
.ask{padding:17px}
|
|
156
156
|
}
|
|
157
|
+
/* 右侧栏这种窄宽度 */
|
|
158
|
+
@media(max-width:480px){
|
|
159
|
+
body{padding:0 8px 40px}
|
|
160
|
+
.tagline{display:none}
|
|
161
|
+
.tabs{width:auto}
|
|
162
|
+
.tabs button{padding:7px 10px}
|
|
163
|
+
.ask .row{flex-direction:column}
|
|
164
|
+
.ask .row .btn{padding:10px 0}
|
|
165
|
+
.section{padding:15px 13px}
|
|
166
|
+
}
|
|
157
167
|
</style>
|
|
158
168
|
</head>
|
|
159
169
|
<body>
|