diff --git a/src/api.ts b/src/api.ts index f902d0d..ed70fa1 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,7 @@ import { spawn } from 'child_process'; import colors from 'colors'; import { NextFunction, Request, Response } from 'express'; +import * as fs from 'fs'; import { hostname } from 'os'; import * as path from 'path'; @@ -11,6 +12,7 @@ import { childProcessResponse, cps, currentFile, + deleteBody, deployBody, fetchBranchListBody, fetchFilesFromRepoBody, @@ -253,6 +255,34 @@ export const showLogs = (req: Request, res: Response): Response => { return res.send('Demo Logs...'); }; +export const deployDelete = ( + req: Omit & { body: deleteBody }, + res: Response, + next: NextFunction +): void => { + const { suffix: appName } = req.body; + + try { + if (cps[appName]) { + cps[appName].kill(); + delete cps[appName]; + } + + const appPath = path.join(appsDir, appName); + + if (fs.existsSync(appPath)) { + fs.rmdirSync(appPath, { recursive: true }); + } + delete allApplications[appName]; + + res.send(`Deleting ${appName} deploy Succeed`); + } catch (error) { + next( + new AppError('error occurred in deleting repository directory', 500) + ); + } +}; + export const validateAndDeployEnabled = ( req: Request, res: Response diff --git a/src/app.ts b/src/app.ts index 0166666..235ca49 100644 --- a/src/app.ts +++ b/src/app.ts @@ -31,6 +31,8 @@ app.post('/api/deploy/logs', api.showLogs); app.post('/api/deploy/create', api.deploy); +app.post('/api/deploy/delete', api.deployDelete); + app.get('/api/inspect', (req, res) => { res.send(Object.values(allApplications)); }); diff --git a/src/constants.ts b/src/constants.ts index 5b8008a..7aadaa9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -108,3 +108,9 @@ export interface childProcessResponse { type: keyof typeof protocol; data: unknown; } + +export interface deleteBody { + suffix: string; + prefix: string; + version: string; +}