code-mon-config 1.0.4 → 1.0.6

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.
Files changed (2) hide show
  1. package/index.js +144 -100
  2. 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
- let results = [];
24
- const list = fs.readdirSync(dir);
28
+ let results = [];
29
+ const list = fs.readdirSync(dir);
25
30
 
26
- list.forEach(file => {
31
+ list.forEach(file => {
27
32
 
28
- const full = path.join(dir, file);
29
- const rel = path.join(base, file);
30
- const stat = fs.statSync(full);
33
+ const full = path.join(dir, file);
34
+ const rel = path.join(base, file);
35
+ const stat = fs.statSync(full);
31
36
 
32
- if (stat.isDirectory()) {
37
+ if (stat.isDirectory()) {
33
38
 
34
- results.push(rel + "/");
35
- results = results.concat(getTree(full, rel));
39
+ results.push(rel + "/");
40
+ results = results.concat(getTree(full, rel));
36
41
 
37
- } else {
42
+ } else {
38
43
 
39
- results.push(rel);
44
+ results.push(rel);
40
45
 
41
- }
46
+ }
42
47
 
43
- });
48
+ });
44
49
 
45
- return results;
50
+ return results;
46
51
  }
47
52
 
48
53
  /* ---------------- FILE APIs ---------------- */
49
54
 
