Skip to content

Commit 2844d46

Browse files
HeeManSuviferga
andauthored
Auto Deploy refactored and sync methods removed (#48)
* autoDeploy refactored and sync method removed * Update package-lock.json --------- Co-authored-by: Vicente Eduardo Ferrer Garcia <[email protected]>
1 parent bc5f8a8 commit 2844d46

File tree

4 files changed

+73
-55
lines changed

4 files changed

+73
-55
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"request": "launch",
1010
"name": "Launch Program",
1111
"skipFiles": ["<node_internals>/**"],
12-
"program": "${workspaceFolder}/dist/server.js",
12+
"program": "${workspaceFolder}/dist/index.js",
1313
"preLaunchTask": "npm: buildDebug",
1414
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
1515
}

src/controller/delete.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextFunction, Request, Response } from 'express';
22

33
import { ChildProcess } from 'child_process';
4-
import { rmSync } from 'fs';
4+
import { rm } from 'fs/promises';
55
import { join } from 'path';
66

77
import { allApplications, childProcesses } from '../constants';
@@ -29,7 +29,6 @@ type DeleteBody = {
2929
version: string;
3030
};
3131

32-
// TODO: Refactor this, do not use sync methods
3332
export const deployDelete = catchAsync(
3433
async (
3534
req: Omit<Request, 'body'> & { body: DeleteBody },
@@ -65,7 +64,7 @@ export const deployDelete = catchAsync(
6564
const appLocation = join(appsDirectory, app);
6665

6766
// Delete the directory of the application
68-
rmSync(appLocation, { recursive: true, force: true });
67+
await rm(appLocation, { recursive: true, force: true });
6968

7069
if (!(await ensureFolderExists(appLocation))) {
7170
isError = true;

src/index.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import colors from 'colors';
22
import dotenv from 'dotenv';
33

44
import app from './app';
5-
import { findJsonFilesRecursively } from './utils/autoDeploy';
5+
import { autoDeployApps } from './utils/autoDeploy';
66
import { appsDirectory } from './utils/config';
77
import { ensureFolderExists } from './utils/utils';
88

@@ -14,11 +14,7 @@ void (async (): Promise<void> => {
1414

1515
await ensureFolderExists(appsDirectory);
1616

17-
// TODO: Refactor this
18-
await findJsonFilesRecursively(appsDirectory);
19-
20-
console.log('Previously deployed applications deployed successfully');
21-
// END-TODO
17+
await autoDeployApps(appsDirectory);
2218

2319
const port = process.env.PORT || 9000;
2420

src/utils/autoDeploy.ts

+68-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { pathIsMetaCallJson } from '@metacall/protocol/package';
1+
import { MetaCallJSON, pathIsMetaCallJson } from '@metacall/protocol';
22
import { spawn } from 'child_process';
3-
import * as fs from 'fs';
3+
import * as fs from 'fs/promises';
44
import * as path from 'path';
55
import {
66
Deployment,
@@ -13,57 +13,80 @@ import {
1313
} from '../constants';
1414
import { isIAllApps, logProcessOutput } from './utils';
1515

16-
// TODO: Refactor this
17-
export const findJsonFilesRecursively = async (
18-
appsDir: string
19-
): Promise<void> => {
20-
// TODO: Avoid sync commands
21-
const files = fs.readdirSync(appsDir, { withFileTypes: true });
22-
for (const file of files) {
23-
if (file.isDirectory()) {
24-
await findJsonFilesRecursively(path.join(appsDir, file.name));
25-
} else if (pathIsMetaCallJson(file.name)) {
26-
const filePath = path.join(appsDir, file.name);
27-
const desiredPath = path.join(
28-
path.resolve(__dirname, '..'),
29-
'worker',
30-
'index.js'
31-
);
32-
const id = path.basename(appsDir);
16+
export const autoDeployApps = async (appsDir: string): Promise<void> => {
17+
const allDirectories = await fs.readdir(appsDir, { withFileTypes: true });
3318

34-
const deployment: Deployment = {
35-
id,
36-
type: 'application/x-zip-compressed',
37-
path: appsDir,
38-
jsons: []
39-
};
19+
let directoryProcessed = false;
4020

41-
const proc = spawn('metacall', [desiredPath, filePath], {
42-
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
21+
for (const directory of allDirectories) {
22+
if (directory.isDirectory()) {
23+
const directoryPath = path.join(appsDir, directory.name);
24+
const directoryFiles = await fs.readdir(directoryPath, {
25+
withFileTypes: true
4326
});
4427

45-
const message: WorkerMessage<Deployment> = {
46-
type: ProtocolMessageType.Load,
47-
data: deployment
48-
};
28+
const jsonFiles = directoryFiles
29+
.filter(file => file.isFile() && pathIsMetaCallJson(file.name))
30+
.map(file => path.join(directoryPath, file.name));
4931

50-
proc.send(message);
32+
if (jsonFiles.length > 0) {
33+
directoryProcessed = true;
5134

52-
logProcessOutput(proc.stdout, proc.pid, deployment.id);
53-
logProcessOutput(proc.stderr, proc.pid, deployment.id);
35+
const desiredPath = path.join(
36+
path.resolve(__dirname, '..'),
37+
'worker',
38+
'index.js'
39+
);
5440

55-
proc.on('message', (payload: WorkerMessageUnknown) => {
56-
if (payload.type === ProtocolMessageType.MetaData) {
57-
const message = payload as WorkerMessage<
58-
Record<string, IAppWithFunctions>
59-
>;
60-
if (isIAllApps(message.data)) {
61-
const appName = Object.keys(message.data)[0];
62-
childProcesses[appName] = proc;
63-
allApplications[appName] = message.data[appName];
41+
const jsonContent: MetaCallJSON[] = await Promise.all(
42+
jsonFiles.map(async file => {
43+
const content = await fs.readFile(file, 'utf-8');
44+
45+
// map method returns array.That's why didn't passed MetaCallJSON[]
46+
return JSON.parse(content) as MetaCallJSON;
47+
})
48+
);
49+
50+
const deployment: Deployment = {
51+
id: directory.name,
52+
type: 'application/x-zip-compressed',
53+
path: directoryPath,
54+
jsons: jsonContent
55+
};
56+
57+
const proc = spawn('metacall', [desiredPath, ...jsonFiles], {
58+
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
59+
});
60+
61+
const message: WorkerMessage<Deployment> = {
62+
type: ProtocolMessageType.Load,
63+
data: deployment
64+
};
65+
66+
proc.send(message);
67+
68+
logProcessOutput(proc.stdout, proc.pid, deployment.id);
69+
logProcessOutput(proc.stderr, proc.pid, deployment.id);
70+
71+
proc.on('message', (payload: WorkerMessageUnknown) => {
72+
if (payload.type === ProtocolMessageType.MetaData) {
73+
const message = payload as WorkerMessage<
74+
Record<string, IAppWithFunctions>
75+
>;
76+
if (isIAllApps(message.data)) {
77+
const appName = Object.keys(message.data)[0];
78+
childProcesses[appName] = proc;
79+
allApplications[appName] = message.data[appName];
80+
}
6481
}
65-
}
66-
});
82+
});
83+
}
6784
}
6885
}
86+
87+
if (directoryProcessed) {
88+
console.log(
89+
'Previously deployed applications deployed successfully'.green
90+
);
91+
}
6992
};

0 commit comments

Comments
 (0)