|
| 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 | +} |
0 commit comments