Skip to content

Commit 85929be

Browse files
authored
Issue35 load configuration error (#43)
* metacall.json file fetched * currentFile data populated * child process attached with each file * findJsonFilesRecursively called in app.ts * worker file path corrected
1 parent 97eaf35 commit 85929be

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/app.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import express, { NextFunction, Request, Response } from 'express';
55
import * as api from './api';
66
import { allApplications } from './constants';
77
import AppError from './utils/appError';
8+
import { findJsonFilesRecursively } from './utils/autoDeploy';
9+
import { appsDirectory } from './utils/config';
810
import globalErrorHandler from './utils/errorHandler';
911

1012
const app = express();
@@ -42,6 +44,15 @@ app.all('*', (req: Request, res: Response, next: NextFunction) => {
4244
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
4345
});
4446

47+
const appsDir = appsDirectory();
48+
findJsonFilesRecursively(appsDir)
49+
.then(() => {
50+
console.log('Previously deployed apllications deployed successfully');
51+
})
52+
.catch(error => {
53+
console.error('Error while re-deploying applications', error);
54+
});
55+
4556
app.use(globalErrorHandler);
4657

4758
export default app;

src/utils/autoDeploy.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { spawn } from 'child_process';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
import {
5+
allApplications,
6+
childProcessResponse,
7+
cps,
8+
currentFile,
9+
protocol
10+
} from '../constants';
11+
import { isIAllApps } from './utils';
12+
13+
export const findJsonFilesRecursively = async (
14+
appsDir: string
15+
): Promise<void> => {
16+
const files = fs.readdirSync(appsDir, { withFileTypes: true });
17+
for (const file of files) {
18+
if (file.isDirectory()) {
19+
await findJsonFilesRecursively(path.join(appsDir, file.name));
20+
} else if (file.name === 'metacall.json') {
21+
const filePath = path.join(appsDir, file.name);
22+
const desiredPath = path.join(__dirname, '../worker/index.js');
23+
const id = path.basename(appsDir);
24+
25+
currentFile.id = id;
26+
(currentFile.type = 'application/x-zip-compressed'),
27+
(currentFile.path = appsDir);
28+
29+
const proc = spawn('metacall', [desiredPath, filePath], {
30+
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
31+
});
32+
33+
proc.send({
34+
type: protocol.l,
35+
currentFile
36+
});
37+
38+
proc.stdout?.on('data', (data: Buffer) => {
39+
console.log(data.toString().green);
40+
});
41+
proc.stderr?.on('data', (data: Buffer) => {
42+
console.log(data.toString().red);
43+
});
44+
45+
proc.on('message', (data: childProcessResponse) => {
46+
if (data.type === protocol.g) {
47+
if (isIAllApps(data.data)) {
48+
const appName = Object.keys(data.data)[0];
49+
cps[appName] = proc;
50+
allApplications[appName] = data.data[appName];
51+
}
52+
}
53+
});
54+
}
55+
}
56+
};

0 commit comments

Comments
 (0)