|
| 1 | +import { spawn } from '@homebridge/node-pty-prebuilt-multiarch'; |
| 2 | +import { ConfigFileSchema } from 'codify-schemas'; |
| 3 | +import * as fs from 'node:fs/promises'; |
| 4 | +import os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { WebSocket } from 'ws'; |
| 7 | + |
| 8 | +import { ConnectOrchestrator } from '../../../orchestrators/connect.js'; |
| 9 | +import { ajv } from '../../../utils/ajv.js'; |
| 10 | +import { ShellUtils } from '../../../utils/shell.js'; |
| 11 | +import { Session } from '../../socket-server.js'; |
| 12 | +import { ConnectCommand, createCommandHandler } from '../create-command.js'; |
| 13 | + |
| 14 | +const validator = ajv.compile(ConfigFileSchema); |
| 15 | + |
| 16 | +export function testHandler() { |
| 17 | + const spawnCommand = async (body: Record<string, unknown>, ws: WebSocket, session: Session) => { |
| 18 | + const codifyConfig = body.config; |
| 19 | + if (!codifyConfig) { |
| 20 | + throw new Error('Unable to parse codify config'); |
| 21 | + } |
| 22 | + |
| 23 | + if (!validator(codifyConfig)) { |
| 24 | + throw new Error('Invalid codify config'); |
| 25 | + } |
| 26 | + |
| 27 | + const tmpDir = await fs.mkdtemp(os.tmpdir() + '/'); |
| 28 | + const filePath = path.join(tmpDir, 'codify.jsonc'); |
| 29 | + await fs.writeFile(filePath, JSON.stringify(codifyConfig, null, 2)); |
| 30 | + |
| 31 | + session.additionalData.filePath = filePath; |
| 32 | + |
| 33 | + return spawn(ShellUtils.getDefaultShell(), ['-c', `${ConnectOrchestrator.nodeBinary} ${ConnectOrchestrator.rootCommand} test`], { |
| 34 | + name: 'xterm-color', |
| 35 | + cols: 80, |
| 36 | + rows: 30, |
| 37 | + cwd: filePath, |
| 38 | + env: process.env |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + const onExit = async (exitCode: number, ws: WebSocket, session: Session) => { |
| 43 | + if (session.additionalData.filePath) { |
| 44 | + await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true }); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return createCommandHandler({ |
| 49 | + name: ConnectCommand.TEST, |
| 50 | + spawnCommand, |
| 51 | + onExit |
| 52 | + }); |
| 53 | +} |
0 commit comments