Skip to content

Commit b071085

Browse files
committed
Add multiple improvements, refactor busboy, needs more improvement in the deploy create and worker.
1 parent 72802cb commit b071085

File tree

8 files changed

+232
-144
lines changed

8 files changed

+232
-144
lines changed

package.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@
6666
"rules": {
6767
"tsdoc/syntax": "warn",
6868
"no-unused-vars": "off",
69+
"@typescript-eslint/naming-convention": [
70+
"error",
71+
{
72+
"selector": "property",
73+
"format": [
74+
"camelCase"
75+
]
76+
}
77+
],
6978
"@typescript-eslint/no-unused-vars": [
7079
"error",
7180
{
@@ -101,4 +110,4 @@
101110
"prettier": "^2.1.2",
102111
"typescript": "^4.3.2"
103112
}
104-
}
113+
}

src/api.ts

+30-43
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import deployDeleteController from './controller/delete';
88
import uploadController from './controller/upload';
99

1010
import {
11-
CurrentUploadedFile,
11+
DeleteBody,
12+
DeployBody,
13+
Deployment,
14+
FetchBranchListBody,
15+
FetchFilesFromRepoBody,
1216
ProtocolMessageType,
1317
WorkerMessage,
1418
WorkerMessageUnknown,
1519
allApplications,
1620
childProcesses,
17-
deleteBody,
18-
deployBody,
19-
fetchBranchListBody,
20-
fetchFilesFromRepoBody
21+
deploymentMap
2122
} from './constants';
2223

