dsh-task-panel-local 0.0.0-stage → 0.5.31

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 CHANGED
@@ -1,3 +1,82 @@
1
- # Temporary Holding Version
1
+ # 脚本与计划 · dsh-task-panel-local
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 看一眼这台机器上**有哪些计划任务和脚本在跑**,顺手能手动跑一次、看输出尾巴。不启停、不删除。
4
+
5
+ 计划任务就是「几点该谁干活」——所以叫班表。
6
+
7
+ ## 打开后长什么样
8
+
9
+ 四个标签,默认停在第一个:
10
+
11
+ 1. **本机计划任务** —— 这台机器的 Windows 计划任务(`\Microsoft\` 下的系统任务一条都不列)
12
+ 2. **最近计划任务** —— 最近跑过的,含它自己跑的和我手动点过的
13
+ 3. **本机脚本** —— 「脚本目录」里那些脚本
14
+ 4. **最近脚本** —— 最近跑过的脚本和它们的输出尾巴
15
+
16
+ 计划任务默认只显示名字带 `Dsh` 的(= DSH 自己的那几个),配置卡里点「全部」能看本机所有任务,选哪个会记住。
17
+
18
+ ## 配置(一次就够)
19
+
20
+ 设置页 →「脚本与计划」→「配置」:
21
+
22
+ - **脚本目录**:一行一个目录,散在几个文件夹里就都写上;留空就只看计划任务
23
+ - 第一次装上没填也能用:会自动找 DSH_HOME\toolkit、DSH_HOME\scripts、当前目录下的 oolkit / scripts,找到哪个算哪个(卡片标题会标「自动找到的目录」,改完点保存就固定)
24
+ - **新增目录**:把路径粘进去点「加进列表」,不用手改文本框
25
+
26
+ 写进 `~/.dsh/task-panel.config.json`,改完下一次刷新就生效,不用重载插件:
27
+
28
+ ```json
29
+ {
30
+ "taskScope": "mine",
31
+ "scriptDirs": ["D:\\scripts", "C:\\tools\\scripts"]
32
+ }
33
+ ```
34
+
35
+ `taskScope` 取 `mine` 或 `all`;老版本只认单目录的 `scriptDir`,现在两样都认、`scriptDirs` 优先。
36
+ 优先级:插件自身 config(非空才用)→ 这个 json → 空。空着的时候页面只显示「还没配脚本目录」,不报红。
37
+
38
+ ## 能做的事
39
+
40
+ **立即跑一次**(所有按钮都要点两下:第一下变「确定跑?」)
41
+
42
+ - **计划任务**:`Start-ScheduledTask`,只认面板里列出来的任务、且不在 `\Microsoft\` 下的,其它一律拒绝
43
+ - **脚本**:按后缀挑解释器,输出写进 `<该脚本所在目录>\logs\<名字>-<时间>.log`
44
+ - `.mjs/.js` → 当前 DSH 的 node(`ELECTRON_RUN_AS_NODE=1`),不写死路径
45
+ - `.py` → `python` `.ps1` → `powershell -ExecutionPolicy Bypass -File` `.cmd/.bat` → `cmd /c`
46
+
47
+ 脚本名只认文件名(不接受路径),解析后必须仍在脚本目录里。
48
+
49
+ **没跑成功的**会集中在配置卡下面一张卡里:自动跑失败的、我手动点失败的都在里面,每行三个按钮:
50
+
51
+ - **复制**:复制错误码或原因
52
+ - **我知道了**:只收起这一行(自动失败的会记住,重开也不再出现)
53
+ - **跑一遍**:再试一次
54
+
55
+ 再手动跑一次、或者它自己又失败,卡片会重新出现。
56
+
57
+ **看日志**:脚本行右侧「看日志」看最近一次输出的最后 200 行。
58
+
59
+ ## 刻意不做的
60
+
61
+ - **启停**:误点一下就会静默停掉守护任务,代价比便利大;需要时在命令行 `Stop-ScheduledTask`,还能顺手记一笔为什么
62
+ - **删除**:同上。真要删,任务先 `Export-ScheduledTask > 名字.xml` 存一份,脚本先挪进 `archive\` 再删
63
+ - **别的**:DSH 自己的 `schedule_*` 与「鱼的心跳」已有工具,这里再管就是两套状态打架;进程管理也不碰
64
+
65
+ ## 装
66
+
67
+ ```bash
68
+ dsh plugin add dsh-task-panel-local
69
+ ```
70
+
71
+ 装上后进「设置 → 脚本与计划」,先填脚本目录(不填也能用,只是只看任务)。
72
+
73
+ ## 开发
74
+
75
+ ```bash
76
+ node --check index.js && node --check client.js
77
+ ```
78
+
79
+ `client.js` 里注册的 id 必须等于包名,否则客户端整块不挂载。
80
+ 本机路径一律只出现在 `~/.dsh/task-panel.config.json`,不写进随包发布的 `cordis.patch.yml`。
81
+
82
+ MIT · 作者 鹿野
package/client.js ADDED
@@ -0,0 +1,571 @@
1
+ /**
2
+ * dsh-task-panel-local — 脚本与计划(Client 半边,0.5.0)
3
+ * 看计划任务与脚本;能「立即跑一次」并看脚本日志尾巴。不启停、不删除。
4
+ */
5
+ // 注意:下面的 id 必须等于 package.json 的 name(DSH 按入口名判重)
6
+ window.__ModuleLoader__.load({
7
+ id: 'dsh-task-panel-local',
8
+ factory(require) {
9
+ const React = require('react')
10
+ const h = React.createElement
11
+ const { useState, useEffect } = React
12
+
13
+ const C = {
14
+ text: '#1c2430', dim: '#5d6878', line: '#e1e6ee',
15
+ bg: '#ffffff', card: '#f7f9fc', accent: '#2f6fed', ok: '#12876b', warn: '#b46a00',
16
+ }
17
+ const S = {
18
+ wrap: { padding: '14px 16px', fontSize: 13, color: C.text, display: 'flex', flexDirection: 'column', gap: 12, height: '100%', boxSizing: 'border-box', overflowY: 'auto', overflowX: 'hidden', background: C.bg },
19
+ row: { display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' },
20
+ dim: { color: C.dim, fontSize: 12, lineHeight: 1.85 },
21
+ btn: { border: '1px solid ' + C.line, borderRadius: 7, background: '#fff', color: C.text, fontSize: 12, padding: '3px 9px', cursor: 'pointer' },
22
+ btnTiny: { border: '1px solid ' + C.line, borderRadius: 6, background: '#fff', color: C.dim, fontSize: 11, padding: '1px 7px', cursor: 'pointer' },
23
+ input: { border: '1px solid ' + C.line, borderRadius: 7, padding: '3px 8px', fontSize: 12, color: C.text, minWidth: 320 },
24
+ table: { width: '100%', borderCollapse: 'collapse', fontSize: 12 },
25
+ th: { textAlign: 'left', color: C.dim, fontWeight: 400, padding: '6px 8px', borderBottom: '1px solid ' + C.line },
26
+ td: { padding: '10px 10px', borderBottom: '1px solid ' + C.line, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
27
+ card: { border: '1px solid ' + C.line, borderRadius: 10, padding: '10px 12px', background: C.card, minWidth: 0, overflow: 'hidden', flexShrink: 0 },
28
+ chip: { fontSize: 11, padding: '1px 7px', borderRadius: 9, border: '1px solid ' + C.line, background: '#fff', color: C.dim },
29
+ chipOk: { fontSize: 11, padding: '1px 7px', borderRadius: 9, border: '1px solid #bfe3d6', background: '#eef8f4', color: C.ok },
30
+ chipWarn: { fontSize: 11, padding: '1px 7px', borderRadius: 9, border: '1px solid #f0d9b3', background: '#fdf6ea', color: C.warn },
31
+ pre: { margin: 0, padding: '8px 10px', border: '1px solid ' + C.line, borderRadius: 8, background: '#0f141b', color: '#d6e2f0', fontSize: 11, lineHeight: 1.5, maxHeight: 260, overflow: 'auto', whiteSpace: 'pre-wrap', wordBreak: 'break-all' },
32
+ }
33
+
34
+ // 表里只给「月-日 时:分」,宽一点也不用挤后面的列
35
+ const fmtShort = (s) => {
36
+ if (!s) return '—'
37
+ const d = new Date(s)
38
+ if (Number.isNaN(d.getTime())) return String(s)
39
+ const p = (n) => String(n).padStart(2, '0')
40
+ return p(d.getMonth() + 1) + '-' + p(d.getDate()) + ' ' + p(d.getHours()) + ':' + p(d.getMinutes())
41
+ }
42
+
43
+ const fmtTime = (s) => {
44
+ if (!s) return '—'
45
+ const d = new Date(s)
46
+ if (Number.isNaN(d.getTime())) return String(s)
47
+ const p = (n) => String(n).padStart(2, '0')
48
+ return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate()) + ' ' + p(d.getHours()) + ':' + p(d.getMinutes())
49
+ }
50
+ const fmtSize = (n) => (n >= 1 << 20 ? (n / (1 << 20)).toFixed(1) + ' MB' : n >= 1024 ? Math.round(n / 1024) + ' KB' : n + ' B')
51
+ const stateChip = (state) => {
52
+ const s = String(state || '')
53
+ if (s === 'Ready') return h('span', { style: S.chipOk }, '就绪')
54
+ if (s === 'Running') return h('span', { style: S.chipOk }, '运行中')
55
+ if (s === 'Disabled') return h('span', { style: S.chipWarn }, '已停用')
56
+ return h('span', { style: S.chip }, s || '—')
57
+ }
58
+ // 计划任务的结果码:成功是 0,其余按十六进制翻成人话(0x4130x 是状态码,0x8004xxxx 是说明,别的多半是错误)
59
+ const KNOWN_CODES = {
60
+ 0x0: '成功',
61
+ 0x41300: '已就绪(还没轮到)',
62
+ 0x41301: '正在运行',
63
+ 0x41302: '已禁用',
64
+ 0x41303: '还没跑过',
65
+ 0x80041324: '还没跑过(等第一次触发)',
66
+ 0x8004131f: '已经在跑了,这次没启动',
67
+ 0x80070002: '找不到程序或文件',
68
+ 0x80070005: '拒绝访问',
69
+ 0x8007010b: '工作目录无效',
70
+ 0x1: '一般错误(去看日志)',
71
+ 0x2: '找不到文件',
72
+ 0xa: '环境不对(多为路径/权限)',
73
+ }
74
+ const CODE_INFO = [0x0, 0x41300, 0x41301, 0x41302, 0x41303, 0x80041324, 0x8004131f]
75
+ const hexOf = (r) => '0x' + (r >>> 0).toString(16).toUpperCase()
76
+ const copyText = (text) => {
77
+ try {
78
+ if (navigator.clipboard && navigator.clipboard.writeText) {
79
+ navigator.clipboard.writeText(text)
80
+ return
81
+ }
82
+ } catch {}
83
+ try {
84
+ const el = document.createElement('textarea')
85
+ el.value = text
86
+ document.body.appendChild(el)
87
+ el.select()
88
+ document.execCommand('copy')
89
+ document.body.removeChild(el)
90
+ } catch {}
91
+ }
92
+ const resultText = (r) => {
93
+ if (r === null || r === undefined) return '—'
94
+ const u = r >>> 0
95
+ const bad = isFail(r)
96
+ return h('span', { style: { whiteSpace: 'nowrap' } },
97
+ h('span', { style: { fontFamily: 'Consolas, monospace' } }, hexOf(r)),
98
+ h('span', null, ' · ' + (KNOWN_CODES[u] || '')),
99
+ bad
100
+ ? h('button', {
101
+ style: { ...S.btnTiny, marginLeft: 6, padding: '0 5px', fontSize: 10, lineHeight: '14px' },
102
+ onClick: () => copyText(hexOf(r)),
103
+ title: '复制这个错误码',
104
+ }, '复制')
105
+ : null,
106
+ )
107
+ }
108
+ const isFail = (r) => {
109
+ if (typeof r !== 'number') return false
110
+ const u = r >>> 0
111
+ return u !== 0 && CODE_INFO.indexOf(u) === -1
112
+ }
113
+
114
+ const TABS = [
115
+ { key: 'localTasks', label: '本机计划任务' },
116
+ { key: 'recentTasks', label: '最近计划任务' },
117
+ { key: 'localScripts', label: '本机脚本' },
118
+ { key: 'recentScripts', label: '最近脚本' },
119
+ ]
120
+
121
+ function Panel() {
122
+ const [data, setData] = useState(null)
123
+ const [err, setErr] = useState('')
124
+ const [busy, setBusy] = useState(false)
125
+ const [note, setNote] = useState('')
126
+ const [logView, setLogView] = useState(null)
127
+ const [dirDraft, setDirDraft] = useState('')
128
+ const [newDir, setNewDir] = useState('')
129
+ const [taskScope, setTaskScope] = useState('mine')
130
+ const [scopeTouched, setScopeTouched] = useState(false)
131
+ const [tab, setTab] = useState('localTasks')
132
+ const [confirm, setConfirm] = useState('')
133
+ const [failAck, setFailAck] = useState('')
134
+ const [runFail, setRunFail] = useState(null)
135
+
136
+ const load = async () => {
137
+ setBusy(true)
138
+ try {
139
+ const r = await fetch('/dsh-task-panel.json', { cache: 'no-store' })
140
+ const j = await r.json()
141
+ setData(j)
142
+ setErr(j && j.ok ? '' : '读取失败')
143
+ if (!scopeTouched && j && (j.taskScope === 'all' || j.taskScope === 'mine')) setTaskScope(j.taskScope)
144
+ setFailAck(j && typeof j.failAck === 'string' ? j.failAck : '')
145
+ if (j && !dirDraft) {
146
+ const ds = Array.isArray(j.scriptDirs) && j.scriptDirs.length ? j.scriptDirs : j.scriptDir ? [j.scriptDir] : []
147
+ if (ds.length) setDirDraft(ds.join('\n'))
148
+ }
149
+ } catch (e) {
150
+ setErr(String((e && e.message) || e))
151
+ } finally {
152
+ setBusy(false)
153
+ }
154
+ }
155
+ // 十秒静默拉一次:只更新数据,不动按钮的忙闲,也不覆盖正在填的输入框
156
+ useEffect(() => {
157
+ if (!note) return undefined
158
+ const t = setTimeout(() => setNote(''), 9000)
159
+ return () => clearTimeout(t)
160
+ }, [note])
161
+
162
+ const refreshQuietly = async () => {
163
+ try {
164
+ const r = await fetch('/dsh-task-panel.json', { cache: 'no-store' })
165
+ const j = await r.json()
166
+ setData(j)
167
+ if (j && j.ok) setErr('')
168
+ } catch {}
169
+ }
170
+ useEffect(() => {
171
+ const t = setInterval(refreshQuietly, 10000)
172
+ return () => clearInterval(t)
173
+ }, [])
174
+ useEffect(() => {
175
+ load()
176
+ }, [])
177
+ // 日志浮层开着时,Esc 只关浮层,不冒到设置面板把整页关掉
178
+ useEffect(() => {
179
+ if (!logView) return
180
+ const onKey = (e) => {
181
+ if (e.key === 'Escape' || e.key === 'Esc') {
182
+ e.preventDefault()
183
+ e.stopPropagation()
184
+ e.stopImmediatePropagation()
185
+ setLogView(null)
186
+ }
187
+ }
188
+ window.addEventListener('keydown', onKey, true)
189
+ return () => window.removeEventListener('keydown', onKey, true)
190
+ }, [logView])
191
+
192
+ const post = async (body) => {
193
+ try {
194
+ const r = await fetch('/dsh-task-panel.run', {
195
+ method: 'POST',
196
+ headers: { 'content-type': 'application/json' },
197
+ body: JSON.stringify(body),
198
+ })
199
+ const j = await r.json()
200
+ const okRun = !!(j && j.ok)
201
+ const why = String((j && j.error) || '未知原因')
202
+ setNote(okRun ? (j.note || '好了') : '没成:' + why)
203
+ setRunFail(okRun ? null : { name: (body && (body.name || body.entry)) || '这一项', error: why, at: Date.now() })
204
+ if (okRun) {
205
+ // 手动跑过之后,失败卡的「已读」要作废,否则跑了也看不出结果
206
+ if (body.kind === 'task') {
207
+ setFailAck('')
208
+ fetch('/dsh-task-panel.config', {
209
+ method: 'POST',
210
+ headers: { 'content-type': 'application/json' },
211
+ body: JSON.stringify({ failAck: '' }),
212
+ }).catch(() => {})
213
+ }
214
+ // 计划任务要跑一会儿才有结果,多收几次
215
+ setTimeout(load, 1500)
216
+ setTimeout(load, 4000)
217
+ setTimeout(load, 10000)
218
+ }
219
+ } catch (e) {
220
+ setNote('没成:' + String((e && e.message) || e))
221
+ }
222
+ }
223
+
224
+ const saveConfig = async (patch) => {
225
+ try {
226
+ const r = await fetch('/dsh-task-panel.config', {
227
+ method: 'POST',
228
+ headers: { 'content-type': 'application/json' },
229
+ body: JSON.stringify(patch),
230
+ })
231
+ const j = await r.json()
232
+ if (j && j.ok) {
233
+ setNote('存好了')
234
+ load()
235
+ } else {
236
+ setNote('存不进:' + String((j && j.error) || '未知'))
237
+ }
238
+ } catch (e) {
239
+ setNote('存不进:' + String((e && e.message) || e))
240
+ }
241
+ }
242
+
243
+ // 跑一次要按两下:第一下变成「确定跑?」,四秒内不按就退回原样
244
+ const ask = (key, label, action) => {
245
+ const on = confirm === key
246
+ return h('button', {
247
+ style: on
248
+ ? { ...S.btnTiny, borderColor: '#c0392b', color: '#c0392b', fontWeight: 600 }
249
+ : S.btnTiny,
250
+ title: on ? '再点一下真的跑' : '点一下要先确认',
251
+ onClick: () => {
252
+ if (on) {
253
+ setConfirm('')
254
+ action()
255
+ } else {
256
+ setConfirm(key)
257
+ setTimeout(() => setConfirm((k) => (k === key ? '' : k)), 4000)
258
+ }
259
+ },
260
+ }, on ? '确定跑?' : label)
261
+ }
262
+
263
+ const showLog = async (file, dir) => {
264
+ try {
265
+ const url = '/dsh-task-panel.log?file=' + encodeURIComponent(file) + '&lines=200' + (dir ? '&dir=' + encodeURIComponent(dir) : '')
266
+ const r = await fetch(url, { cache: 'no-store' })
267
+ const j = await r.json()
268
+ if (j && j.ok) setLogView({ file, text: j.text })
269
+ else setNote('看不了日志:' + String((j && j.error) || '未知'))
270
+ } catch (e) {
271
+ setNote('看不了日志:' + String((e && e.message) || e))
272
+ }
273
+ }
274
+
275
+ const ts = (x) => { const n = new Date(x || 0).getTime(); return Number.isNaN(n) ? 0 : n }
276
+ const recent = ((data && data.recent) || []).slice().sort((a, b) => ts(b.at) - ts(a.at))
277
+ const recentCard = h('div', { style: S.card },
278
+ h('div', { style: { ...S.dim, marginBottom: 6 } }, '最近跑过的脚本'),
279
+ recent.length
280
+ ? h('table', { style: { ...S.table, tableLayout: 'fixed' } },
281
+ h('thead', null, h('tr', null,
282
+ h('th', { style: { ...S.th, width: 112 } }, '时间'),
283
+ h('th', { style: { ...S.th, width: 110 } }, '脚本'),
284
+ h('th', { style: { ...S.th, width: '45%' } }, '上次输出'),
285
+ h('th', { style: { ...S.th, width: 132 } }, '操作'),
286
+ )),
287
+ h('tbody', null, recent.map((r) =>
288
+ h('tr', { key: r.log },
289
+ h('td', { style: { ...S.td, width: 112, color: C.dim, whiteSpace: 'nowrap' } }, fmtShort(r.at)),
290
+ h('td', { style: { ...S.td, width: 110 }, title: r.script }, r.script),
291
+ h('td', { style: { ...S.td, width: '45%', color: C.dim }, title: r.lastLine || '' }, r.lastLine || '(没有输出)'),
292
+ h('td', { style: { ...S.td, width: 132 } },
293
+ ask('recent|' + (r.dir || '') + '|' + r.script, '跑一次', () => post({ kind: 'script', name: r.script, dir: r.dir })),
294
+ ' ',
295
+ h('button', { style: S.btnTiny, onClick: () => showLog(r.log, r.dir) }, '看日志'),
296
+ ),
297
+ ))),
298
+ )
299
+ : h('div', { style: S.dim }, '还没有跑过任何脚本。'),
300
+ )
301
+
302
+ // 最近跑过的任务:计划任务自己的上次运行 + 我手动点过的(成功的、被拒的都算)
303
+ const recentTasks = (() => {
304
+ const map = new Map()
305
+ for (const t of (data && data.recentTasks) || []) {
306
+ map.set(t.name, { name: t.name, at: t.last, result: t.result, mine: t.mine, manual: null })
307
+ }
308
+ for (const r of (data && data.manualRuns) || []) {
309
+ if (r.kind !== 'task') continue
310
+ const cur = map.get(r.name)
311
+ if (cur && ts(r.at) < ts(cur.at)) continue
312
+ map.set(r.name, { name: r.name, at: r.at, result: r.ok ? (cur ? cur.result : 0) : null, mine: cur ? cur.mine : /^dsh/i.test(r.name), manual: r })
313
+ }
314
+ return [...map.values()].filter((x) => taskScope === 'all' || x.mine).sort((a, b) => ts(b.at) - ts(a.at)).slice(0, 8)
315
+ })()
316
+ const recentTaskCard = h('div', { style: S.card },
317
+ h('div', { style: { ...S.dim, marginBottom: 6 } }, taskScope === 'mine' ? '最近跑过的计划任务(只看 DSH)' : '最近跑过的计划任务(全部)'),
318
+ recentTasks.length
319
+ ? h('table', { style: { ...S.table, tableLayout: 'fixed' } },
320
+ h('thead', null, h('tr', null,
321
+ h('th', { style: { ...S.th, width: 112 } }, '时间'),
322
+ h('th', { style: { ...S.th, width: '38%' } }, '任务'),
323
+ h('th', { style: { ...S.th, width: 160 } }, '上次结果'),
324
+ h('th', { style: { ...S.th, width: 96 } }, '操作'),
325
+ )),
326
+ h('tbody', null, recentTasks.map((r) =>
327
+ h('tr', { key: r.name },
328
+ h('td', { style: { ...S.td, width: 112, color: C.dim, whiteSpace: 'nowrap' } }, fmtShort(r.at)),
329
+ h('td', { style: { ...S.td, width: '38%' }, title: r.name }, r.name),
330
+ h('td', { style: { ...S.td, width: 160 } },
331
+ r.manual && !r.manual.ok
332
+ ? h('span', { style: { color: '#c0392b', fontWeight: 600 }, title: r.manual.error || '' }, '手动跑失败')
333
+ : h('span', null, r.manual ? '手动 · ' : '', resultText(r.result)),
334
+ ),
335
+ h('td', { style: { ...S.td, width: 96 } },
336
+ ask('task|' + r.name, '立即跑一次', () => post({ kind: 'task', name: r.name })),
337
+ ),
338
+ ))),
339
+ )
340
+ : h('div', { style: S.dim }, '还没有跑过的任务。'),
341
+ )
342
+
343
+ const allTasks = ((data && data.tasks) || []).slice().sort((a, b) => ts(b.last) - ts(a.last) || String(a.name).localeCompare(String(b.name)))
344
+ const mineTasks = allTasks.filter((t) => t.mine)
345
+ const tasks = taskScope === 'mine' ? mineTasks : allTasks
346
+ const scripts = (data && data.scripts) || []
347
+ const problems = (data && data.errors) || []
348
+ const failed = allTasks.filter((t) => isFail(t.result))
349
+
350
+ // 「没跑成功」统一一张卡:自动跑失败的 + 我手动跑失败的,同一套按钮
351
+ const ackedNames = String(failAck || '').split('|').map((s) => s.trim()).filter(Boolean)
352
+ const failRows = []
353
+ for (const t of failed) {
354
+ if (ackedNames.indexOf(t.name) >= 0) continue
355
+ failRows.push({ key: 'auto|' + t.name, kind: 'auto', name: t.name, at: t.last, code: t.result, error: '' })
356
+ }
357
+ if (runFail) failRows.push({ key: 'manual|' + runFail.name, kind: 'manual', name: runFail.name, at: runFail.at, code: null, error: runFail.error })
358
+
359
+ const ackOne = (row) => {
360
+ if (row.kind === 'manual') { setRunFail(null); return }
361
+ const next = ackedNames.concat([row.name]).join('|')
362
+ setFailAck(next)
363
+ saveConfig({ failAck: next })
364
+ }
365
+
366
+ const failPanel = failRows.length
367
+ ? h('div', { style: { ...S.card, borderColor: '#f0c4c4', background: '#fffafa' } },
368
+ h('div', { style: { ...S.dim, marginBottom: 6, color: '#c0392b' } }, '有 ' + failRows.length + ' 条没跑成功'),
369
+ h('div', { style: { display: 'flex', flexDirection: 'column', gap: 8 } },
370
+ failRows.map((r) =>
371
+ h('div', { key: r.key, style: { display: 'flex', gap: 10, alignItems: 'center', fontSize: 12 } },
372
+ h('span', { style: { color: '#c0392b', fontWeight: 600, whiteSpace: 'nowrap', minWidth: 92 } }, r.code == null ? '手动跑失败' : resultText(r.code)),
373
+ h('span', { style: { flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }, title: r.name }, r.name),
374
+ h('span', { style: { color: C.dim, whiteSpace: 'nowrap' } }, fmtShort(r.at)),
375
+ h('button', { style: S.btnTiny, onClick: () => { copyText(r.error || (r.code == null ? '' : String(r.code))); setNote('复制好了') } }, '复制'),
376
+ h('button', { style: S.btnTiny, onClick: () => ackOne(r) }, '我知道了'),
377
+ ask('rerun|' + r.name, '跑一遍', () => { setRunFail(null); post({ kind: 'task', name: r.name }) }),
378
+ ),
379
+ ),
380
+ ),
381
+ )
382
+ : null
383
+ const prefix = String((data && data.prefix) || '')
384
+ const taskTable = tasks.length
385
+ ? h('table', { style: { ...S.table, tableLayout: 'fixed' } },
386
+ h('thead', null, h('tr', null,
387
+ h('th', { style: { ...S.th, width: '38%' } }, '任务'),
388
+ h('th', { style: { ...S.th, width: 58 } }, '状态'),
389
+ h('th', { style: { ...S.th, width: 200 } }, '上次 / 下次'),
390
+ h('th', { style: { ...S.th, width: 92 } }, ''),
391
+ )),
392
+ h('tbody', null, tasks.map((t) =>
393
+ h('tr', { key: t.name, style: isFail(t.result) ? { background: '#fff4f4' } : undefined },
394
+ h('td', { style: S.td, title: t.path + t.name }, t.name),
395
+ h('td', { style: S.td }, stateChip(t.state)),
396
+ h('td', { style: { ...S.td, width: 200, whiteSpace: 'normal', lineHeight: 1.6 } },
397
+ h('div', { style: { whiteSpace: 'nowrap' } }, '上次 ' + fmtShort(t.last) + ' · ',
398
+ h('span', { style: isFail(t.result) ? { color: '#c0392b', fontWeight: 600 } : undefined }, resultText(t.result))),
399
+ h('div', { style: { color: C.dim, whiteSpace: 'nowrap' } }, '下次 ' + fmtShort(t.next)),
400
+ ),
401
+ h('td', { style: S.td },
402
+ ask('task|' + t.name, '立即跑一次', () => post({ kind: 'task', name: t.name })),
403
+ ),
404
+ ))),
405
+ )
406
+ : h('div', { style: S.dim },
407
+ allTasks.length && taskScope === 'mine'
408
+ ? '这台机器上没有名字带 DSH 的计划任务。点上面「全部」能看到另外 ' + allTasks.length + ' 个。'
409
+ : '这台机器上看不到任何计划任务(可能没有权限)。')
410
+
411
+ const scriptDirs = (data && (Array.isArray(data.scriptDirs) ? data.scriptDirs : data.scriptDir ? [data.scriptDir] : [])) || []
412
+ const multiDir = scriptDirs.length > 1
413
+ const showLogOf = (s) => showLog(s.lastLog, s.dir)
414
+ const scriptTable = scripts.length
415
+ ? h('table', { style: { ...S.table, tableLayout: 'fixed' } },
416
+ h('thead', null, h('tr', null,
417
+ h('th', { style: S.th }, '脚本'),
418
+ multiDir ? h('th', { style: { ...S.th, width: 110 } }, '目录') : null,
419
+ h('th', { style: { ...S.th, width: 56 } }, '大小'),
420
+ h('th', { style: { ...S.th, width: 112 } }, '修改时间'),
421
+ h('th', { style: { ...S.th, width: 190 } }, ''),
422
+ )),
423
+ h('tbody', null, scripts.map((s) =>
424
+ h('tr', { key: (s.dir || '') + '|' + s.name },
425
+ h('td', { style: S.td, title: s.name }, s.name),
426
+ multiDir ? h('td', { style: { ...S.td, width: 110, color: C.dim }, title: s.dir || '' }, s.dirLabel || '') : null,
427
+ h('td', { style: S.td }, fmtSize(s.size)),
428
+ h('td', { style: S.td }, fmtTime(s.mtime)),
429
+ h('td', { style: S.td },
430
+ ask('script|' + (s.dir || '') + '|' + s.name, '立即跑一次', () => post({ kind: 'script', name: s.name, dir: s.dir })),
431
+ ' ',
432
+ h('button', { style: { ...S.btnTiny, opacity: s.lastLog ? 1 : 0.45 }, onClick: () => s.lastLog && showLogOf(s), title: s.lastLog ? '看最近一次日志' : '还没有日志,先跑一次' }, '看日志'),
433
+ ),
434
+ ))),
435
+ )
436
+ : h('div', { style: S.dim }, '这些目录里没有 .ps1 / .py / .mjs / .cmd / .bat 脚本。脚本在别的文件夹?把路径贴到下面「新增目录」。')
437
+
438
+ const needConfig = !!(data && data.needConfig)
439
+ const autoDirs = !!(data && data.autoDirs)
440
+ const configCard = h('div', { style: { ...S.card, borderColor: needConfig ? '#f0d9b3' : C.line, background: needConfig ? '#fdf9f1' : C.card } },
441
+ h('div', { style: { ...S.dim, marginBottom: 6 } }, needConfig ? '还没加脚本目录' : autoDirs ? '配置 · 自动找到的目录' : '配置'),
442
+ h('div', { style: S.row },
443
+ h('span', { style: S.dim }, '脚本目录'),
444
+ h('textarea', {
445
+ style: { ...S.input, width: 280, minWidth: 280, minHeight: 44, resize: 'vertical', lineHeight: 1.5, fontFamily: 'inherit' },
446
+ value: dirDraft,
447
+ placeholder: '一行一个目录,比如\nD:\\scripts\nC:\\tools\\scripts',
448
+ onChange: (e) => setDirDraft(e.target.value),
449
+ }),
450
+ h('button', { style: S.btn, onClick: () => saveConfig({ scriptDirs: dirDraft.split(/[\r\n;]+/).map((s) => s.trim()).filter(Boolean) }) }, '保存'),
451
+ h('button', { style: S.btn, onClick: () => { setDirDraft(''); saveConfig({ scriptDirs: [] }) } }, '清空'),
452
+ ),
453
+ autoDirs ? h('div', { style: { ...S.dim, marginTop: 2, fontSize: 12 } }, '这是自动找到的目录(DSH 的 toolkit / scripts)。想固定成你自己的路径,改完点「保存」。') : null,
454
+ h('div', { style: { ...S.row, marginTop: 8 } },
455
+ h('span', { style: S.dim }, '新增目录'),
456
+ h('input', {
457
+ style: { ...S.input, width: 280, minWidth: 280 },
458
+ value: newDir,
459
+ placeholder: '粘贴一个文件夹的完整路径,比如 C:\\tools\\scripts',
460
+ onChange: (e) => setNewDir(e.target.value),
461
+ }),
462
+ h('button', {
463
+ style: S.btn,
464
+ onClick: () => {
465
+ const add = String(newDir || '').trim().replace(/[\\/]+$/, '')
466
+ if (!add) { setNote('先把文件夹路径贴上'); return }
467
+ const cur = dirDraft.split(/[\r\n;]+/).map((s) => s.trim()).filter(Boolean)
468
+ if (cur.some((d) => d.toLowerCase() === add.toLowerCase())) { setNote('这个目录已经在列表里了'); return }
469
+ const next = cur.concat([add])
470
+ setDirDraft(next.join('\n'))
471
+ setNewDir('')
472
+ saveConfig({ scriptDirs: next })
473
+ },
474
+ }, '加进列表'),
475
+ ),
476
+ h('div', { style: { ...S.row, marginTop: 8 } },
477
+ h('span', { style: S.dim }, '计划任务显示'),
478
+ h('button', {
479
+ style: { ...S.btnTiny, borderColor: taskScope === 'mine' ? C.accent : C.line, color: taskScope === 'mine' ? C.accent : C.dim, fontWeight: taskScope === 'mine' ? 600 : 400 },
480
+ onClick: () => { setScopeTouched(true); setTaskScope('mine'); saveConfig({ taskScope: 'mine' }) },
481
+ }, '只看 DSH(' + mineTasks.length + ')'),
482
+ h('button', {
483
+ style: { ...S.btnTiny, borderColor: taskScope === 'all' ? C.accent : C.line, color: taskScope === 'all' ? C.accent : C.dim, fontWeight: taskScope === 'all' ? 600 : 400 },
484
+ onClick: () => { setScopeTouched(true); setTaskScope('all'); saveConfig({ taskScope: 'all' }) },
485
+ }, '全部(' + allTasks.length + ')'),
486
+ ),
487
+ h('div', { style: { ...S.dim, marginTop: 8 } },
488
+ '这些设置存在 ' + String((data && data.configFile) || '~/.dsh/task-panel.config.json') + ' 里,下次打开还在。面板只读这些目录,不搬脚本;跑完的输出写在各自目录下的 logs\\ 里。'),
489
+ )
490
+
491
+ return h('div', { style: S.wrap },
492
+ h('div', { style: S.row },
493
+ h('div', { style: { fontWeight: 600 } }, '脚本与计划'),
494
+ h('span', { style: S.dim }, '点「立即跑一次」=现在手动跑一遍'),
495
+ h('div', { style: { flex: 1 } }),
496
+ h('button', { style: S.btn, onClick: load, disabled: busy }, busy ? '读取中…' : '刷新'),
497
+ ),
498
+ h('div', { style: S.dim },
499
+ data
500
+ ? '计划任务 ' + tasks.length + ' 个(全机共 ' + allTasks.length + ') · 脚本 ' + scripts.length + ' 个 | ' + ((data.scriptDirs && data.scriptDirs.length) ? (data.scriptDirs.length === 1 ? String(data.scriptDirs[0]) : data.scriptDirs.length + ' 个脚本目录') : '还没加脚本目录')
501
+ : '还没读到数据'),
502
+ h('div', { style: { ...S.dim, marginTop: 0 } },
503
+ '计划任务默认只看我自己的(名字带 Dsh);点下面「全部」能看本机所有任务。脚本只列「配置」里那点目录。'),
504
+ note ? h('div', { style: { fontSize: 12, color: C.accent } }, note) : null,
505
+
506
+
507
+ h('div', { style: { ...S.dim, marginTop: 0 } },
508
+ '启停与删除不在这个面板里——要真的停掉或删掉,请在系统「任务计划程序」里做(这里所有按钮都只读、只跑一次)。'),
509
+ problems.length ? h('div', { style: { color: C.warn, fontSize: 12 } }, problems.join(';')) : null,
510
+ err ? h('div', { style: { color: '#c0392b', fontSize: 12 } }, '读不到:' + err) : null,
511
+ configCard,
512
+ failPanel,
513
+ h('div', { style: { ...S.row, gap: 6, borderBottom: '1px solid ' + C.line, paddingBottom: 0, flexShrink: 0 } },
514
+ ...TABS.map((t) => h('button', {
515
+ key: t.key,
516
+ style: {
517
+ border: 'none', borderBottom: '2px solid ' + (tab === t.key ? C.accent : 'transparent'),
518
+ background: 'transparent', color: tab === t.key ? C.accent : C.dim,
519
+ fontWeight: tab === t.key ? 600 : 400, fontSize: 12.5, padding: '4px 6px', cursor: 'pointer',
520
+ },
521
+ onClick: () => setTab(t.key),
522
+ }, t.label)),
523
+ ),
524
+ tab === 'recentScripts' ? recentCard : null,
525
+ tab === 'recentTasks' ? recentTaskCard : null,
526
+ tab === 'localScripts'
527
+ ? (scriptDirs.length
528
+ ? h('div', { style: S.card },
529
+ h('div', { style: { ...S.dim, marginBottom: 6 } },
530
+ scriptDirs.length === 1 ? '脚本(' + String(scriptDirs[0]) + ')' : '脚本(' + scriptDirs.length + ' 个目录,日志各写各的)'),
531
+ scriptTable,
532
+ )
533
+ : h('div', { style: S.card }, h('div', { style: S.dim }, '还没加脚本目录——在上面「配置」里贴一个路径,点「加进列表」。如果脚本放在 DSH 的 toolkit 或 scripts 目录里,会自动找到。')))
534
+ : null,
535
+ tab === 'localTasks'
536
+ ? h('div', { style: S.card },
537
+ h('div', { style: { ...S.dim, marginBottom: 6 } }, taskScope === 'mine' ? 'Windows 计划任务(只看名字带 DSH 的,不碰 \\Microsoft 下的系统任务)' : 'Windows 计划任务(全部,不碰 \\Microsoft 下的系统任务)'),
538
+ taskTable,
539
+ )
540
+ : null,
541
+ logView
542
+ ? h('div', { style: { position: 'fixed', inset: 0, zIndex: 9999, background: 'rgba(10,14,22,0.92)', display: 'flex', flexDirection: 'column', gap: 8, padding: '16px 18px', boxSizing: 'border-box' } },
543
+ h('div', { style: S.row },
544
+ h('div', { style: { flex: 1, color: '#d6e2f0', fontSize: 12 } }, '日志:' + logView.file + '(最后 200 行 · Esc 关掉)'),
545
+ h('button', { style: { ...S.btnTiny, color: '#d6e2f0', background: 'transparent', borderColor: '#3a4657' }, onClick: () => setLogView(null) }, '关闭'),
546
+ ),
547
+ h('pre', { style: { ...S.pre, flex: 1, maxHeight: 'none' } }, logView.text || '(空)'),
548
+ )
549
+ : null,
550
+ h('div', { style: { ...S.dim, marginTop: 2, lineHeight: 1.7 } }, '不启停、不删除:这两个动作误点代价大,要的话在命令行里做(停任务用 Stop-ScheduledTask,删脚本先挪进 archive\\ 再删)。'),
551
+ )
552
+ }
553
+
554
+ function Boundary(props) {
555
+ try {
556
+ return h(Panel, props)
557
+ } catch (e) {
558
+ return h('div', { style: { padding: 14, color: '#ff8080', fontSize: 12 } }, '脚本与计划出错了:' + String((e && e.message) || e))
559
+ }
560
+ }
561
+
562
+ return {
563
+ inject: ['slots'],
564
+ apply(ctx) {
565
+ const slots = (typeof ctx.get === 'function' ? ctx.get('slots') : undefined) || ctx.slots
566
+ if (!slots) return
567
+ slots.inject('settings.section', () => slots.register({ name: 'settings.section', id: 'dsh-task-panel-local', order: 69, label: '脚本与计划' }, () => h(Boundary)))
568
+ },
569
+ }
570
+ },
571
+ })
@@ -0,0 +1,3 @@
1
+ - insert:
2
+ - id: dsh-task-panel-local
3
+ name: dsh-task-panel-local