code-mon-config 1.0.2 → 1.0.4

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 +169 -52
  2. package/package.json +11 -1
package/index.js CHANGED
@@ -4,112 +4,229 @@ 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
+ /* ---------------- FILE TREE ---------------- */
20
+
21
+ function getTree(dir, base = "") {
22
+
23
+ let results = [];
24
+ const list = fs.readdirSync(dir);
25
+
26
+ list.forEach(file => {
27
+
28
+ const full = path.join(dir, file);
29
+ const rel = path.join(base, file);
30
+ const stat = fs.statSync(full);
31
+
32
+ if (stat.isDirectory()) {
33
+
34
+ results.push(rel + "/");
35
+ results = results.concat(getTree(full, rel));
36
+
37
+ } else {
38
+
39
+ results.push(rel);
40
+
41
+ }
42
+
43
+ });
44
+
45
+ return results;
19
46
  }
20
47
 
21
- /* CREATE FILE */
22
- app.post("/create", (req, res) => {
23
- const { filename } = req.body;
48
+ /* ---------------- FILE APIs ---------------- */
49
+
50
+ app.get("/list", (req, res) => {
24
51
 
25
- if (!filename) return res.status(400).json({ error: "filename required" });
52
+ try {
26
53
 
27
- const filePath = path.join(FILE_DIR, filename);
54
+ const tree = getTree(ROOT);
55
+
56
+ res.json({
57
+ root: ROOT,
58
+ files: tree
59
+ });
60
+
61
+ } catch (err) {
62
+
63
+ res.json({ error: err.message });
28
64
 
29
- if (fs.existsSync(filePath)) {
30
- return res.status(400).json({ error: "File already exists" });
31
65
  }
32
66
 
33
- fs.writeFileSync(filePath, "");
67
+ });
68
+
69
+ app.get("/load", (req, res) => {
70
+
71
+ const filename = req.query.filename;
72
+ const filePath = path.join(ROOT, filename);
73
+
74
+ if (!fs.existsSync(filePath))
75
+ return res.json({ error: "File not found" });
76
+
77
+ const content = fs.readFileSync(filePath, "utf8");
78
+
79
+ res.json({ content });
34
80
 
35
- res.json({ success: true, filename });
36
81
  });
37
82
 
38
- /* SAVE FILE */
39
83
  app.post("/save", (req, res) => {
40
- const { filename, content } = req.body;
41
84
 
42
- if (!filename) return res.status(400).json({ error: "filename required" });
85
+ const { filename, content } = req.body;
43
86
 
44
- const filePath = path.join(FILE_DIR, filename);
87
+ const filePath = path.join(ROOT, filename);
45
88
 
46
89
  fs.writeFileSync(filePath, content || "");
47
90
 
48
91
  res.json({ success: true });
49
- });
50
92
 
51
- /* LIST FILES */
52
- app.get("/list", (req, res) => {
53
- const files = fs.readdirSync(FILE_DIR);
54
- res.json(files);
55
93
  });
56
94
 
57
- /* LOAD FILE */
58
- app.get("/load", (req, res) => {
59
- const filename = req.query.filename;
95
+ app.post("/create", (req, res) => {
60
96
 
61
- if (!filename) return res.status(400).json({ error: "filename required" });
97
+ const { filename } = req.body;
62
98
 
63
- const filePath = path.join(FILE_DIR, filename);
99
+ const filePath = path.join(ROOT, filename);
64
100
 
65
- if (!fs.existsSync(filePath)) {
66
- return res.status(404).json({ error: "File not found" });
67
- }
101
+ fs.writeFileSync(filePath, "");
68
102
 
69
- const content = fs.readFileSync(filePath, "utf8");
103
+ res.json({ success: true });
70
104
 
71
- res.json({ content });
72
105
  });
73
106
 
74
- /* DELETE FILE */
75
107
  app.delete("/delete", (req, res) => {
108
+
76
109
  const { filename } = req.body;
77
110
 
78
- const filePath = path.join(FILE_DIR, filename);
111
+ const filePath = path.join(ROOT, filename);
79
112
 
80
113
  if (fs.existsSync(filePath)) {
114
+
81
115
  fs.unlinkSync(filePath);
116
+
82
117
  }
83
118
 
84
119
  res.json({ success: true });
120
+
85
121
  });
86
122
 
87
- /* EXECUTE COMMAND */
123
+ /* ---------------- EXECUTE COMMAND ---------------- */
124
+
88
125
  app.post("/execute", (req, res) => {
126
+
89
127
  const { command } = req.body;
90
128
 
91
- if (!command) return res.status(400).json({ error: "command required" });
129
+ if (!command) {
92
130
 
93
- exec(command, (error, stdout, stderr) => {
131
+ return res.json({ error: "Command required" });
94
132
 
95
- if (error) {
96
- return res.json({
97
- success: false,
98
- error: error.message,
99
- stdout,
100
- stderr
133
+ }
134
+
135
+ const parts = command.split(" ");
136
+
137
+ const proc = spawn(parts[0], parts.slice(1), {
138
+ cwd: ROOT,
139
+ shell: true
140
+ });
141
+
142
+ proc.stdout.on("data", data => {
143
+ console.log(data.toString());
144
+ });
145
+
146
+ proc.stderr.on("data", data => {
147
+ console.error(data.toString());
148
+ });
149
+
150
+ res.json({
151
+ success: true
152
+ });
153
+
154
+ });
155
+
156
+ /* ---------------- START SERVER ---------------- */
157
+
158
+ const server = app.listen(PORT, async () => {
159
+
160
+ console.log("\nšŸš€ Code Mon Config Starting...\n");
161
+
162
+ console.log("šŸ“‚ Connected Folder:", ROOT);
163
+
164
+ console.log("\nāœ… Your terminal has been connected with Code Mon Code Space\n");
165
+
166
+ const url = "https://code-mon.pages.dev/code-mon-spacr";
167
+
168
+ console.log("🌐 Opening:", url);
169
+
170
+ try {
171
+
172
+ await open(url);
173
+
174
+ } catch {
175
+
176
+ console.log("Open manually:", url);
177
+
178
+ }
179
+
180
+ });
181
+
182
+ /* ---------------- WEBSOCKET TERMINAL ---------------- */
183
+
184
+ const wss = new WebSocket.Server({ server });
185
+
186
+ wss.on("connection", ws => {
187
+
188
+ console.log("🟢 Terminal connected");
189
+
190
+ let proc;
191
+
192
+ ws.on("message", msg => {
193
+
194
+ const command = msg.toString();
195
+
196
+ if (!proc) {
197
+
198
+ proc = spawn(command, {
199
+ cwd: ROOT,
200
+ shell: true
201
+ });
202
+
203
+ proc.stdout.on("data", data => {
204
+ ws.send(data.toString());
205
+ });
206
+
207
+ proc.stderr.on("data", data => {
208
+ ws.send(data.toString());
209
+ });
210
+
211
+ proc.on("close", code => {
212
+ ws.send(`\nProcess exited with code ${code}\n`);
213
+ proc = null;
101
214
  });
215
+
216
+ } else {
217
+
218
+ proc.stdin.write(command);
219
+
102
220
  }
103
221
 
104
- res.json({
105
- success: true,
106
- stdout,
107
- stderr
108
- });
222
+ });
223
+
224
+ ws.on("close", () => {
225
+
226
+ if (proc) proc.kill();
227
+
228
+ console.log("šŸ”“ Terminal disconnected");
109
229
 
110
230
  });
111
- });
112
231
 
113
- app.listen(PORT, () => {
114
- console.log(`šŸš€ CodeMon Config running on http://localhost:${PORT}`);
115
232
  });
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.4",
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
  }