-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildRepoController.ts
More file actions
78 lines (78 loc) · 2.85 KB
/
BuildRepoController.ts
File metadata and controls
78 lines (78 loc) · 2.85 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
import { Context } from "elysia";
import { StatusCode } from "../types/types";
import { spawn } from "bun";
import fs from "fs"
const buildRepoController = async ({ body, set }: Context) => {
const req = body as { repoId: string,buildCommand: string, startCommand: string, outputDir: string, projectType: string, packageInstallerCommand: string };
try {
const repoId = req.repoId;
let p = spawn({
cmd: req.packageInstallerCommand.split(" "),
cwd: `./cloned-repo/${repoId}`,
stdout: "pipe",
stderr: "pipe"
});
for await (const chunk of p.stdout) {
console.log(chunk.toString());
}
let errorOutput = "";
for await (const chunk of p.stderr) {
errorOutput += chunk.toString();
}
if (errorOutput) {
console.log("Error during npm install:", errorOutput);
//fs.rmdirSync(`./cloned-repo/${repoId}`, { recursive: true });
set.status = StatusCode.INTERNAL_SERVER_ERROR
return {
message: "Error during npm install",
error: errorOutput
}
}
p = spawn({
cmd: req.buildCommand.split(" "),
cwd: `./cloned-repo/${repoId}`,
stdout: "pipe",
stderr: "pipe"
});
for await (const chunk of p.stdout) {
console.log(chunk.toString());
}
errorOutput = "";
for await (const chunk of p.stderr) {
errorOutput += chunk.toString();
}
if (errorOutput) {
console.log("Error during build:", errorOutput);
//fs.rmdirSync(`./cloned-repo/${repoId}`, { recursive: true });
set.status = StatusCode.INTERNAL_SERVER_ERROR
return {
message: "Error during build",
error: errorOutput
}
}
if (fs.existsSync(`./cloned-repo/${repoId}/${req.outputDir}`)) {
return {
message: "Project built successfully",
repoId: repoId,
outputDir: req.outputDir,
startCommand: req.startCommand
}
} else {
set.status = StatusCode.INTERNAL_SERVER_ERROR
//fs.rmdirSync(`./cloned-repo/${repoId}`, { recursive: true });
return {
message: "Build output directory does not exist.",
error: "Invalid output directory." + req.outputDir + " not found."
}
}
} catch (error) {
console.log("Error while building the project:");
//fs.rmdirSync(`./cloned-repo/${req.repoId}`, { recursive: true });
set.status = StatusCode.INTERNAL_SERVER_ERROR
return {
message: "Error while building the project",
error
}
}
}
export default buildRepoController;