Skip to content

Commit 03320ad

Browse files
committed
feat: Added test to list of remote commands
1 parent 6ae774b commit 03320ad

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/connect/http-routes/create-command.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export enum ConnectCommand {
1313
IMPORT = 'import',
1414
REFRESH = 'refresh',
1515
INIT = 'init',
16+
TEST = 'test',
1617
}
1718

1819
interface Params {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

src/connect/http-routes/router.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { initHandler } from './handlers/init-handler.js';
77
import { planHandler } from './handlers/plan-handler.js';
88
import { refreshHandler } from './handlers/refresh-handler.js';
99
import { terminalHandler } from './handlers/terminal-handler.js';
10+
import { testHandler } from './handlers/test-handler.js';
1011

1112
const router = Router();
1213

@@ -17,5 +18,7 @@ router.use('/import', importHandler());
1718
router.use('/refresh', refreshHandler());
1819
router.use('/terminal', terminalHandler());
1920
router.use('/init', initHandler());
21+
router.use('/test', testHandler());
22+
2023

2124
export default router;

0 commit comments

Comments
 (0)