Skip to content

Commit ddf8ba3

Browse files
committed
Added import handler
1 parent add4147 commit ddf8ba3

2 files changed

Lines changed: 86 additions & 4 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { spawn } from '@homebridge/node-pty-prebuilt-multiarch';
2+
import { ConfigFileSchema } from 'codify-schemas';
3+
import 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 { Session, SocketServer } from '../../socket-server.js';
11+
import { ConnectCommand, createCommandHandler } from './create-command.js';
12+
13+
enum ImportType {
14+
REFRESH = 'refresh',
15+
NEW_RESOURCES = 'new_resources',
16+
NEW_ALL = 'new_all',
17+
}
18+
19+
const validator = ajv.compile(ConfigFileSchema);
20+
21+
export function importHandler() {
22+
const spawnCommand = async (body: Record<string, unknown>, ws: WebSocket, session: Session) => {
23+
const { config: codifyConfig, type, resourceTypes } = body;
24+
if (!codifyConfig) {
25+
throw new Error('Unable to parse codify config');
26+
}
27+
28+
if (!type || !Object.values(ImportType).includes(type as ImportType)) {
29+
throw new Error('Unable to parse import type');
30+
}
31+
32+
if (type === ImportType.NEW_RESOURCES && (!resourceTypes || Array.isArray(resourceTypes))) {
33+
throw new Error('For new resources import type, a list of resource types must be provided');
34+
}
35+
36+
if (!validator(codifyConfig)) {
37+
throw new Error('Invalid codify config');
38+
}
39+
40+
const tmpDir = await fs.mkdtemp(os.tmpdir());
41+
const filePath = path.join(tmpDir, 'codify.jsonc');
42+
await fs.writeFile(filePath, JSON.stringify(codifyConfig, null, 2));
43+
session.additionalData.filePath = filePath;
44+
45+
let args = '';
46+
switch (type as ImportType) {
47+
case ImportType.REFRESH: {
48+
break;
49+
}
50+
51+
case ImportType.NEW_RESOURCES: {
52+
args = (resourceTypes as string[]).join(' ');
53+
break;
54+
}
55+
56+
case ImportType.NEW_ALL: {
57+
args = '*'
58+
break;
59+
}
60+
}
61+
62+
return spawn('zsh', ['-c', `${ConnectOrchestrator.rootCommand} import ${args} -p ${filePath}`], {
63+
name: 'xterm-color',
64+
cols: 80,
65+
rows: 30,
66+
cwd: process.env.HOME,
67+
env: process.env
68+
});
69+
}
70+
71+
const onExit = async (exitCode: number, ws: WebSocket, session: Session) => {
72+
if (session.additionalData.filePath) {
73+
await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true });
74+
}
75+
}
76+
77+
return createCommandHandler({
78+
name: ConnectCommand.APPLY,
79+
spawnCommand,
80+
onExit
81+
});
82+
}

src/connect/http-routes/router.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { Router } from 'express';
22

3-
import { ConnectCommand, createCommandHandler } from './handlers/create-command.js';
4-
import defaultHandler from './handlers/index.js';
5-
import { terminalHandler } from './handlers/terminal-handler.js';
63
import { applyHandler } from './handlers/apply-handler.js';
4+
import { importHandler } from './handlers/import-handler.js';
5+
import defaultHandler from './handlers/index.js';
76
import { planHandler } from './handlers/plan-handler.js';
7+
import { terminalHandler } from './handlers/terminal-handler.js';
88

99
const router = Router();
1010

1111
router.use('/', defaultHandler);
1212
router.use('/apply', applyHandler());
1313
router.use('/plan', planHandler())
14-
// router.use('/import', createCommandHandler(ConnectCommand.IMPORT));
14+
router.use('/import', importHandler());
1515
router.use('/terminal', terminalHandler());
1616

1717
export default router;

0 commit comments

Comments
 (0)