|
| 1 | +import { spawn } from '@homebridge/node-pty-prebuilt-multiarch'; |
| 2 | +import { ConfigFileSchema } from 'codify-schemas'; |
| 3 | +import { diffChars } from 'diff'; |
| 4 | +import fs from 'node:fs/promises'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | +import { WebSocket } from 'ws'; |
| 8 | + |
| 9 | +import { ConnectOrchestrator } from '../../../orchestrators/connect.js'; |
| 10 | +import { ajv } from '../../../utils/ajv.js'; |
| 11 | +import { Session, SocketServer } from '../../socket-server.js'; |
| 12 | +import { ConnectCommand, createCommandHandler } from './create-command.js'; |
| 13 | + |
| 14 | +enum RefreshType { |
| 15 | + REFRESH = 'refresh', |
| 16 | + REFRESH_SPECIFIC = 'refresh_specific' |
| 17 | +} |
| 18 | + |
| 19 | +const validator = ajv.compile(ConfigFileSchema); |
| 20 | + |
| 21 | +export function refreshHandler() { |
| 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(RefreshType).includes(type as RefreshType)) { |
| 29 | + throw new Error('Unable to parse import type'); |
| 30 | + } |
| 31 | + |
| 32 | + if (type === RefreshType.REFRESH_SPECIFIC && (!resourceTypes || Array.isArray(resourceTypes))) { |
| 33 | + throw new Error('For refresh specific, 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 | + session.additionalData.existingFile = codifyConfig; |
| 45 | + |
| 46 | + let args = ''; |
| 47 | + switch (type as RefreshType) { |
| 48 | + case RefreshType.REFRESH: { |
| 49 | + break; |
| 50 | + } |
| 51 | + |
| 52 | + case RefreshType.REFRESH_SPECIFIC: { |
| 53 | + args = (resourceTypes as string[]).join(' '); |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return spawn('zsh', ['-c', `${ConnectOrchestrator.rootCommand} refresh ${args} -p ${filePath}`], { |
| 59 | + name: 'xterm-color', |
| 60 | + cols: 80, |
| 61 | + rows: 30, |
| 62 | + cwd: process.env.HOME, |
| 63 | + env: process.env |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + const onExit = async (exitCode: number, ws: WebSocket, session: Session) => { |
| 68 | + if (session.additionalData.filePath) { |
| 69 | + const updatedFile = await fs.readFile(session.additionalData.filePath as string, 'utf8') |
| 70 | + |
| 71 | + // Changes were found |
| 72 | + if (diffChars(updatedFile, session.additionalData.existingFile as string).length > 0) { |
| 73 | + console.log('Writing imported changes to Codify dashboard'); |
| 74 | + |
| 75 | + const ws = SocketServer.get().getMainConnection(session.clientId); |
| 76 | + if (!ws) { |
| 77 | + throw new Error(`Unable to find client for clientId ${session.clientId}`); |
| 78 | + } |
| 79 | + |
| 80 | + ws.send(JSON.stringify({ key: 'new_import', data: { |
| 81 | + updated: updatedFile, |
| 82 | + } })) |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return createCommandHandler({ |
| 91 | + name: ConnectCommand.REFRESH, |
| 92 | + spawnCommand, |
| 93 | + onExit |
| 94 | + }); |
| 95 | +} |
0 commit comments