code-mon-config 1.0.4 ā 1.0.5
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 +144 -100
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -16,107 +16,110 @@ app.use(express.json());
|
|
|
16
16
|
const PORT = 3000;
|
|
17
17
|
const ROOT = process.cwd();
|
|
18
18
|
|
|
19
|
+
/* ---------------- PROCESS MANAGER ---------------- */
|
|
20
|
+
|
|
21
|
+
let processes = {};
|
|
22
|
+
let processId = 0;
|
|
23
|
+
|
|
19
24
|
/* ---------------- FILE TREE ---------------- */
|
|
20
25
|
|
|
21
26
|
function getTree(dir, base = "") {
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
let results = [];
|
|
29
|
+
const list = fs.readdirSync(dir);
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
list.forEach(file => {
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
const full = path.join(dir, file);
|
|
34
|
+
const rel = path.join(base, file);
|
|
35
|
+
const stat = fs.statSync(full);
|
|
31
36
|
|
|
32
|
-
|
|
37
|
+
if (stat.isDirectory()) {
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
results.push(rel + "/");
|
|
40
|
+
results = results.concat(getTree(full, rel));
|
|
36
41
|
|
|
37
|
-
|
|
42
|
+
} else {
|
|
38
43
|
|
|
39
|
-
|
|
44
|
+
results.push(rel);
|
|
40
45
|
|
|
41
|
-
|
|
46
|
+
}
|
|
42
47
|
|
|
43
|
-
|
|
48
|
+
});
|
|
44
49
|
|
|
45
|
-
|
|
50
|
+
return results;
|
|
46
51
|
}
|
|
47
52
|
|
|
48
53
|
/* ---------------- FILE APIs ---------------- */
|
|
49
54
|
|
|
50
55
|
app.get("/list", (req, res) => {
|
|
51
56
|
|
|
52
|
-
|
|
57
|
+
try {
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
const tree = getTree(ROOT);
|
|
55
60
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
res.json({
|
|
62
|
+
root: ROOT,
|
|
63
|
+
files: tree
|
|
64
|
+
});
|
|
60
65
|
|
|
61
|
-
|
|
66
|
+
} catch (err) {
|
|
62
67
|
|
|
63
|
-
|
|
68
|
+
res.json({ error: err.message });
|
|
64
69
|
|
|
65
|
-
|
|
70
|
+
}
|
|
66
71
|
|
|
67
72
|
});
|
|
68
73
|
|
|
69
74
|
app.get("/load", (req, res) => {
|
|
70
75
|
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
const filename = req.query.filename;
|
|
77
|
+
const filePath = path.join(ROOT, filename);
|
|
73
78
|
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
if (!fs.existsSync(filePath))
|
|
80
|
+
return res.json({ error: "File not found" });
|
|
76
81
|
|
|
77
|
-
|
|
82
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
78
83
|
|
|
79
|
-
|
|
84
|
+
res.json({ content });
|
|
80
85
|
|
|
81
86
|
});
|
|
82
87
|
|
|
83
88
|
app.post("/save", (req, res) => {
|
|
84
89
|
|
|
85
|
-
|
|
90
|
+
const { filename, content } = req.body;
|
|
86
91
|
|
|
87
|
-
|
|
92
|
+
const filePath = path.join(ROOT, filename);
|
|
88
93
|
|
|
89
|
-
|
|
94
|
+
fs.writeFileSync(filePath, content || "");
|
|
90
95
|
|
|
91
|
-
|
|
96
|
+
res.json({ success: true });
|
|
92
97
|
|
|
93
98
|
});
|
|
94
99
|
|
|
95
100
|
app.post("/create", (req, res) => {
|
|
96
101
|
|
|
97
|
-
|
|
102
|
+
const { filename } = req.body;
|
|
98
103
|
|
|
99
|
-
|
|
104
|
+
const filePath = path.join(ROOT, filename);
|
|
100
105
|
|
|
101
|
-
|
|
106
|
+
fs.writeFileSync(filePath, "");
|
|
102
107
|
|
|
103
|
-
|
|
108
|
+
res.json({ success: true });
|
|
104
109
|
|
|
105
110
|
});
|
|
106
111
|
|
|
107
112
|
app.delete("/delete", (req, res) => {
|
|
108
113
|
|
|
109
|
-
|
|
114
|
+
const { filename } = req.body;
|
|
110
115
|
|
|
111
|
-
|
|
116
|
+
const filePath = path.join(ROOT, filename);
|
|
112
117
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
+
if (fs.existsSync(filePath)) {
|
|
119
|
+
fs.unlinkSync(filePath);
|
|
120
|
+
}
|
|
118
121
|
|
|
119
|
-
|
|
122
|
+
res.json({ success: true });
|
|
120
123
|
|
|
121
124
|
});
|
|
122
125
|
|
|
@@ -124,32 +127,73 @@ app.delete("/delete", (req, res) => {
|
|
|
124
127
|
|
|
125
128
|
app.post("/execute", (req, res) => {
|
|
126
129
|
|
|
127
|
-
|
|
130
|
+
const { command } = req.body;
|
|
128
131
|
|
|
129
|
-
|
|
132
|
+
if (!command) {
|
|
133
|
+
return res.json({ error: "Command required" });
|
|
134
|
+
}
|
|
130
135
|
|
|
131
|
-
|
|
136
|
+
const parts = command.split(" ");
|
|
132
137
|
|
|
133
|
-
|
|
138
|
+
const proc = spawn(parts[0], parts.slice(1), {
|
|
139
|
+
cwd: ROOT,
|
|
140
|
+
shell: true
|
|
141
|
+
});
|
|
134
142
|
|
|
135
|
-
|
|
143
|
+
const id = processId++;
|
|
136
144
|
|
|
137
|
-
|
|
138
|
-
cwd: ROOT,
|
|
139
|
-
shell: true
|
|
140
|
-
});
|
|
145
|
+
processes[id] = proc;
|
|
141
146
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
147
|
+
proc.stdout.on("data", data => {
|
|
148
|
+
console.log(`[${id}]`, data.toString());
|
|
149
|
+
});
|
|
145
150
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
151
|
+
proc.stderr.on("data", data => {
|
|
152
|
+
console.error(`[${id}]`, data.toString());
|
|
153
|
+
});
|
|
149
154
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
155
|
+
proc.on("close", code => {
|
|
156
|
+
delete processes[id];
|
|
157
|
+
console.log(`Process ${id} exited`);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
res.json({
|
|
161
|
+
success: true,
|
|
162
|
+
processId: id
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
/* ---------------- PROCESS LIST ---------------- */
|
|
168
|
+
|
|
169
|
+
app.get("/processes", (req, res) => {
|
|
170
|
+
|
|
171
|
+
const list = Object.keys(processes).map(id => ({
|
|
172
|
+
id: id
|
|
173
|
+
}));
|
|
174
|
+
|
|
175
|
+
res.json(list);
|
|
176
|
+
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
/* ---------------- STOP PROCESS ---------------- */
|
|
180
|
+
|
|
181
|
+
app.post("/stop-process", (req, res) => {
|
|
182
|
+
|
|
183
|
+
const { id } = req.body;
|
|
184
|
+
|
|
185
|
+
if (!processes[id]) {
|
|
186
|
+
return res.json({ error: "Process not found" });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
processes[id].kill();
|
|
190
|
+
|
|
191
|
+
delete processes[id];
|
|
192
|
+
|
|
193
|
+
res.json({
|
|
194
|
+
success: true,
|
|
195
|
+
message: "Process stopped"
|
|
196
|
+
});
|
|
153
197
|
|
|
154
198
|
});
|
|
155
199
|
|
|
@@ -157,25 +201,25 @@ app.post("/execute", (req, res) => {
|
|
|
157
201
|
|
|
158
202
|
const server = app.listen(PORT, async () => {
|
|
159
203
|
|
|
160
|
-
|
|
204
|
+
console.log("\nš Code Mon Config Starting...\n");
|
|
161
205
|
|
|
162
|
-
|
|
206
|
+
console.log("š Connected Folder:", ROOT);
|
|
163
207
|
|
|
164
|
-
|
|
208
|
+
console.log("\nā
Your terminal has been connected with Code Mon Code Space\n");
|
|
165
209
|
|
|
166
|
-
|
|
210
|
+
const url = "https://your-domain.com";
|
|
167
211
|
|
|
168
|
-
|
|
212
|
+
console.log("š Opening:", url);
|
|
169
213
|
|
|
170
|
-
|
|
214
|
+
try {
|
|
171
215
|
|
|
172
|
-
|
|
216
|
+
await open(url);
|
|
173
217
|
|
|
174
|
-
|
|
218
|
+
} catch {
|
|
175
219
|
|
|
176
|
-
|
|
220
|
+
console.log("Open manually:", url);
|
|
177
221
|
|
|
178
|
-
|
|
222
|
+
}
|
|
179
223
|
|
|
180
224
|
});
|
|
181
225
|
|
|
@@ -185,48 +229,48 @@ const wss = new WebSocket.Server({ server });
|
|
|
185
229
|
|
|
186
230
|
wss.on("connection", ws => {
|
|
187
231
|
|
|
188
|
-
|
|
232
|
+
console.log("š¢ Terminal connected");
|
|
189
233
|
|
|
190
|
-
|
|
234
|
+
let proc;
|
|
191
235
|
|
|
192
|
-
|
|
236
|
+
ws.on("message", msg => {
|
|
193
237
|
|
|
194
|
-
|
|
238
|
+
const command = msg.toString();
|
|
195
239
|
|
|
196
|
-
|
|
240
|
+
if (!proc) {
|
|
197
241
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
242
|
+
proc = spawn(command, {
|
|
243
|
+
cwd: ROOT,
|
|
244
|
+
shell: true
|
|
245
|
+
});
|
|
202
246
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
247
|
+
proc.stdout.on("data", data => {
|
|
248
|
+
ws.send(data.toString());
|
|
249
|
+
});
|
|
206
250
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
251
|
+
proc.stderr.on("data", data => {
|
|
252
|
+
ws.send(data.toString());
|
|
253
|
+
});
|
|
210
254
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
255
|
+
proc.on("close", code => {
|
|
256
|
+
ws.send(`\nProcess exited with code ${code}\n`);
|
|
257
|
+
proc = null;
|
|
258
|
+
});
|
|
215
259
|
|
|
216
|
-
|
|
260
|
+
} else {
|
|
217
261
|
|
|
218
|
-
|
|
262
|
+
proc.stdin.write(command);
|
|
219
263
|
|
|
220
|
-
|
|
264
|
+
}
|
|
221
265
|
|
|
222
|
-
|
|
266
|
+
});
|
|
223
267
|
|
|
224
|
-
|
|
268
|
+
ws.on("close", () => {
|
|
225
269
|
|
|
226
|
-
|
|
270
|
+
if (proc) proc.kill();
|
|
227
271
|
|
|
228
|
-
|
|
272
|
+
console.log("š“ Terminal disconnected");
|
|
229
273
|
|
|
230
|
-
|
|
274
|
+
});
|
|
231
275
|
|
|
232
276
|
});
|