-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.post.js
More file actions
107 lines (96 loc) · 3.27 KB
/
Copy pathrun.post.js
File metadata and controls
107 lines (96 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Node imports
import fs from "node:fs";
import path from "node:path";
// Third party imports
import { createError, defineEventHandler, readBody } from "h3";
// Local imports
import {
addMicroserviceMetadatas,
runBack,
} from "@geode/opengeodeweb-front/app/utils/local/microservices.js";
import {
executableName,
extensionFolderPath,
extensionFrontendPath,
} from "@geode/opengeodeweb-front/app/utils/local/path.js";
import { extensionsConf } from "@geode/opengeodeweb-front/app/utils/config.js";
import { unzipFile } from "@geode/opengeodeweb-front/app/utils/server.js";
export default defineEventHandler(async (event) => {
try {
const body = await readBody(event);
const { projectFolderPath, projectName } = body;
const extensionsConfig = extensionsConf(projectName);
const extensionsArray = await Promise.all(
Object.keys(extensionsConfig).map(async (extensionId) => {
const extensionPath = extensionsConfig[extensionId].path;
const unzippedExtensionPath = await unzipFile(
extensionPath,
extensionFolderPath(projectFolderPath, extensionId),
);
const metadataPath = path.join(unzippedExtensionPath, "metadata.json");
const metadataContent = await fs.promises.readFile(metadataPath, "utf8");
if (!metadataContent) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing metadata.json",
});
}
const metadata = JSON.parse(metadataContent);
console.log("runExtensions", { metadata });
const { id, name, version, backendExecutable, frontendFile } = metadata;
console.log("runExtensions", { id, name, version, backendExecutable });
if (!frontendFile) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing frontend JavaScript",
});
}
if (!backendExecutable) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing backend executable",
});
}
const frontendFilePath = await extensionFrontendPath(
unzippedExtensionPath,
frontendFile,
path.resolve(),
id,
);
console.log("runExtensions", { frontendFilePath });
const frontendContent = await fs.promises.readFile(frontendFilePath, "utf8");
const backendExecutablePath = path.join(
unzippedExtensionPath,
executableName(backendExecutable),
);
console.log("runExtensions", { backendExecutablePath });
fs.chmodSync(backendExecutablePath, "755");
const port = await runBack(backendExecutable, unzippedExtensionPath, {
projectFolderPath,
});
await addMicroserviceMetadatas(projectFolderPath, {
type: "back",
name,
port,
});
return {
id,
name,
version,
frontendContent,
port,
};
}),
);
return {
statusCode: 200,
extensionsArray,
};
} catch (error) {
console.error("Error running extensions:", error);
throw createError({
statusCode: 500,
statusMessage: error.message,
});
}
});