Skip to content

Commit 9424c71

Browse files
committed
feat: Added destroy to connect
1 parent 07d167a commit 9424c71

4 files changed

Lines changed: 57 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
"deploy": "npm run pkg && npm run notarize && npm run upload",
146146
"prepublishOnly": "npm run build"
147147
},
148-
"version": "1.1.0-beta7",
148+
"version": "1.1.0-beta8",
149149
"bugs": "https://github.com/codifycli/codify/issues",
150150
"keywords": [
151151
"oclif",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export enum ConnectCommand {
1212
PLAN = 'plan',
1313
IMPORT = 'import',
1414
REFRESH = 'refresh',
15+
DESTROY = 'destroy',
1516
INIT = 'init',
1617
TEST = 'test',
1718
}
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 '@codifycli/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 destroyHandler() {
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} destroy -p ${filePath}`], {
34+
name: 'xterm-color',
35+
cols: 80,
36+
rows: 30,
37+
cwd: process.env.HOME,
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.DESTROY,
50+
spawnCommand,
51+
onExit
52+
});
53+
}

src/connect/http-routes/router.ts

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

33
import { applyHandler } from './handlers/apply-handler.js';
4+
import { destroyHandler } from './handlers/destroy-handler.js';
45
import { importHandler } from './handlers/import-handler.js';
56
import defaultHandler from './handlers/index.js';
67
import { initHandler } from './handlers/init-handler.js';
@@ -14,6 +15,7 @@ const router = Router();
1415

1516
router.use('/', defaultHandler);
1617
router.use('/apply', applyHandler());
18+
router.use('/destroy', destroyHandler());
1719
router.use('/plan', planHandler())
1820
router.use('/import', importHandler());
1921
router.use('/refresh', refreshHandler());

0 commit comments

Comments
 (0)