code-mon-config 1.0.8 ā 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +37 -79
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -12,6 +12,7 @@ const app = express();
|
|
|
12
12
|
|
|
13
13
|
app.use(cors());
|
|
14
14
|
app.use(express.json());
|
|
15
|
+
app.use(express.static(process.cwd()));
|
|
15
16
|
|
|
16
17
|
const PORT = 3000;
|
|
17
18
|
const ROOT = process.cwd();
|
|
@@ -25,17 +26,14 @@ let processId = 0;
|
|
|
25
26
|
|
|
26
27
|
function safePath(filename) {
|
|
27
28
|
|
|
28
|
-
if (!filename)
|
|
29
|
-
throw new Error("Filename required");
|
|
30
|
-
}
|
|
29
|
+
if (!filename) throw new Error("Filename required");
|
|
31
30
|
|
|
32
31
|
const resolved = path.join(ROOT, filename);
|
|
33
32
|
|
|
34
|
-
if (!resolved.startsWith(ROOT))
|
|
35
|
-
throw new Error("Invalid path");
|
|
36
|
-
}
|
|
33
|
+
if (!resolved.startsWith(ROOT)) throw new Error("Invalid path");
|
|
37
34
|
|
|
38
35
|
return resolved;
|
|
36
|
+
|
|
39
37
|
}
|
|
40
38
|
|
|
41
39
|
/* ---------------- FILE TREE ---------------- */
|
|
@@ -65,6 +63,7 @@ function getTree(dir, base = "") {
|
|
|
65
63
|
});
|
|
66
64
|
|
|
67
65
|
return results;
|
|
66
|
+
|
|
68
67
|
}
|
|
69
68
|
|
|
70
69
|
/* ---------------- FILE APIs ---------------- */
|
|
@@ -97,9 +96,8 @@ app.get("/load", (req, res) => {
|
|
|
97
96
|
const filename = req.query.filename;
|
|
98
97
|
const filePath = safePath(filename);
|
|
99
98
|
|
|
100
|
-
if (!fs.existsSync(filePath))
|
|
99
|
+
if (!fs.existsSync(filePath))
|
|
101
100
|
return res.json({ error: "File not found" });
|
|
102
|
-
}
|
|
103
101
|
|
|
104
102
|
const content = fs.readFileSync(filePath, "utf8");
|
|
105
103
|
|
|
@@ -167,9 +165,7 @@ app.delete("/delete", (req, res) => {
|
|
|
167
165
|
|
|
168
166
|
const filePath = safePath(filename);
|
|
169
167
|
|
|
170
|
-
if (fs.existsSync(filePath))
|
|
171
|
-
fs.unlinkSync(filePath);
|
|
172
|
-
}
|
|
168
|
+
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
|
|
173
169
|
|
|
174
170
|
res.json({ success: true });
|
|
175
171
|
|
|
@@ -189,40 +185,34 @@ app.post("/execute", (req, res) => {
|
|
|
189
185
|
|
|
190
186
|
const { command } = req.body;
|
|
191
187
|
|
|
192
|
-
if (!command) {
|
|
193
|
-
return res.json({ error: "Command required" });
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const parts = command.split(" ");
|
|
188
|
+
if (!command) return res.json({ error: "Command required" });
|
|
197
189
|
|
|
198
|
-
const proc = spawn(
|
|
190
|
+
const proc = spawn(command, {
|
|
199
191
|
cwd: ROOT,
|
|
200
192
|
shell: true
|
|
201
193
|
});
|
|
202
194
|
|
|
203
195
|
const id = processId++;
|
|
204
|
-
|
|
205
196
|
processes[id] = proc;
|
|
206
197
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
});
|
|
198
|
+
let output = "";
|
|
199
|
+
let error = "";
|
|
210
200
|
|
|
211
|
-
proc.
|
|
212
|
-
|
|
213
|
-
});
|
|
201
|
+
proc.stdout.on("data", d => output += d.toString());
|
|
202
|
+
proc.stderr.on("data", d => error += d.toString());
|
|
214
203
|
|
|
215
204
|
proc.on("close", code => {
|
|
216
205
|
|
|
217
206
|
delete processes[id];
|
|
218
207
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
208
|
+
res.json({
|
|
209
|
+
success: true,
|
|
210
|
+
processId: id,
|
|
211
|
+
code,
|
|
212
|
+
output,
|
|
213
|
+
error
|
|
214
|
+
});
|
|
222
215
|
|
|
223
|
-
res.json({
|
|
224
|
-
success: true,
|
|
225
|
-
processId: id
|
|
226
216
|
});
|
|
227
217
|
|
|
228
218
|
} catch (err) {
|
|
@@ -237,9 +227,7 @@ app.post("/execute", (req, res) => {
|
|
|
237
227
|
|
|
238
228
|
app.get("/processes", (req, res) => {
|
|
239
229
|
|
|
240
|
-
const list = Object.keys(processes).map(id => ({
|
|
241
|
-
id
|
|
242
|
-
}));
|
|
230
|
+
const list = Object.keys(processes).map(id => ({ id }));
|
|
243
231
|
|
|
244
232
|
res.json(list);
|
|
245
233
|
|
|
@@ -253,12 +241,10 @@ app.post("/stop-process", (req, res) => {
|
|
|
253
241
|
|
|
254
242
|
const { id } = req.body;
|
|
255
243
|
|
|
256
|
-
if (!processes[id])
|
|
244
|
+
if (!processes[id])
|
|
257
245
|
return res.json({ error: "Process not found" });
|
|
258
|
-
}
|
|
259
246
|
|
|
260
247
|
processes[id].kill();
|
|
261
|
-
|
|
262
248
|
delete processes[id];
|
|
263
249
|
|
|
264
250
|
res.json({
|
|
@@ -295,7 +281,7 @@ if (fs.existsSync(EXTENSIONS_DIR)) {
|
|
|
295
281
|
|
|
296
282
|
console.log(`š Loaded extension: ${file}`);
|
|
297
283
|
|
|
298
|
-
} catch
|
|
284
|
+
} catch {
|
|
299
285
|
|
|
300
286
|
console.log(`ā Failed to load extension: ${file}`);
|
|
301
287
|
|
|
@@ -312,23 +298,16 @@ if (fs.existsSync(EXTENSIONS_DIR)) {
|
|
|
312
298
|
const server = app.listen(PORT, async () => {
|
|
313
299
|
|
|
314
300
|
console.log("\nš Code Mon Config Starting...\n");
|
|
315
|
-
|
|
316
301
|
console.log("š Connected Folder:", ROOT);
|
|
317
302
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
const url = "http://localhost:3000";
|
|
303
|
+
const url = "http://code-mon.pages.dev/iDE";
|
|
321
304
|
|
|
322
305
|
console.log("š Opening:", url);
|
|
323
306
|
|
|
324
307
|
try {
|
|
325
|
-
|
|
326
308
|
await open(url);
|
|
327
|
-
|
|
328
309
|
} catch {
|
|
329
|
-
|
|
330
310
|
console.log("Open manually:", url);
|
|
331
|
-
|
|
332
311
|
}
|
|
333
312
|
|
|
334
313
|
});
|
|
@@ -341,47 +320,26 @@ wss.on("connection", ws => {
|
|
|
341
320
|
|
|
342
321
|
console.log("š¢ Terminal connected");
|
|
343
322
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const command = msg.toString();
|
|
349
|
-
|
|
350
|
-
if (!proc) {
|
|
351
|
-
|
|
352
|
-
proc = spawn(command, {
|
|
353
|
-
cwd: ROOT,
|
|
354
|
-
shell: true
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
proc.stdout.on("data", data => {
|
|
358
|
-
ws.send(data.toString());
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
proc.stderr.on("data", data => {
|
|
362
|
-
ws.send(data.toString());
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
proc.on("close", code => {
|
|
366
|
-
|
|
367
|
-
ws.send(`\nProcess exited with code ${code}\n`);
|
|
368
|
-
|
|
369
|
-
proc = null;
|
|
370
|
-
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
} else {
|
|
323
|
+
// PTY-like shell using script
|
|
324
|
+
const shell = spawn("script", ["-q", "/dev/null", "bash"], {
|
|
325
|
+
cwd: ROOT
|
|
326
|
+
});
|
|
374
327
|
|
|
375
|
-
|
|
328
|
+
shell.stdout.on("data", data => {
|
|
329
|
+
ws.send(data.toString());
|
|
330
|
+
});
|
|
376
331
|
|
|
377
|
-
|
|
332
|
+
shell.stderr.on("data", data => {
|
|
333
|
+
ws.send(data.toString());
|
|
334
|
+
});
|
|
378
335
|
|
|
336
|
+
ws.on("message", msg => {
|
|
337
|
+
shell.stdin.write(msg);
|
|
379
338
|
});
|
|
380
339
|
|
|
381
340
|
ws.on("close", () => {
|
|
382
341
|
|
|
383
|
-
|
|
384
|
-
|
|
342
|
+
shell.kill();
|
|
385
343
|
console.log("š“ Terminal disconnected");
|
|
386
344
|
|
|
387
345
|
});
|