Skip to content

Commit

Permalink
refactor: tackle corner cases and change variable names for better re…
Browse files Browse the repository at this point in the history
…adability (#38)

* refactor: tackle corner cases and change variable names for better readability

Signed-off-by: Yash <yash2010118@akgec.ac.in>

* fix: linting errors

* Update delete.ts

---------

Signed-off-by: Yash <yash2010118@akgec.ac.in>
Co-authored-by: Vicente Eduardo Ferrer Garcia <7854099+viferga@users.noreply.github.com>
  • Loading branch information
khareyash05 and viferga authored Mar 25, 2024
1 parent f5b2ea0 commit 46c7802
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
19 changes: 13 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import uploadController from './controller/upload';
import {
allApplications,
childProcessResponse,
cps,
childProcesses,
currentFile,
deleteBody,
deployBody,
Expand Down Expand Up @@ -56,7 +56,7 @@ export const callFnByName = (
const { appName: app, name } = req.params;
const args = Object.values(req.body);

if (!(app in cps)) {
if (!(app in childProcesses)) {
return res
.status(404)
.send(
Expand All @@ -67,15 +67,15 @@ export const callFnByName = (
let responseSent = false; // Flag to track if response has been sent
let errorCame = false;

cps[app].send({
childProcesses[app].send({
type: protocol.c,
fn: {
name,
args
}
});

cps[app].on('message', (data: childProcessResponse) => {
childProcesses[app].on('message', (data: childProcessResponse) => {
if (!responseSent) {
// Check if response has already been sent
if (data.type === protocol.r) {
Expand Down Expand Up @@ -103,7 +103,14 @@ export const serveStatic = catchAsync(

const appLocation = path.join(appsDir, `${app}/${file}`);

// TODO - The best way to handle this is first list all the application which has been deployed and match if there is application or not and then go for file search
if (!(app in childProcesses)) {
next(
new AppError(
`Oops! It looks like the application (${app}) hasn't been deployed yet. Please deploy it before you can call its functions.`,
404
)
);
}

if (!(await exists(appLocation)))
next(
Expand Down Expand Up @@ -244,7 +251,7 @@ export const deploy = catchAsync(
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
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const protocol = {
r: 'functionInvokeResult'
};

export const cps: { [key: string]: ChildProcess } = {};
export const childProcesses: { [key: string]: ChildProcess } = {};

export interface childProcessResponse {
type: keyof typeof protocol;
Expand Down
28 changes: 20 additions & 8 deletions src/controller/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { join } from 'path';

import { Request, Response } from 'express';

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

export default (
req: Omit<Request, 'body'> & { body: deleteBody },
Expand All @@ -18,25 +19,36 @@ export default (
// Initialize isError flag
let isError = false;

// Check if the application exists in cps and allApplications objects
if (!(app in cps && app in allApplications)) {
// 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 appCP: ChildProcess = cps[app];
appCP.kill();
const childProcessesInApplications: ChildProcess = childProcesses[app];
childProcessesInApplications.kill();

// Remove the application from cps and allApplications objects
delete cps[app];
// 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
Expand Down
13 changes: 13 additions & 0 deletions src/utils/responseTexts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const deleteStatusMessage = (
app: string
): {
success: string;
error: string;
folderShouldntExist: string;
appShouldntExist: string;
} => ({
success: 'Deploy Delete Succeed',
error: `Oops! It looks like the application (${app}) hasn't been deployed yet. Please deploy it before you delete it.`,
folderShouldntExist: `The folder shouldnt exist after deleting it.`,
appShouldntExist: `The application shouldnt exist after deleting it`
});
9 changes: 0 additions & 9 deletions src/utils/resposeTexts.ts

This file was deleted.

0 comments on commit 46c7802

Please sign in to comment.