code-mon-config 1.0.2 → 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.
Files changed (2) hide show
  1. package/index.js +224 -63
  2. package/package.json +11 -1
package/index.js CHANGED
@@ -4,112 +4,273 @@ const express = require("express");
4
4
  const fs = require("fs");
5
5
  const path = require("path");
6
6
  const cors = require("cors");
7
- const { exec } = require("child_process");
7
+ const { spawn } = require("child_process");
8
+ const open = require("open");
9
+ const WebSocket = require("ws");
8
10
 
9
11
  const app = express();
10
12
 
11
13
  app.use(cors());
12
14
  app.use(express.json());
13
15
 
14
- const PORT = process.env.PORT || 3000;
15
- const FILE_DIR = path.join(process.cwd(), "files");
16
+ const PORT = 3000;
17
+ const ROOT = process.cwd();
16
18
 
17
- if (!fs.existsSync(FILE_DIR)) {
18
- fs.mkdirSync(FILE_DIR);
19
- }
19
+ /* ---------------- PROCESS MANAGER ---------------- */
20
20
 
21
- /* CREATE FILE */
22
- app.post("/create", (req, res) => {
23
- const { filename } = req.body;
21
+ let processes = {};
22
+ let processId = 0;
24
23
 
25
- if (!filename) return res.status(400).json({ error: "filename required" });
24
+ /* ---------------- FILE TREE ---------------- */
26
25
 
27
- const filePath = path.join(FILE_DIR, filename);
26
+ function getTree(dir, base = "") {
28
27
 
29
- if (fs.existsSync(filePath)) {
30
- return res.status(400).json({ error: "File already exists" });
31
- }
28
+ let results = [];
29
+ const list = fs.readdirSync(dir);
32
30
 
33
- fs.writeFileSync(filePath, "");
31
+ list.forEach(file => {
34
32
 
35
- res.json({ success: true, filename });
36
- });
33
+ const full = path.join(dir, file);
34
+ const rel = path.join(base, file);
35
+ const stat = fs.statSync(full);
37
36
 
38
- /* SAVE FILE */
39
- app.post("/save", (req, res) => {
40
- const { filename, content } = req.body;
37
+ if (stat.isDirectory()) {
41
38
 
42
- if (!filename) return res.status(400).json({ error: "filename required" });
39
+ results.push(rel + "/");
40
+ results = results.concat(getTree(full, rel));
43
41
 
44
- const filePath = path.join(FILE_DIR, filename);
42
+ } else {
45
43
 
46
- fs.writeFileSync(filePath, content || "");
44
+ results.push(rel);
45
+
46
+ }
47
47
 
48
- res.json({ success: true });
49
48
  });
50
49
 
51
- /* LIST FILES */
50
+ return results;
51
+ }
52
+
53
+ /* ---------------- FILE APIs ---------------- */
54
+
52
55
  app.get("/list", (req, res) => {
53
- const files = fs.readdirSync(FILE_DIR);
54
- res.json(files);
56
+
57
+ try {
58
+
59
+ const tree = getTree(ROOT);
60
+
61
+ res.json({
62
+ root: ROOT,
63
+ files: tree
64
+ });
65
+
66
+ } catch (err) {
67
+
68
+ res.json({ error: err.message });
69
+
70
+ }
71
+
55
72
  });
56
73
 
57
- /* LOAD FILE */
58
74
  app.get("/load", (req, res) => {
59
- const filename = req.query.filename;
60
75
 
61
- if (!filename) return res.status(400).json({ error: "filename required" });
76
+ const filename = req.query.filename;
77
+ const filePath = path.join(ROOT, filename);
78
+
79
+ if (!fs.existsSync(filePath))
80
+ return res.json({ error: "File not found" });
81
+
82
+ const content = fs.readFileSync(filePath, "utf8");
83
+
84
+ res.json({ content });
62
85
 
63
- const filePath = path.join(FILE_DIR, filename);
86
+ });
87
+
88
+ app.post("/save", (req, res) => {
89
+
90
+ const { filename, content } = req.body;
64
91
 
65
- if (!fs.existsSync(filePath)) {
66
- return res.status(404).json({ error: "File not found" });
67
- }
92
+ const filePath = path.join(ROOT, filename);
68
93
 
69
- const content = fs.readFileSync(filePath, "utf8");
94
+ fs.writeFileSync(filePath, content || "");
95
+
96
+ res.json({ success: true });
97
+
98
+ });
99
+
100
+ app.post("/create", (req, res) => {
101
+
102
+ const { filename } = req.body;
103
+
104
+ const filePath = path.join(ROOT, filename);
105
+
106
+ fs.writeFileSync(filePath, "");
107
+
108
+ res.json({ success: true });
70
109
 
71
- res.json({ content });
72
110
  });
73
111
 
74
- /* DELETE FILE */
75
112
  app.delete("/delete", (req, res) => {
76
- const { filename } = req.body;
77
113
 
78
- const filePath = path.join(FILE_DIR, filename);
114
+ const { filename } = req.body;
115
+
116
+ const filePath = path.join(ROOT, filename);
79
117
 
80
- if (fs.existsSync(filePath)) {
81
- fs.unlinkSync(filePath);
82
- }
118
+ if (fs.existsSync(filePath)) {
119
+ fs.unlinkSync(filePath);
120
+ }
121
+
122
+ res.json({ success: true });
83
123
 
84
- res.json({ success: true });
85
124
  });
86
125
 
87
- /* EXECUTE COMMAND */
126
+ /* ---------------- EXECUTE COMMAND ---------------- */
127
+
88
128
  app.post("/execute", (req, res) => {
89
- const { command } = req.body;
90
129
 
91
- if (!command) return res.status(400).json({ error: "command required" });
130
+ const { command } = req.body;
131
+
132
+ if (!command) {
133
+ return res.json({ error: "Command required" });
134
+ }
135
+
136
+ const parts = command.split(" ");
137
+
138
+ const proc = spawn(parts[0], parts.slice(1), {
139
+ cwd: ROOT,
140
+ shell: true
141
+ });
142
+
143
+ const id = processId++;
144
+
145
+ processes[id] = proc;
146
+
147
+ proc.stdout.on("data", data => {
148
+ console.log(`[${id}]`, data.toString());
149
+ });
150
+
151
+ proc.stderr.on("data", data => {
152
+ console.error(`[${id}]`, data.toString());
153
+ });
154
+
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
+ });
197
+
198
+ });
199
+
200
+ /* ---------------- START SERVER ---------------- */
201
+
202
+ const server = app.listen(PORT, async () => {
203
+
204
+ console.log("\nšŸš€ Code Mon Config Starting...\n");
205
+
206
+ console.log("šŸ“‚ Connected Folder:", ROOT);
207
+
208
+ console.log("\nāœ… Your terminal has been connected with Code Mon Code Space\n");
209
+
210
+ const url = "https://your-domain.com";
211
+
212
+ console.log("🌐 Opening:", url);
213
+
214
+ try {
215
+
216
+ await open(url);
217
+
218
+ } catch {
219
+
220
+ console.log("Open manually:", url);
221
+
222
+ }
223
+
224
+ });
225
+
226
+ /* ---------------- WEBSOCKET TERMINAL ---------------- */
227
+
228
+ const wss = new WebSocket.Server({ server });
229
+
230
+ wss.on("connection", ws => {
231
+
232
+ console.log("🟢 Terminal connected");
233
+
234
+ let proc;
235
+
236
+ ws.on("message", msg => {
237
+
238
+ const command = msg.toString();
239
+
240
+ if (!proc) {
241
+
242
+ proc = spawn(command, {
243
+ cwd: ROOT,
244
+ shell: true
245
+ });
246
+
247
+ proc.stdout.on("data", data => {
248
+ ws.send(data.toString());
249
+ });
250
+
251
+ proc.stderr.on("data", data => {
252
+ ws.send(data.toString());
253
+ });
254
+
255
+ proc.on("close", code => {
256
+ ws.send(`\nProcess exited with code ${code}\n`);
257
+ proc = null;
258
+ });
259
+
260
+ } else {
261
+
262
+ proc.stdin.write(command);
263
+
264
+ }
265
+
266
+ });
92
267
 
93
- exec(command, (error, stdout, stderr) => {
268
+ ws.on("close", () => {
94
269
 
95
- if (error) {
96
- return res.json({
97
- success: false,
98
- error: error.message,
99
- stdout,
100
- stderr
101
- });
102
- }
270
+ if (proc) proc.kill();
103
271
 
104
- res.json({
105
- success: true,
106
- stdout,
107
- stderr
108
- });
272
+ console.log("šŸ”“ Terminal disconnected");
109
273
 
110
- });
111
274
  });
112
275
 
113
- app.listen(PORT, () => {
114
- console.log(`šŸš€ CodeMon Config running on http://localhost:${PORT}`);
115
276
  });
package/package.json CHANGED
@@ -1,8 +1,18 @@
1
1
  {
2
2
  "name": "code-mon-config",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
+ "description": "Code Mon Code Space CLI",
4
5
  "main": "index.js",
5
6
  "bin": {
6
7
  "code-mon-config": "./index.js"
8
+ },
9
+ "scripts": {
10
+ "start": "node index.js"
11
+ },
12
+ "dependencies": {
13
+ "cors": "^2.8.6",
14
+ "express": "^4.22.1",
15
+ "open": "^10.2.0",
16
+ "ws": "^8.19.0"
7
17
  }
8
18
  }