Update server.js
Browse files
server.js
CHANGED
|
@@ -124,41 +124,62 @@ app.get("/logs", async (req, res) => {
|
|
| 124 |
});
|
| 125 |
|
| 126 |
// Endpoint for "Deploy Kaizen"
|
| 127 |
-
app.post("/deploy",
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
}
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
await fs.rmdir(userDir, { recursive: true }).catch(() => {});
|
| 138 |
-
await fs.mkdir(userDir, { recursive: true });
|
| 139 |
|
| 140 |
-
|
| 141 |
-
const cloneCommand = `git clone ${repoUrl} . && /usr/bin/yarn install && /usr/bin/yarn start`;
|
| 142 |
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
});
|
| 163 |
|
| 164 |
// Start the server
|
|
|
|
| 124 |
});
|
| 125 |
|
| 126 |
// Endpoint for "Deploy Kaizen"
|
| 127 |
+
app.post("/deploy", (req, res) => {
|
| 128 |
+
const { username, repoUrl } = req.body;
|
| 129 |
|
| 130 |
+
const userDir = path.join(usersDir, username);
|
| 131 |
+
if (!fs.existsSync(userDir)) fs.mkdirSync(userDir, { recursive: true });
|
|
|
|
| 132 |
|
| 133 |
+
// Clear the user's directory before deployment
|
| 134 |
+
fs.rmdirSync(userDir, { recursive: true });
|
| 135 |
+
fs.mkdirSync(userDir);
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
const cloneCommand = `git clone ${repoUrl} .`;
|
|
|
|
| 138 |
|
| 139 |
+
const child = exec(cloneCommand, { cwd: userDir });
|
| 140 |
|
| 141 |
+
child.stdout.on("data", (data) => {
|
| 142 |
+
appendLog(username, data.trim());
|
| 143 |
+
});
|
| 144 |
|
| 145 |
+
child.stderr.on("data", (data) => {
|
| 146 |
+
appendLog(username, data.trim());
|
| 147 |
+
});
|
| 148 |
|
| 149 |
+
child.on("close", async (code) => {
|
| 150 |
+
appendLog(username, `Clone process exited with code ${code}`);
|
| 151 |
+
if (code !== 0) {
|
| 152 |
+
res.status(500).send("Error cloning repository.");
|
| 153 |
+
return;
|
| 154 |
+
}
|
| 155 |
|
| 156 |
+
// Ensure package.json exists
|
| 157 |
+
if (!fs.existsSync(path.join(userDir, "package.json"))) {
|
| 158 |
+
appendLog(username, "No package.json found. Initializing a default one.");
|
| 159 |
+
execSync("yarn init -y", { cwd: userDir });
|
| 160 |
}
|
| 161 |
+
|
| 162 |
+
// Run yarn install and start
|
| 163 |
+
const installStartCommand = `yarn install && yarn start`;
|
| 164 |
+
const installChild = exec(installStartCommand, { cwd: userDir });
|
| 165 |
+
|
| 166 |
+
installChild.stdout.on("data", (data) => {
|
| 167 |
+
appendLog(username, data.trim());
|
| 168 |
+
});
|
| 169 |
+
|
| 170 |
+
installChild.stderr.on("data", (data) => {
|
| 171 |
+
appendLog(username, data.trim());
|
| 172 |
+
});
|
| 173 |
+
|
| 174 |
+
installChild.on("close", (installCode) => {
|
| 175 |
+
appendLog(username, `Install/Start process exited with code ${installCode}`);
|
| 176 |
+
if (installCode === 0) {
|
| 177 |
+
res.send("Deployment completed successfully.");
|
| 178 |
+
} else {
|
| 179 |
+
res.status(500).send("Error during deployment.");
|
| 180 |
+
}
|
| 181 |
+
});
|
| 182 |
+
});
|
| 183 |
});
|
| 184 |
|
| 185 |
// Start the server
|