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