Skip to content

Commit

Permalink
Solved issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Mar 25, 2024
1 parent 46c7802 commit d3d9d45
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 63 deletions.
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@
"project": "./tsconfig.json"
},
"rules": {
"tsdoc/syntax": "warn"
"tsdoc/syntax": "warn",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
}
},
"dependencies": {
Expand Down Expand Up @@ -91,4 +100,4 @@
"prettier": "^2.1.2",
"typescript": "^4.3.2"
}
}
}
19 changes: 11 additions & 8 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const callFnByName = (
};

export const serveStatic = catchAsync(
async (req: Request, res: Response, next: NextFunction) => {
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
if (!req.params) next(new AppError('Invalid API endpoint', 404));

const { app, file } = req.params;
Expand All @@ -120,7 +120,7 @@ export const serveStatic = catchAsync(
)
);

res.status(200).sendFile(appLocation);
return res.status(200).sendFile(appLocation);
}
);

Expand Down Expand Up @@ -160,7 +160,7 @@ export const fetchFilesFromRepo = catchAsync(
currentFile['id'] = id;
currentFile.path = `${appsDir}/${id}`;

res.status(201).send({ id });
return res.status(201).send({ id });
}
);

Expand All @@ -183,7 +183,7 @@ export const fetchBranchList = catchAsync(
}
});

res.send({ branches });
return res.send({ branches });
}
);

Expand Down Expand Up @@ -215,7 +215,9 @@ export const fetchFileList = catchAsync(
`cd ${dirPath} ; git ls-tree -r ${req.body.branch} --name-only; cd .. ; rm -r ${dirPath}`
);

res.send({ files: JSON.stringify(stdout.toString()).split('\\n') });
return res.send({
files: JSON.stringify(stdout.toString()).split('\\n')
});
}
);

Expand Down Expand Up @@ -257,7 +259,7 @@ export const deploy = catchAsync(
}
});

res.status(200).json({
return res.status(200).json({
suffix: hostname(),
prefix: currentFile.id,
version: 'v1'
Expand All @@ -278,8 +280,9 @@ export const showLogs = (req: Request, res: Response): Response => {

export const deployDelete = (
req: Omit<Request, 'body'> & { body: deleteBody },
res: Response
): Response => deployDeleteController(req, res);
res: Response,
next: NextFunction
): void => deployDeleteController(req, res, next);

export const validateAndDeployEnabled = (
req: Request,
Expand Down
101 changes: 52 additions & 49 deletions src/controller/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,60 @@ import { ChildProcess } from 'child_process';
import { rmSync } from 'fs';
import { join } from 'path';

import { Request, Response } from 'express';
import { NextFunction, Request, Response } from 'express';

import { allApplications, childProcesses, deleteBody } from '../constants';
import { appsDirectory } from '../utils/config';
import { deleteStatusMessage } from '../utils/responseTexts';
import { ensureFolderExists } from '../utils/utils';

export default (
req: Omit<Request, 'body'> & { body: deleteBody },
res: Response
): Response => {
// Extract the suffix (application name) of the application from the request body
const { suffix: app } = req.body;

// Initialize isError flag
let isError = false;

// Check if the application exists in childProcesses and allApplications objects
if (!(app in childProcesses && app in allApplications)) {
isError = true;
return res.send(deleteStatusMessage(app)['error']);
import { catchAsync, ensureFolderExists } from '../utils/utils';

export default catchAsync(
async (
req: Omit<Request, 'body'> & { body: deleteBody },
res: Response,
_next: NextFunction
): Promise<Response> => {
// Extract the suffix (application name) of the application from the request body
const { suffix: app } = req.body;

// Initialize isError flag
let isError = false;

// Check if the application exists in childProcesses and allApplications objects
if (!(app in childProcesses && app in allApplications)) {
isError = true;
return res.send(deleteStatusMessage(app)['error']);
}

// Retrieve the child process associated with the application and kill it
const childProcessesInApplications: ChildProcess = childProcesses[app];
childProcessesInApplications.kill();

// Remove the application from childProcesses and allApplications objects
delete childProcesses[app];
delete allApplications[app];

if (app in childProcesses && app in allApplications) {
isError = true;
return res.send(deleteStatusMessage(app)['appShouldntExist']);
}

// Determine the location of the application
const appLocation = join(appsDirectory(), app);

// Delete the directory of the application
rmSync(appLocation, { recursive: true, force: true });

if (!(await ensureFolderExists(appLocation))) {
isError = true;
return res.send(deleteStatusMessage(app)['folderShouldntExist']);
}

// Send response based on whether there was an error during deletion
return res.send(
isError
? deleteStatusMessage(app)['error']
: deleteStatusMessage(app)['success']
);
}

// Retrieve the child process associated with the application and kill it
const childProcessesInApplications: ChildProcess = childProcesses[app];
childProcessesInApplications.kill();

// Remove the application from childProcesses and allApplications objects
delete childProcesses[app];
delete allApplications[app];

if (app in childProcesses && app in allApplications) {
isError = true;
return res.send(deleteStatusMessage(app)['appShouldntExist']);
}

// Determine the location of the application
const appLocation = join(appsDirectory(), app);

// Delete the directory of the application
rmSync(appLocation, { recursive: true, force: true });

if (!(await ensureFolderExists(appLocation))) {
isError = true;
return res.send(deleteStatusMessage(app)['folderShouldntExist']);
}

// Send response based on whether there was an error during deletion
return res.send(
isError
? deleteStatusMessage(app)['error']
: deleteStatusMessage(app)['success']
);
};
);
4 changes: 2 additions & 2 deletions src/utils/autoDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path';
import {
allApplications,
childProcessResponse,
cps,
childProcesses,
currentFile,
protocol
} from '../constants';
Expand Down Expand Up @@ -47,7 +47,7 @@ export const findJsonFilesRecursively = async (
if (data.type === protocol.g) {
if (isIAllApps(data.data)) {
const appName = Object.keys(data.data)[0];
cps[appName] = proc;
childProcesses[appName] = proc;
allApplications[appName] = data.data[appName];
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ export const execPromise = (
});

export const catchAsync = (
fn: (req: Request, res: Response, next: NextFunction) => Promise<void>
fn: (
req: Request,
res: Response,
next: NextFunction
) => Promise<Response | void>
): RequestHandler => {
return (req: Request, res: Response, next: NextFunction) => {
fn(req, res, next).catch(err => next(err));
return fn(req, res, next).catch(err => next(err));
};
};

Expand Down

0 comments on commit d3d9d45

Please sign in to comment.