-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.ts
More file actions
51 lines (40 loc) · 1.3 KB
/
apply.ts
File metadata and controls
51 lines (40 loc) · 1.3 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
import { spawn } from '@homebridge/node-pty-prebuilt-multiarch';
import * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';
import { v4 as uuid } from 'uuid';
import { WebSocket } from 'ws';
import { WsServerManager } from './server.js';
export function connectApplyInitHandler(msg: any, initWs: WebSocket, manager: WsServerManager) {
const sessionId = uuid();
manager.addAdhocWsServer(sessionId, async (ws) => {
console.log('connected apply ws');
const { config } = msg;
console.log('apply ws open', config);
const tmpDir = await fs.mkdtemp(os.tmpdir());
const filePath = path.join(tmpDir, 'codify.json');
await fs.writeFile(filePath, JSON.stringify(config));
const pty = spawn('zsh', ['-c', 'codify apply'], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
pty.onData((data) => {
ws.send(Buffer.from(data, 'utf8'));
});
ws.on('message', (message) => {
pty.write(message.toString('utf8'));
})
pty.onExit(({ exitCode, signal }) => {
console.log('pty exit', exitCode, signal);
// ws.close(exitCode);
ws.terminate();
})
});
initWs.send(JSON.stringify({
cmd: 'apply_init_response',
sessionId,
}))
}