|
| 1 | +import type { DevToolsChildProcessTerminalOptions, DevToolsChildProcessTerminalSession, DevToolsNodeContext, DevToolsTerminalHost as DevToolsTerminalHostType, DevToolsTerminalSession, DevToolsTerminalSessionSerializable } from '@vitejs/devtools-kit' |
| 2 | +import type { Result as TinyExecResult } from 'tinyexec' |
| 3 | +import process from 'node:process' |
| 4 | + |
| 5 | +export class DevToolsTerminalHost implements DevToolsTerminalHostType { |
| 6 | + constructor( |
| 7 | + public readonly context: DevToolsNodeContext, |
| 8 | + ) { |
| 9 | + } |
| 10 | + |
| 11 | + readonly sessions: Map<string, DevToolsTerminalSession> = new Map() |
| 12 | + |
| 13 | + serialize(session: DevToolsTerminalSession): DevToolsTerminalSessionSerializable { |
| 14 | + return { |
| 15 | + id: session.id, |
| 16 | + title: session.title, |
| 17 | + description: session.description, |
| 18 | + status: session.status, |
| 19 | + buffer: session.buffer ?? [], |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + register(session: DevToolsTerminalSession): DevToolsTerminalSession { |
| 24 | + if (this.sessions.has(session.id)) { |
| 25 | + throw new Error(`Terminal session with id "${session.id}" already registered`) |
| 26 | + } |
| 27 | + this.sessions.set(session.id, session) |
| 28 | + return session |
| 29 | + } |
| 30 | + |
| 31 | + update(session: DevToolsTerminalSession): void { |
| 32 | + if (!this.sessions.has(session.id)) { |
| 33 | + throw new Error(`Terminal session with id "${session.id}" not registered`) |
| 34 | + } |
| 35 | + this.sessions.set(session.id, session) |
| 36 | + } |
| 37 | + |
| 38 | + async startChildProcess(options: DevToolsChildProcessTerminalOptions): Promise<DevToolsChildProcessTerminalSession> { |
| 39 | + if (this.sessions.has(options.id)) { |
| 40 | + throw new Error(`Terminal session with id "${options.id}" already registered`) |
| 41 | + } |
| 42 | + const { exec } = await import('tinyexec') |
| 43 | + |
| 44 | + let controller: ReadableStreamDefaultController<string> | undefined |
| 45 | + const buffer = new ReadableStream<string>({ |
| 46 | + async start(_controller) { |
| 47 | + controller = _controller |
| 48 | + }, |
| 49 | + }) |
| 50 | + const writer = new WritableStream<string>({ |
| 51 | + write(chunk) { |
| 52 | + controller?.enqueue(chunk) |
| 53 | + }, |
| 54 | + }) |
| 55 | + |
| 56 | + function createChildProcess() { |
| 57 | + const cp = exec( |
| 58 | + options.command, |
| 59 | + options.args || [], |
| 60 | + { |
| 61 | + nodeOptions: { |
| 62 | + env: { |
| 63 | + COLORS: 'true', |
| 64 | + FORCE_COLOR: 'true', |
| 65 | + ...(options.env || {}), |
| 66 | + }, |
| 67 | + cwd: options.cwd ?? process.cwd(), |
| 68 | + stdio: 'pipe', |
| 69 | + }, |
| 70 | + }, |
| 71 | + ) |
| 72 | + |
| 73 | + const stream = new ReadableStream<string>({ |
| 74 | + async start(controller) { |
| 75 | + for await (const chunk of cp) { |
| 76 | + controller.enqueue(chunk) |
| 77 | + } |
| 78 | + }, |
| 79 | + }) |
| 80 | + stream.pipeTo(writer) |
| 81 | + return cp |
| 82 | + } |
| 83 | + |
| 84 | + let cp: TinyExecResult | undefined = createChildProcess() |
| 85 | + |
| 86 | + const restart = async () => { |
| 87 | + cp?.kill() |
| 88 | + cp = createChildProcess() |
| 89 | + } |
| 90 | + const terminate = async () => { |
| 91 | + cp?.kill() |
| 92 | + cp = undefined |
| 93 | + } |
| 94 | + |
| 95 | + const session: DevToolsChildProcessTerminalSession = { |
| 96 | + ...options, |
| 97 | + status: 'running', |
| 98 | + stream: buffer, |
| 99 | + getChildProcess: () => cp?.process, |
| 100 | + terminate, |
| 101 | + restart, |
| 102 | + } |
| 103 | + this.sessions.set(session.id, session) |
| 104 | + |
| 105 | + return Promise.resolve(session) |
| 106 | + } |
| 107 | +} |
0 commit comments