dsh-plugin-workbench 0.0.5 → 0.0.6
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/CHANGELOG.md +32 -1
- package/README.md +18 -5
- package/lib/client.js +6399 -155
- package/lib/client.js.map +1 -1
- package/lib/index.js +302 -5
- package/package.json +2 -1
- package/src/client/FileExplorer.tsx +537 -38
- package/src/client/FilePreview.tsx +182 -6
- package/src/client/files.module.css +279 -0
- package/src/client/highlight.ts +18 -1
- package/src/client/index.ts +23 -4
- package/src/client/locales.ts +48 -4
- package/src/client/markdown.ts +69 -0
- package/src/client/store.ts +132 -2
- package/src/index.ts +400 -7
package/src/index.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Registers one loopback-only generic RPC channel (`/dsh-plugin-files`) with
|
|
5
5
|
* list/read/write endpoints over `ctx.fs` (the sandboxed filesystem service),
|
|
6
|
-
* plus New File / New Folder / Rename / Delete for the explorer's
|
|
7
|
-
* menu. Reads pass through untouched in every sandbox mode; the context-menu
|
|
6
|
+
* plus New File / New Folder / Rename / Delete / Copy for the explorer's
|
|
7
|
+
* context menu and a `reveal` endpoint that shows an item in the OS file manager. Reads pass through untouched in every sandbox mode; the context-menu
|
|
8
8
|
* mutations and the editor write are explicit user actions over the
|
|
9
9
|
* loopback-only channel, run unfenced like the /api write tools — the sandbox
|
|
10
10
|
* service has no mkdir/rename/rm primitives, so those use node:fs/promises
|
|
@@ -16,9 +16,21 @@
|
|
|
16
16
|
* dsh-plugin-image-tools uses for inline chat images. The route resolves and
|
|
17
17
|
* stats the path through `ctx.fs` (so sandbox containment and file-ness apply
|
|
18
18
|
* exactly as for RPC reads) and only ever serves image extensions.
|
|
19
|
+
*
|
|
20
|
+
* Disk watching: the client keeps the host's watch set in sync with the open
|
|
21
|
+
* tabs via the `watch` endpoint. The host watches each file's PARENT DIRECTORY
|
|
22
|
+
* with `fs.watch` (survives atomic editor renames on Windows, unlike watching
|
|
23
|
+
* the file itself), coalesces events per path, and pushes `change` frames over
|
|
24
|
+
* a same-origin SSE route (`/dsh-plugin-files/events`) that the preview pane
|
|
25
|
+
* consumes. Writes this plugin performs itself are suppressed for a short
|
|
26
|
+
* window so a save never bounces back as a "changed on disk" event.
|
|
19
27
|
*/
|
|
20
28
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
21
|
-
import {
|
|
29
|
+
import { spawn } from 'node:child_process'
|
|
30
|
+
import { watch as watchFs } from 'node:fs'
|
|
31
|
+
import type { FSWatcher } from 'node:fs'
|
|
32
|
+
import { basename, dirname } from 'node:path'
|
|
33
|
+
import { mkdir, rename as renameFs, rm, cp, writeFile as writeFileNode } from 'node:fs/promises'
|
|
22
34
|
import type { Context } from '@deepseek-ai/cordis'
|
|
23
35
|
|
|
24
36
|
export const name = 'dsh-plugin-workbench'
|
|
@@ -33,9 +45,21 @@ export const MAX_PREVIEW_BYTES = 512 * 1024
|
|
|
33
45
|
/** Same-origin route serving raw bytes for image files (see module doc). */
|
|
34
46
|
export const RAW_PREFIX = '/dsh-plugin-files/raw'
|
|
35
47
|
|
|
48
|
+
/** Same-origin SSE route streaming disk-change events to the preview pane. */
|
|
49
|
+
export const EVENTS_PREFIX = '/dsh-plugin-files/events'
|
|
50
|
+
|
|
36
51
|
/** Images larger than this are never served to the preview (browser shows a hint). */
|
|
37
52
|
export const MAX_IMAGE_BYTES = 20 * 1024 * 1024
|
|
38
53
|
|
|
54
|
+
/** Coalesce bursty editor writes into a single change notification. */
|
|
55
|
+
const WATCH_DEBOUNCE_MS = 300
|
|
56
|
+
|
|
57
|
+
/** Ignore fs.watch events caused by this plugin's own saves (see module doc). */
|
|
58
|
+
const SELF_WRITE_WINDOW_MS = 1500
|
|
59
|
+
|
|
60
|
+
/** SSE keep-alive interval (proxies may otherwise drop idle connections). */
|
|
61
|
+
const SSE_HEARTBEAT_MS = 15000
|
|
62
|
+
|
|
39
63
|
const IMAGE_MIME: Record<string, string> = {
|
|
40
64
|
png: 'image/png',
|
|
41
65
|
jpg: 'image/jpeg',
|
|
@@ -93,7 +117,7 @@ export interface FsMutationResult {
|
|
|
93
117
|
}
|
|
94
118
|
|
|
95
119
|
export type FilesRpcOk = { ok: true; value: FsListResult | FsReadResult | FsWriteResult | FsMutationResult }
|
|
96
|
-
export type FilesRpcErr = { ok: false; error: { code:
|
|
120
|
+
export type FilesRpcErr = { ok: false; error: { code: string; message: string; details: Record<string, never> } }
|
|
97
121
|
export type FilesRpcResult = FilesRpcOk | FilesRpcErr
|
|
98
122
|
|
|
99
123
|
/**
|
|
@@ -188,14 +212,26 @@ function pathOf(payload: unknown): string | undefined {
|
|
|
188
212
|
* cancels the underlying fs call (or aborts between steps).
|
|
189
213
|
*/
|
|
190
214
|
export function apply(ctx: Context): void {
|
|
215
|
+
// Per-apply watch state: created here (not module-level) so disable/reload
|
|
216
|
+
// cycles never leak watchers or SSE clients across applies.
|
|
217
|
+
const watchState: WatchState = {
|
|
218
|
+
dirs: new Map(),
|
|
219
|
+
files: new Map(),
|
|
220
|
+
selfWrites: new Map(),
|
|
221
|
+
clients: new Set(),
|
|
222
|
+
}
|
|
223
|
+
|
|
191
224
|
const handler = async (endpoint: string, payload: unknown, signal: AbortSignal): Promise<FilesRpcResult> => {
|
|
192
225
|
if (endpoint === 'list') return listDir(ctx, payload, signal)
|
|
193
226
|
if (endpoint === 'read') return readFile(ctx, payload, signal)
|
|
194
|
-
if (endpoint === 'write') return writeFile(ctx, payload, signal)
|
|
227
|
+
if (endpoint === 'write') return writeFile(ctx, watchState, payload, signal)
|
|
228
|
+
if (endpoint === 'watch') return setWatch(ctx, watchState, payload, signal)
|
|
195
229
|
if (endpoint === 'createFile') return createFile(ctx, payload, signal)
|
|
196
230
|
if (endpoint === 'createDir') return createDir(ctx, payload, signal)
|
|
197
231
|
if (endpoint === 'rename') return renameEntry(ctx, payload, signal)
|
|
198
232
|
if (endpoint === 'delete') return deleteEntry(ctx, payload, signal)
|
|
233
|
+
if (endpoint === 'copy') return copyEntry(ctx, payload, signal)
|
|
234
|
+
if (endpoint === 'reveal') return revealInExplorer(ctx, payload, signal)
|
|
199
235
|
return fail(`unknown endpoint: ${endpoint}`)
|
|
200
236
|
}
|
|
201
237
|
// Effect-wrapped so HMR/disable cycles dispose the channel (the connection
|
|
@@ -214,6 +250,214 @@ export function apply(ctx: Context): void {
|
|
|
214
250
|
void serveRaw(ctx, req, res)
|
|
215
251
|
},
|
|
216
252
|
}), 'dsh-plugin-workbench: raw image route')
|
|
253
|
+
|
|
254
|
+
// Disk-change stream: the SSE route + heartbeat + watcher lifecycle live in
|
|
255
|
+
// one effect so disposal closes every client and every fs.watch handle.
|
|
256
|
+
ctx.effect(() => {
|
|
257
|
+
const disposeRoute = ctx.webServer.register({
|
|
258
|
+
kind: 'exact',
|
|
259
|
+
path: EVENTS_PREFIX,
|
|
260
|
+
handler: (req, res) => sseHandler(watchState, req, res),
|
|
261
|
+
})
|
|
262
|
+
const heartbeat = setInterval(() => {
|
|
263
|
+
for (const res of watchState.clients) {
|
|
264
|
+
try {
|
|
265
|
+
res.write(': ping\n\n')
|
|
266
|
+
} catch {
|
|
267
|
+
// Dropped client — removed by its own 'close' event.
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}, SSE_HEARTBEAT_MS)
|
|
271
|
+
return () => {
|
|
272
|
+
clearInterval(heartbeat)
|
|
273
|
+
disposeRoute()
|
|
274
|
+
disposeWatch(watchState)
|
|
275
|
+
}
|
|
276
|
+
}, 'dsh-plugin-workbench: disk change stream (SSE)')
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ---------------------------------------------------------------------------
|
|
280
|
+
// Disk watching (open-tab change detection)
|
|
281
|
+
//
|
|
282
|
+
// The client sends the full set of open tab paths; the host diffs it against
|
|
283
|
+
// the current watcher set. Each watched file lives under an `fs.watch` on its
|
|
284
|
+
// PARENT DIRECTORY (file-level handles die when editors atomic-rename, and
|
|
285
|
+
// directory watching also sees deletes), events are filtered by basename,
|
|
286
|
+
// coalesced per path, and pushed over the SSE route. `watch` is a pure
|
|
287
|
+
// reconciliation — call it as often as you like.
|
|
288
|
+
// ---------------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
/** Per-apply watch registry (see the WatchState fields inline). */
|
|
291
|
+
interface WatchState {
|
|
292
|
+
/** Parent dir → its fs.watch handle and the watched basenames living in it. */
|
|
293
|
+
dirs: Map<string, { watcher: FSWatcher; basenames: Map<string, Set<string>> }>
|
|
294
|
+
/** OS path → client-facing path + pending coalescing timer. */
|
|
295
|
+
files: Map<string, { path: string; timer: ReturnType<typeof setTimeout> | undefined }>
|
|
296
|
+
/** OS path → timestamp of the plugin's own last write (self-change suppression). */
|
|
297
|
+
selfWrites: Map<string, number>
|
|
298
|
+
/** Connected SSE responses. */
|
|
299
|
+
clients: Set<ServerResponse>
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Serve one SSE client connection (kept open until the browser disconnects). */
|
|
303
|
+
function sseHandler(state: WatchState, req: IncomingMessage, res: ServerResponse): void {
|
|
304
|
+
if ((req.method ?? 'GET').toUpperCase() !== 'GET') {
|
|
305
|
+
res.writeHead(405, { 'content-type': 'text/plain; charset=utf-8' })
|
|
306
|
+
res.end('method not allowed')
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
res.writeHead(200, {
|
|
310
|
+
'content-type': 'text/event-stream',
|
|
311
|
+
'cache-control': 'no-cache',
|
|
312
|
+
connection: 'keep-alive',
|
|
313
|
+
'x-accel-buffering': 'no',
|
|
314
|
+
})
|
|
315
|
+
res.write(': connected\n\n')
|
|
316
|
+
state.clients.add(res)
|
|
317
|
+
req.on('close', () => {
|
|
318
|
+
state.clients.delete(res)
|
|
319
|
+
})
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Push one `change` frame to every connected SSE client. */
|
|
323
|
+
function emitChange(state: WatchState, path: string): void {
|
|
324
|
+
const frame = `event: change\ndata: ${JSON.stringify({ path })}\n\n`
|
|
325
|
+
for (const res of state.clients) {
|
|
326
|
+
try {
|
|
327
|
+
res.write(frame)
|
|
328
|
+
} catch {
|
|
329
|
+
// Client gone — dropped from the set by its 'close' event.
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Coalesce one filesystem event for an osPath into a single change
|
|
336
|
+
* notification (editors emit several events per save; the debounce collapses
|
|
337
|
+
* them). Events caused by this plugin's own saves are suppressed.
|
|
338
|
+
*/
|
|
339
|
+
function scheduleEmit(state: WatchState, osPath: string): void {
|
|
340
|
+
const entry = state.files.get(osPath)
|
|
341
|
+
if (entry === undefined) return
|
|
342
|
+
const selfTs = state.selfWrites.get(osPath)
|
|
343
|
+
if (selfTs !== undefined && Date.now() - selfTs < SELF_WRITE_WINDOW_MS) return
|
|
344
|
+
if (entry.timer !== undefined) clearTimeout(entry.timer)
|
|
345
|
+
entry.timer = setTimeout(() => {
|
|
346
|
+
entry.timer = undefined
|
|
347
|
+
const still = state.files.get(osPath)
|
|
348
|
+
if (still === undefined) return
|
|
349
|
+
emitChange(state, still.path)
|
|
350
|
+
}, WATCH_DEBOUNCE_MS)
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Start (or extend) the parent-dir watcher covering osPath. */
|
|
354
|
+
function watchDir(state: WatchState, dir: string, osPath: string): boolean {
|
|
355
|
+
let bucket = state.dirs.get(dir)
|
|
356
|
+
if (bucket === undefined) {
|
|
357
|
+
let watcher: FSWatcher
|
|
358
|
+
try {
|
|
359
|
+
watcher = watchFs(dir, { persistent: false }, (_eventType, filename) => {
|
|
360
|
+
const name = typeof filename === 'string' ? filename : undefined
|
|
361
|
+
if (name === undefined) {
|
|
362
|
+
// Platform omitted the filename: re-emit for every watched file here.
|
|
363
|
+
for (const os of state.files.keys()) {
|
|
364
|
+
if (dirname(os) === dir) scheduleEmit(state, os)
|
|
365
|
+
}
|
|
366
|
+
return
|
|
367
|
+
}
|
|
368
|
+
const bucketNow = state.dirs.get(dir)
|
|
369
|
+
const targets = bucketNow?.basenames.get(name)
|
|
370
|
+
if (targets === undefined) return
|
|
371
|
+
for (const os of targets) scheduleEmit(state, os)
|
|
372
|
+
})
|
|
373
|
+
} catch {
|
|
374
|
+
return false
|
|
375
|
+
}
|
|
376
|
+
bucket = { watcher, basenames: new Map() }
|
|
377
|
+
state.dirs.set(dir, bucket)
|
|
378
|
+
}
|
|
379
|
+
const name = basename(osPath)
|
|
380
|
+
let targets = bucket.basenames.get(name)
|
|
381
|
+
if (targets === undefined) {
|
|
382
|
+
targets = new Set()
|
|
383
|
+
bucket.basenames.set(name, targets)
|
|
384
|
+
}
|
|
385
|
+
targets.add(osPath)
|
|
386
|
+
return true
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** Stop watching one osPath (and its parent dir when nothing else uses it). */
|
|
390
|
+
function unwatch(state: WatchState, osPath: string): void {
|
|
391
|
+
const entry = state.files.get(osPath)
|
|
392
|
+
if (entry === undefined) return
|
|
393
|
+
if (entry.timer !== undefined) clearTimeout(entry.timer)
|
|
394
|
+
state.files.delete(osPath)
|
|
395
|
+
state.selfWrites.delete(osPath)
|
|
396
|
+
const dir = dirname(osPath)
|
|
397
|
+
const bucket = state.dirs.get(dir)
|
|
398
|
+
if (bucket === undefined) return
|
|
399
|
+
const name = basename(osPath)
|
|
400
|
+
const targets = bucket.basenames.get(name)
|
|
401
|
+
if (targets !== undefined) {
|
|
402
|
+
targets.delete(osPath)
|
|
403
|
+
if (targets.size === 0) bucket.basenames.delete(name)
|
|
404
|
+
}
|
|
405
|
+
if (bucket.basenames.size === 0) {
|
|
406
|
+
bucket.watcher.close()
|
|
407
|
+
state.dirs.delete(dir)
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Close every watcher, pending timer and SSE client (effect disposal). */
|
|
412
|
+
function disposeWatch(state: WatchState): void {
|
|
413
|
+
for (const bucket of state.dirs.values()) bucket.watcher.close()
|
|
414
|
+
state.dirs.clear()
|
|
415
|
+
for (const entry of state.files.values()) {
|
|
416
|
+
if (entry.timer !== undefined) clearTimeout(entry.timer)
|
|
417
|
+
}
|
|
418
|
+
state.files.clear()
|
|
419
|
+
state.selfWrites.clear()
|
|
420
|
+
for (const res of state.clients) {
|
|
421
|
+
try {
|
|
422
|
+
res.end()
|
|
423
|
+
} catch {
|
|
424
|
+
// Already closed.
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
state.clients.clear()
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** Reconcile the watcher set with the client's open tab paths (idempotent). */
|
|
431
|
+
async function setWatch(ctx: Context, state: WatchState, payload: unknown, signal: AbortSignal): Promise<FilesRpcResult> {
|
|
432
|
+
const raw = typeof payload === 'object' && payload !== null ? (payload as { paths?: unknown }).paths : undefined
|
|
433
|
+
if (!Array.isArray(raw) || raw.some((p) => typeof p !== 'string')) {
|
|
434
|
+
return fail('watch: payload.paths must be an array of strings')
|
|
435
|
+
}
|
|
436
|
+
// Resolve each requested path to its OS path. Unresolvable paths (deleted,
|
|
437
|
+
// sandboxed) are skipped; the next sync round retries them.
|
|
438
|
+
const wanted = new Map<string, string>()
|
|
439
|
+
for (const p of raw as string[]) {
|
|
440
|
+
if (signal.aborted) break
|
|
441
|
+
try {
|
|
442
|
+
const target = await ctx.fs.resolve(p, { signal })
|
|
443
|
+
const osPath = ctx.fs.processPath(target)
|
|
444
|
+
// Key by OS path, keep the client's display path for the emit.
|
|
445
|
+
wanted.set(osPath, p)
|
|
446
|
+
} catch {
|
|
447
|
+
// Not resolvable this round — drop it.
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
// Remove watchers no longer wanted.
|
|
451
|
+
for (const osPath of [...state.files.keys()]) {
|
|
452
|
+
if (!wanted.has(osPath)) unwatch(state, osPath)
|
|
453
|
+
}
|
|
454
|
+
// Start missing watchers.
|
|
455
|
+
for (const [osPath, displayPath] of wanted) {
|
|
456
|
+
if (state.files.has(osPath)) continue
|
|
457
|
+
if (!watchDir(state, dirname(osPath), osPath)) continue
|
|
458
|
+
state.files.set(osPath, { path: displayPath, timer: undefined })
|
|
459
|
+
}
|
|
460
|
+
return { ok: true, value: { path: '' } }
|
|
217
461
|
}
|
|
218
462
|
|
|
219
463
|
async function serveRaw(ctx: Context, req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
@@ -305,7 +549,7 @@ async function readFile(ctx: Context, payload: unknown, signal: AbortSignal): Pr
|
|
|
305
549
|
}
|
|
306
550
|
}
|
|
307
551
|
|
|
308
|
-
async function writeFile(ctx: Context, payload: unknown, signal: AbortSignal): Promise<FilesRpcResult> {
|
|
552
|
+
async function writeFile(ctx: Context, state: WatchState, payload: unknown, signal: AbortSignal): Promise<FilesRpcResult> {
|
|
309
553
|
const path = pathOf(payload)
|
|
310
554
|
const content = typeof payload === 'object' && payload !== null ? (payload as { content?: unknown }).content : undefined
|
|
311
555
|
if (path === undefined) return fail('write: payload.path must be a non-empty string')
|
|
@@ -318,7 +562,18 @@ async function writeFile(ctx: Context, payload: unknown, signal: AbortSignal): P
|
|
|
318
562
|
mode: 'danger-full-access',
|
|
319
563
|
workspaceRoot: ctx.fs.processPath(target),
|
|
320
564
|
})
|
|
321
|
-
|
|
565
|
+
// This save will trip the fs.watch on the file's parent dir; suppress it
|
|
566
|
+
// so an own save never bounces back as a "changed on disk" event.
|
|
567
|
+
const osPath = ctx.fs.processPath(target)
|
|
568
|
+
state.selfWrites.set(osPath, Date.now())
|
|
569
|
+
// Prune stale markers occasionally.
|
|
570
|
+
if (state.selfWrites.size > 64) {
|
|
571
|
+
const cutoff = Date.now() - SELF_WRITE_WINDOW_MS * 4
|
|
572
|
+
for (const [os, ts] of state.selfWrites) {
|
|
573
|
+
if (ts < cutoff) state.selfWrites.delete(os)
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
return { ok: true, value: { path: osPath, size: content.length } }
|
|
322
577
|
} catch (error) {
|
|
323
578
|
return fail(mapError(error))
|
|
324
579
|
}
|
|
@@ -399,3 +654,141 @@ async function deleteEntry(ctx: Context, payload: unknown, signal: AbortSignal):
|
|
|
399
654
|
return fail(mapError(error))
|
|
400
655
|
}
|
|
401
656
|
}
|
|
657
|
+
|
|
658
|
+
// ---------------------------------------------------------------------------
|
|
659
|
+
// Copy (the explorer's Copy / Cut + Paste)
|
|
660
|
+
//
|
|
661
|
+
// `fs.cp` handles files and (recursively) folders; `errorOnExist` turns a
|
|
662
|
+
// colliding destination into a distinct `exists` error so the client can ask
|
|
663
|
+
// the user whether to overwrite, exactly like the OS file manager. The same
|
|
664
|
+
// `ctx.fs.resolve` → `processPath` resolution as every other endpoint applies,
|
|
665
|
+
// so sandbox containment and error mapping stay uniform.
|
|
666
|
+
// ---------------------------------------------------------------------------
|
|
667
|
+
|
|
668
|
+
async function copyEntry(ctx: Context, payload: unknown, signal: AbortSignal): Promise<FilesRpcResult> {
|
|
669
|
+
// Note: the client sends `from`, not `path` (which is what pathOf reads).
|
|
670
|
+
const from = typeof payload === 'object' && payload !== null ? (payload as { from?: unknown }).from : undefined
|
|
671
|
+
const to = typeof payload === 'object' && payload !== null ? (payload as { to?: unknown }).to : undefined
|
|
672
|
+
const overwrite = typeof payload === 'object' && payload !== null && (payload as { overwrite?: unknown }).overwrite === true
|
|
673
|
+
if (typeof from !== 'string' || from.trim().length === 0) return fail('copy: payload.from must be a non-empty string')
|
|
674
|
+
if (typeof to !== 'string' || to.trim().length === 0) return fail('copy: payload.to must be a non-empty string')
|
|
675
|
+
try {
|
|
676
|
+
const fromTarget = await ctx.fs.resolve(from, { signal })
|
|
677
|
+
const fromOs = ctx.fs.processPath(fromTarget)
|
|
678
|
+
const info = await ctx.fs.stat(fromTarget, signal)
|
|
679
|
+
if (info === undefined) return fail(`path not found: ${from}`)
|
|
680
|
+
const toTarget = await ctx.fs.resolve(to, { signal })
|
|
681
|
+
const toOs = ctx.fs.processPath(toTarget)
|
|
682
|
+
// Refuse trivial self-copies and copying a folder into itself or one of
|
|
683
|
+
// its own descendants (comparisons case-insensitive — Windows).
|
|
684
|
+
const samePath = fromOs.toLowerCase() === toOs.toLowerCase()
|
|
685
|
+
if (samePath) return fail('copy: source and destination are the same path')
|
|
686
|
+
if (info.type === 'directory') {
|
|
687
|
+
const sep = fromOs.includes('\\') ? '\\' : '/'
|
|
688
|
+
const prefix = fromOs.endsWith('\\') || fromOs.endsWith('/') ? fromOs : fromOs + sep
|
|
689
|
+
if (toOs.toLowerCase().startsWith(prefix.toLowerCase())) return fail('copy: cannot copy a folder into itself')
|
|
690
|
+
}
|
|
691
|
+
await cp(fromOs, toOs, { recursive: true, force: overwrite, errorOnExist: !overwrite })
|
|
692
|
+
return { ok: true, value: { path: toOs } }
|
|
693
|
+
} catch (error) {
|
|
694
|
+
if (isFsErrorCode(error, 'ERR_FS_CP_EEXIST')) {
|
|
695
|
+
return { ok: false, error: { code: 'exists', message: 'destination already exists', details: {} } }
|
|
696
|
+
}
|
|
697
|
+
return fail(mapError(error))
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// ---------------------------------------------------------------------------
|
|
702
|
+
// Reveal in the OS file manager ("在资源管理器打开")
|
|
703
|
+
//
|
|
704
|
+
// Spawns the platform's file-manager command so the item shows up in the
|
|
705
|
+
// system explorer — files are SELECTED inside their containing folder
|
|
706
|
+
// (Windows `explorer /select,`, macOS `open -R`), folders are OPENED directly
|
|
707
|
+
// (`explorer <dir>`, `open <dir>`). WSL paths are translated through
|
|
708
|
+
// `wslpath` first; desktop Linux falls back to `xdg-open` (file → its parent
|
|
709
|
+
// directory, folder → itself). Paths are resolved through `ctx.fs` first, so
|
|
710
|
+
// the sandbox containment applies exactly as for every other endpoint; the
|
|
711
|
+
// spawn itself is a local desktop action, the same trust level as the existing
|
|
712
|
+
// "open in system" gesture (`ctx.workspaces.openPath`).
|
|
713
|
+
// ---------------------------------------------------------------------------
|
|
714
|
+
|
|
715
|
+
/** Spawn one short-lived desktop command; resolves once the process launched. */
|
|
716
|
+
function runDesktop(command: string, args: string[]): Promise<void> {
|
|
717
|
+
return new Promise((resolve, reject) => {
|
|
718
|
+
const child = spawn(command, args, { detached: true, stdio: 'ignore', windowsHide: true })
|
|
719
|
+
child.once('error', reject)
|
|
720
|
+
child.once('spawn', () => {
|
|
721
|
+
// The window stays after the harness exits; nothing to wait for.
|
|
722
|
+
child.unref()
|
|
723
|
+
resolve()
|
|
724
|
+
})
|
|
725
|
+
})
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/** Run one command and capture its stdout; rejects on non-zero exit. */
|
|
729
|
+
function execCapture(command: string, args: string[]): Promise<string> {
|
|
730
|
+
return new Promise((resolve, reject) => {
|
|
731
|
+
const child = spawn(command, args, { windowsHide: true })
|
|
732
|
+
let out = ''
|
|
733
|
+
child.stdout.on('data', (chunk: Buffer) => {
|
|
734
|
+
out += chunk.toString()
|
|
735
|
+
})
|
|
736
|
+
child.once('error', reject)
|
|
737
|
+
child.once('close', (code) => {
|
|
738
|
+
if (code === 0) resolve(out)
|
|
739
|
+
else reject(new Error(`${command} exited with code ${code}`))
|
|
740
|
+
})
|
|
741
|
+
})
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/** Reveal one resolved OS path in the platform's file manager. */
|
|
745
|
+
async function revealNative(osPath: string, isDir: boolean, signal: AbortSignal): Promise<void> {
|
|
746
|
+
signal.throwIfAborted()
|
|
747
|
+
const platform = process.platform
|
|
748
|
+
if (platform === 'win32') {
|
|
749
|
+
// explorer returns exit code 1 when it opens a NEW window, so exit codes
|
|
750
|
+
// carry no meaning; a clean spawn is success. Files are selected in their
|
|
751
|
+
// folder; folders are opened directly.
|
|
752
|
+
await runDesktop('explorer.exe', isDir ? [osPath] : ['/select,', osPath])
|
|
753
|
+
return
|
|
754
|
+
}
|
|
755
|
+
if (platform === 'darwin') {
|
|
756
|
+
// `open -R` reveals in Finder; plain `open` opens a folder.
|
|
757
|
+
await runDesktop('open', isDir ? [osPath] : ['-R', osPath])
|
|
758
|
+
return
|
|
759
|
+
}
|
|
760
|
+
if (platform === 'linux') {
|
|
761
|
+
// WSL: translate to a Windows path and hand it to the Windows desktop.
|
|
762
|
+
const env = process.env
|
|
763
|
+
if (env.WSL_DISTRO_NAME !== undefined || env.WSL_INTEROP !== undefined) {
|
|
764
|
+
const windowsPath = (await execCapture('wslpath', ['-w', osPath])).replace(/[\r\n]+$/, '')
|
|
765
|
+
if (windowsPath === '') throw new Error('wslpath returned no Windows path')
|
|
766
|
+
await runDesktop('explorer.exe', isDir ? [windowsPath] : ['/select,', windowsPath])
|
|
767
|
+
return
|
|
768
|
+
}
|
|
769
|
+
// Desktop Linux: the default file manager opens folders; files open in
|
|
770
|
+
// their parent directory.
|
|
771
|
+
await runDesktop('xdg-open', [isDir ? osPath : dirname(osPath)])
|
|
772
|
+
return
|
|
773
|
+
}
|
|
774
|
+
throw new Error(`reveal in the file manager is unsupported on ${platform}`)
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** RPC endpoint: reveal one explorer path in the OS file manager. */
|
|
778
|
+
async function revealInExplorer(ctx: Context, payload: unknown, signal: AbortSignal): Promise<FilesRpcResult> {
|
|
779
|
+
const path = pathOf(payload)
|
|
780
|
+
const kind = typeof payload === 'object' && payload !== null ? (payload as { kind?: unknown }).kind : undefined
|
|
781
|
+
if (path === undefined) return fail('reveal: payload.path must be a non-empty string')
|
|
782
|
+
try {
|
|
783
|
+
const target = await ctx.fs.resolve(path, { signal })
|
|
784
|
+
const info = await ctx.fs.stat(target, signal)
|
|
785
|
+
if (info === undefined) return fail(`path not found: ${path}`)
|
|
786
|
+
const isDir = kind === 'dir' || info.type === 'directory'
|
|
787
|
+
const osPath = ctx.fs.processPath(target)
|
|
788
|
+
await revealNative(osPath, isDir, signal)
|
|
789
|
+
return { ok: true, value: { path: osPath } }
|
|
790
|
+
} catch (error) {
|
|
791
|
+
if (signal.aborted) return fail('reveal: aborted')
|
|
792
|
+
return fail(mapError(error))
|
|
793
|
+
}
|
|
794
|
+
}
|