50
55
  app.get("/list", (req, res) => {
51
56
 
52
- try {
57
+ try {
53
58
 
54
- const tree = getTree(ROOT);
59
+ const tree = getTree(ROOT);
55
60
 
56
- res.json({
57
- root: ROOT,
58
- files: tree
59
- });
61
+ res.json({
62
+ root: ROOT,
63
+ files: tree
64
+ });
60
65
 
61
- } catch (err) {
66
+ } catch (err) {
62
67
 
63
- res.json({ error: err.message });
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
- const filename = req.query.filename;
72
- const filePath = path.join(ROOT, filename);
76
+ const filename = req.query.filename;
77
+ const filePath = path.join(ROOT, filename);
73
78
 
74
- if (!fs.existsSync(filePath))
75
- return res.json({ error: "File not found" });
79
+ if (!fs.existsSync(filePath))
80
+ return res.json({ error: "File not found" });
76
81
 
77
- const content = fs.readFileSync(filePath, "utf8");
82
+ const content = fs.readFileSync(filePath, "utf8");
78
83
 
79
- res.json({ content });
84
+ res.json({ content });
80
85
 
81
86
  });
82
87
 
83
88
  app.post("/save", (req, res) => {
84
89
 
85
- const { filename, content } = req.body;
90
+ const { filename, content } = req.body;
86
91
 
87
- const filePath = path.join(ROOT, filename);
92
+ const filePath = path.join(ROOT, filename);
88
93
 
89
- fs.writeFileSync(filePath, content || "");
94
+ fs.writeFileSync(filePath, content || "");
90
95
 
91
- res.json({ success: true });
96
+ res.json({ success: true });
92
97
 
93
98
  });
94
99
 
95
100
  app.post("/create", (req, res) => {
96
101
 
97
- const { filename } = req.body;
102
+ const { filename } = req.body;
98
103
 
99
- const filePath = path.join(ROOT, filename);
104
+ const filePath = path.join(ROOT, filename);
100
105
 
101
- fs.writeFileSync(filePath, "");
106
+ fs.writeFileSync(filePath, "");
102
107
 
103
- res.json({ success: true });
108
+ res.json({ success: true });
104
109
 
105
110
  });
106
111
 
107
112
  app.delete("/delete", (req, res) => {
108
113
 
109
- const { filename } = req.body;
114
+ const { filename } = req.body;
110
115
 
111
- const filePath = path.join(ROOT, filename);
116
+ const filePath = path.join(ROOT, filename);
112
117
 
113
- if (fs.existsSync(filePath)) {
114
-
115
- fs.unlinkSync(filePath);
116
-
117
- }
118
+ if (fs.existsSync(filePath)) {
119
+ fs.unlinkSync(filePath);
120
+ }
118
121
 
119
- res.json({ success: true });
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
- const { command } = req.body;
130
+ const { command } = req.body;
128
131
 
129
- if (!command) {
132
+ if (!command) {
133
+ return res.json({ error: "Command required" });
134
+ }
130
135
 
131
- return res.json({ error: "Command required" });
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
- const parts = command.split(" ");
143
+ const id = processId++;
136
144
 
137
- const proc = spawn(parts[0], parts.slice(1), {
138
- cwd: ROOT,
139
- shell: true
140
- });
145
+ processes[id] = proc;
141
146
 
142
- proc.stdout.on("data", data => {
143
- console.log(data.toString());
144
- });
147
+ proc.stdout.on("data", data => {
148
+ console.log(`[${id}]`, data.toString());
149
+ });
145
150
 
146
- proc.stderr.on("data", data => {
147
- console.error(data.toString());
148
- });
151
+ proc.stderr.on("data", data => {
152
+ console.error(`[${id}]`, data.toString());
153
+ });
149
154
 
150
- res.json({
151
- success: true
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
- console.log("\nšŸš€ Code Mon Config Starting...\n");
204
+ console.log("\nšŸš€ Code Mon Config Starting...\n");
161
205
 
162
- console.log("šŸ“‚ Connected Folder:", ROOT);
206
+ console.log("šŸ“‚ Connected Folder:", ROOT);
163
207
 
164
- console.log("\nāœ… Your terminal has been connected with Code Mon Code Space\n");
208
+ console.log("\nāœ… Your terminal has been connected with Code Mon Code Space\n");
165
209
 
166
- const url = "https://code-mon.pages.dev/code-mon-spacr";
210
+ const url = "https://code-mon.pages.dev/code-mon-spacr";
167
211
 
168
- console.log("🌐 Opening:", url);
212
+ console.log("🌐 Opening:", url);
169
213
 
170
- try {
214
+ try {
171
215
 
172
- await open(url);
216
+ await open(url);
173
217
 
174
- } catch {
218
+ } catch {
175
219
 
176
- console.log("Open manually:", url);
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
- console.log("🟢 Terminal connected");
232
+ console.log("🟢 Terminal connected");
189
233
 
190
- let proc;
234
+ let proc;
191
235
 
192
- ws.on("message", msg => {
236
+ ws.on("message", msg => {
193
237
 
194
- const command = msg.toString();
238
+ const command = msg.toString();
195
239
 
196
- if (!proc) {
240
+ if (!proc) {
197
241
 
198
- proc = spawn(command, {
199
- cwd: ROOT,
200
- shell: true
201
- });
242
+ proc = spawn(command, {
243
+ cwd: ROOT,
244
+ shell: true
245
+ });
202
246
 
203
- proc.stdout.on("data", data => {
204
- ws.send(data.toString());
205
- });
247
+ proc.stdout.on("data", data => {
248
+ ws.send(data.toString());
249
+ });
206
250
 
207
- proc.stderr.on("data", data => {
208
- ws.send(data.toString());
209
- });
251
+ proc.stderr.on("data", data => {
252
+ ws.send(data.toString());
253
+ });
210
254
 
211
- proc.on("close", code => {
212
- ws.send(`\nProcess exited with code ${code}\n`);
213
- proc = null;
214
- });
255
+ proc.on("close", code => {
256
+ ws.send(`\nProcess exited with code ${code}\n`);
257
+ proc = null;
258
+ });
215
259
 
216
- } else {
260
+ } else {
217
261
 
218
- proc.stdin.write(command);
262
+ proc.stdin.write(command);
219
263
 
220
- }
264
+ }
221
265
 
222
- });
266
+ });
223
267
 
224
- ws.on("close", () => {
268
+ ws.on("close", () => {
225
269
 
226
- if (proc) proc.kill();
270
+ if (proc) proc.kill();
227
271
 
228
- console.log("šŸ”“ Terminal disconnected");
272
+ console.log("šŸ”“ Terminal disconnected");
229
273
 
230
- });
274
+ });
231
275
 
232
276
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-mon-config",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Code Mon Code Space CLI",
5
5
  "main": "index.js",
6
6
  "bin": {