Skip to content

Commit 46c7802

Browse files
khareyash05viferga
andauthored
refactor: tackle corner cases and change variable names for better readability (#38)
* refactor: tackle corner cases and change variable names for better readability Signed-off-by: Yash <[email protected]> * fix: linting errors * Update delete.ts --------- Signed-off-by: Yash <[email protected]> Co-authored-by: Vicente Eduardo Ferrer Garcia <[email protected]>
1 parent f5b2ea0 commit 46c7802

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

src/api.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import uploadController from './controller/upload';
1010
import {
1111
allApplications,
1212
childProcessResponse,
13-
cps,
13+
childProcesses,
1414
currentFile,
1515
deleteBody,
1616
deployBody,
@@ -56,7 +56,7 @@ export const callFnByName = (
5656
const { appName: app, name } = req.params;
5757
const args = Object.values(req.body);
5858

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

70-
cps[app].send({
70+
childProcesses[app].send({
7171
type: protocol.c,
7272
fn: {
7373
name,
7474
args
7575
}
7676
});
7777

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

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

106-
// 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
106+
if (!(app in childProcesses)) {
107+
next(
108+
new AppError(
109+
`Oops! It looks like the application (${app}) hasn't been deployed yet. Please deploy it before you can call its functions.`,
110+
404
111+
)
112+
);
113+
}
107114

108115
if (!(await exists(appLocation)))
109116
next(
@@ -244,7 +251,7 @@ export const deploy = catchAsync(
244251
if (data.type === protocol.g) {
245252
if (isIAllApps(data.data)) {
246253
const appName = Object.keys(data.data)[0];
247-
cps[appName] = proc;
254+
childProcesses[appName] = proc;
248255
allApplications[appName] = data.data[appName];
249256
}
250257
}

src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const protocol = {
108108
r: 'functionInvokeResult'
109109
};
110110

111-
export const cps: { [key: string]: ChildProcess } = {};
111+
export const childProcesses: { [key: string]: ChildProcess } = {};
112112

113113
export interface childProcessResponse {
114114
type: keyof typeof protocol;

src/controller/delete.ts

+20-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { join } from 'path';
44

55
import { Request, Response } from 'express';
66

7-
import { allApplications, cps, deleteBody } from '../constants';
7+
import { allApplications, childProcesses, deleteBody } from '../constants';
88
import { appsDirectory } from '../utils/config';
9-
import { deleteStatusMessage } from '../utils/resposeTexts';
9+
import { deleteStatusMessage } from '../utils/responseTexts';
10+
import { ensureFolderExists } from '../utils/utils';
1011

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

21-
// Check if the application exists in cps and allApplications objects
22-
if (!(app in cps && app in allApplications)) {
22+
// Check if the application exists in childProcesses and allApplications objects
23+
if (!(app in childProcesses && app in allApplications)) {
2324
isError = true;
25+
return res.send(deleteStatusMessage(app)['error']);
2426
}
2527

2628
// Retrieve the child process associated with the application and kill it
27-
const appCP: ChildProcess = cps[app];
28-
appCP.kill();
29+
const childProcessesInApplications: ChildProcess = childProcesses[app];
30+
childProcessesInApplications.kill();
2931

30-
// Remove the application from cps and allApplications objects
31-
delete cps[app];
32+
// Remove the application from childProcesses and allApplications objects
33+
delete childProcesses[app];
3234
delete allApplications[app];
3335

36+
if (app in childProcesses && app in allApplications) {
37+
isError = true;
38+
return res.send(deleteStatusMessage(app)['appShouldntExist']);
39+
}
40+
3441
// Determine the location of the application
3542
const appLocation = join(appsDirectory(), app);
3643

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

47+
if (!(await ensureFolderExists(appLocation))) {
48+
isError = true;
49+
return res.send(deleteStatusMessage(app)['folderShouldntExist']);
50+
}
51+
4052
// Send response based on whether there was an error during deletion
4153
return res.send(
4254
isError

src/utils/responseTexts.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const deleteStatusMessage = (
2+
app: string
3+
): {
4+
success: string;
5+
error: string;
6+
folderShouldntExist: string;
7+
appShouldntExist: string;
8+
} => ({
9+
success: 'Deploy Delete Succeed',
10+
error: `Oops! It looks like the application (${app}) hasn't been deployed yet. Please deploy it before you delete it.`,
11+
folderShouldntExist: `The folder shouldnt exist after deleting it.`,
12+
appShouldntExist: `The application shouldnt exist after deleting it`
13+
});

src/utils/resposeTexts.ts

-9
This file was deleted.

0 commit comments

Comments
 (0)