26 lines
1.0 KiB
TypeScript
26 lines
1.0 KiB
TypeScript
import type { FastifyInstance } from 'fastify';
|
|
import { terminalRunRequestSchema, terminalRunResponseSchema } from '@hermes-mobile/shared';
|
|
import type { CompanionConfig } from '../config/companionConfig.js';
|
|
import { toSafeAbsolutePath, toWorkspacePath } from '../config/paths.js';
|
|
import { runProcess } from '../system/processes.js';
|
|
|
|
export async function registerTerminalRoutes(app: FastifyInstance, config: CompanionConfig): Promise<void> {
|
|
app.post('/api/terminal/run', async (request) => {
|
|
const body = terminalRunRequestSchema.parse(request.body);
|
|
const cwd = toSafeAbsolutePath(config.workspaceRoot, body.cwd ?? '.');
|
|
const result = await runProcess('/bin/sh', ['-lc', body.command], {
|
|
cwd,
|
|
timeoutMs: body.timeoutMs ?? 10000,
|
|
});
|
|
|
|
return terminalRunResponseSchema.parse({
|
|
cwd: toWorkspacePath(config.workspaceRoot, cwd),
|
|
command: body.command,
|
|
stdout: result.stdout,
|
|
stderr: result.stderr,
|
|
exitCode: result.exitCode,
|
|
timedOut: result.timedOut,
|
|
});
|
|
});
|
|
}
|