2324
import AppError from './utils/appError';
@@ -132,7 +133,7 @@ export const fetchFiles = (
132133

133134
export const fetchFilesFromRepo = catchAsync(
134135
async (
135-
req: Omit<Request, 'body'> & { body: fetchFilesFromRepoBody },
136+
req: Omit<Request, 'body'> & { body: FetchFilesFromRepoBody },
136137
res: Response,
137138
next: NextFunction
138139
) => {
@@ -158,16 +159,16 @@ export const fetchFilesFromRepo = catchAsync(
158159
const id = dirName(req.body.url);
159160

160161
// TODO: This method is wrong
161-
// currentFile['id'] = id;
162-
// currentFile.path = `${appsDir}/${id}`;
162+
// deployment.id = id;
163+
// deployment.path = `${appsDir}/${id}`;
163164

164165
return res.status(201).send({ id });
165166
}
166167
);
167168

168169
export const fetchBranchList = catchAsync(
169170
async (
170-
req: Omit<Request, 'body'> & { body: fetchBranchListBody },
171+
req: Omit<Request, 'body'> & { body: FetchBranchListBody },
171172
res: Response
172173
) => {
173174
const { stdout } = await execPromise(
@@ -190,7 +191,7 @@ export const fetchBranchList = catchAsync(
190191

191192
export const fetchFileList = catchAsync(
192193
async (
193-
req: Omit<Request, 'body'> & { body: fetchFilesFromRepoBody },
194+
req: Omit<Request, 'body'> & { body: FetchFilesFromRepoBody },
194195
res: Response,
195196
next: NextFunction
196197
) => {
@@ -224,44 +225,45 @@ export const fetchFileList = catchAsync(
224225

225226
export const deploy = catchAsync(
226227
async (
227-
req: Omit<Request, 'body'> & { body: deployBody },
228+
req: Omit<Request, 'body'> & { body: DeployBody },
228229
res: Response,
229230
next: NextFunction
230231
) => {
231232
try {
232-
// TODO Currently Deploy function will only work for workdir, we will add the addRepo
233+
// TODO: Implement repository
233234
// req.body.resourceType == 'Repository' &&
234235
// (await calculatePackages(next));
235236

236-
console.log(req.body);
237+
const deployment = deploymentMap[req.body.suffix];
237238

238-
const currentFile: CurrentUploadedFile = {
239-
id: '',
240-
type: '',
241-
path: '',
242-
jsons: []
243-
};
239+
if (deployment === undefined) {
240+
return next(
241+
new AppError(
242+
`Invalid deployment id: ${req.body.suffix}`,
243+
400
244+
)
245+
);
246+
}
244247

245-
await installDependencies(currentFile);
248+
await installDependencies(deployment);
246249

247-
const desiredPath = path.join(__dirname, '/worker/index.js');
250+
const desiredPath = path.join(__dirname, 'worker', 'index.js');
248251

249252
const proc = spawn('metacall', [desiredPath], {
250253
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
251254
});
252255

253256
proc.send({
254257
type: ProtocolMessageType.Load,
255-
data: currentFile
258+
data: deployment
256259
});
257260

258-
logProcessOutput(proc.stdout, proc.pid, currentFile.id);
259-
logProcessOutput(proc.stderr, proc.pid, currentFile.id);
261+
logProcessOutput(proc.stdout, proc.pid, deployment.id);
262+
logProcessOutput(proc.stderr, proc.pid, deployment.id);
260263

261264
proc.on('message', (payload: WorkerMessageUnknown) => {
262265
if (payload.type === ProtocolMessageType.MetaData) {
263-
const message =
264-
payload as WorkerMessage<CurrentUploadedFile>;
266+
const message = payload as WorkerMessage<Deployment>;
265267
if (isIAllApps(message.data)) {
266268
const appName = Object.keys(message.data)[0];
267269
childProcesses[appName] = proc;
@@ -272,7 +274,7 @@ export const deploy = catchAsync(
272274

273275
return res.status(200).json({
274276
suffix: hostname(),
275-
prefix: currentFile.id,
277+
prefix: deployment.id,
276278
version: 'v1'
277279
});
278280
} catch (err) {
@@ -290,7 +292,7 @@ export const showLogs = (req: Request, res: Response): Response => {
290292
};
291293

292294
export const deployDelete = (
293-
req: Omit<Request, 'body'> & { body: deleteBody },
295+
req: Omit<Request, 'body'> & { body: DeleteBody },
294296
res: Response,
295297
next: NextFunction
296298
): void => deployDeleteController(req, res, next);
@@ -303,18 +305,3 @@ export const validateAndDeployEnabled = (
303305
status: 'success',
304306
data: true
305307
});
306-
307-
/**
308-
* deploy
309-
* Provide a mesage that repo has been deployed, use --inspect to know more about deployment
310-
* We can add the type of url in the --inspect
311-
* If there is already metacall.json present then, log found metacall.json and reading it, reading done
312-
* We must an option to go back in the fileselection wizard so that, user dont have to close the connection
313-
* At the end of deployment through deploy cli, we should run the --inspect command so that current deployed file is shown and show only the current deployed app
314-
*
315-
*
316-
* FAAS
317-
* the apps are not getting detected once the server closes, do we need to again deploy them
318-
* find a way to detect metacall.json in the files and dont deploy if there is not because json ke through we are uploading
319-
*
320-
*/

src/constants.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { DeployStatus, MetaCallJSON } from '@metacall/protocol/deployment';
22
import { ChildProcess } from 'child_process';
33

4-
export interface CurrentUploadedFile {
4+
export interface Deployment {
55
id: string;
66
type?: string;
77
jsons: MetaCallJSON[];
88
runners?: string[];
99
path: string;
10+
blob?: string;
1011
}
1112

13+
export const deploymentMap: Record<string, Deployment> = {};
14+
1215
export const createInstallDependenciesScript = (
1316
runner: string,
1417
path: string
@@ -22,28 +25,25 @@ export const createInstallDependenciesScript = (
2225
return installDependenciesScript[runner];
2326
};
2427

25-
export type namearg = 'id' | 'type' | 'jsons' | 'runners' | 'path';
26-
export type valueArg = string;
27-
28-
export type fetchFilesFromRepoBody = {
28+
export type FetchFilesFromRepoBody = {
2929
branch: 'string';
3030
url: 'string';
3131
};
32-
export type fetchBranchListBody = {
32+
export type FetchBranchListBody = {
3333
url: 'string';
3434
};
3535

36-
export type deployBody = {
37-
suffix: string; //name of deployment
36+
export type DeployBody = {
37+
suffix: string; // name of deployment
3838
resourceType: 'Package' | 'Repository';
39-
release: string; //release date
39+
release: string; // release date
4040
env: string[];
4141
plan: string;
4242
version: string;
4343
};
4444

45-
export type deleteBody = {
46-
suffix: string; //name of deployment
45+
export type DeleteBody = {
46+
suffix: string; // name of deployment
4747
prefix: string;
4848
version: string;
4949
};
@@ -118,7 +118,7 @@ export interface LogMessage {
118118
message: string;
119119
}
120120

121-
export const asniCode: number[] = [
121+
export const ANSICode: number[] = [
122122
166, 154, 142, 118, 203, 202, 190, 215, 214, 32, 6, 4, 220, 208, 184, 172
123123
];
124124

src/controller/delete.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { join } from 'path';
44

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

7-
import { allApplications, childProcesses, deleteBody } from '../constants';
7+
import { allApplications, childProcesses, DeleteBody } from '../constants';
88
import { appsDirectory } from '../utils/config';
99
import { deleteStatusMessage } from '../utils/responseTexts';
1010
import { catchAsync, ensureFolderExists } from '../utils/utils';
1111

1212
export default catchAsync(
1313
async (
14-
req: Omit<Request, 'body'> & { body: deleteBody },
14+
req: Omit<Request, 'body'> & { body: DeleteBody },
1515
res: Response,
1616
_next: NextFunction
1717
): Promise<Response> => {

0 commit comments

Comments
 (0)