Reaperxxxx commited on
Commit
049a78a
·
verified ·
1 Parent(s): 2c0bd38

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +47 -26
server.js CHANGED
@@ -124,41 +124,62 @@ app.get("/logs", async (req, res) => {
124
  });
125
 
126
  // Endpoint for "Deploy Kaizen"
127
- app.post("/deploy", async (req, res) => {
128
- const { username, repoUrl } = req.body;
129
 
130
- if (!username || !repoUrl) {
131
- return res.status(400).send("Username and repository URL are required.");
132
- }
133
 
134
- const userDir = path.join(usersDir, username);
135
- try {
136
- // Clear user directory
137
- await fs.rmdir(userDir, { recursive: true }).catch(() => {});
138
- await fs.mkdir(userDir, { recursive: true });
139
 
140
- // Clone repository and install dependencies
141
- const cloneCommand = `git clone ${repoUrl} . && /usr/bin/yarn install && /usr/bin/yarn start`;
142
 
143
- const child = exec(cloneCommand, { cwd: userDir, shell: "/bin/bash" });
144
 
145
- child.stdout.on("data", (data) => {
146
- appendLog(username, `STDOUT: ${data.trim()}`);
147
- });
148
 
149
- child.stderr.on("data", (data) => {
150
- appendLog(username, `STDERR: ${data.trim()}`);
151
- });
152
 
153
- child.on("close", (code) => {
154
- appendLog(username, `Deploy process exited with code ${code}`);
155
- });
 
 
 
156
 
157
- res.send("Deployment started.");
158
- } catch (error) {
159
- console.error("Error during deployment:", error);
160
- res.status(500).send("Failed to start deployment.");
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