code-mon-config 1.0.1 ā 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.
- package/index.js +170 -105
- package/package.json +4 -13
package/index.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
1
3
|
const express = require("express");
|
|
2
4
|
const fs = require("fs");
|
|
3
5
|
const path = require("path");
|
|
4
6
|
const cors = require("cors");
|
|
5
|
-
const {
|
|
7
|
+
const { spawn } = require("child_process");
|
|
8
|
+
const open = require("open");
|
|
9
|
+
const WebSocket = require("ws");
|
|
6
10
|
|
|
7
11
|
const app = express();
|
|
8
12
|
|
|
@@ -10,158 +14,219 @@ app.use(cors());
|
|
|
10
14
|
app.use(express.json());
|
|
11
15
|
|
|
12
16
|
const PORT = 3000;
|
|
13
|
-
const
|
|
17
|
+
const ROOT = process.cwd();
|
|
18
|
+
|
|
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));
|
|
14
36
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
37
|
+
} else {
|
|
38
|
+
|
|
39
|
+
results.push(rel);
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return results;
|
|
18
46
|
}
|
|
19
47
|
|
|
20
|
-
/*
|
|
21
|
-
CREATE FILE
|
|
22
|
-
POST /create
|
|
23
|
-
*/
|
|
24
|
-
app.post("/create", (req, res) => {
|
|
25
|
-
const { filename } = req.body;
|
|
48
|
+
/* ---------------- FILE APIs ---------------- */
|
|
26
49
|
|
|
27
|
-
|
|
28
|
-
return res.status(400).json({ error: "filename required" });
|
|
29
|
-
}
|
|
50
|
+
app.get("/list", (req, res) => {
|
|
30
51
|
|
|
31
|
-
|
|
52
|
+
try {
|
|
53
|
+
|
|
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 });
|
|
32
64
|
|
|
33
|
-
if (fs.existsSync(filePath)) {
|
|
34
|
-
return res.status(400).json({ error: "File already exists" });
|
|
35
65
|
}
|
|
36
66
|
|
|
37
|
-
|
|
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 });
|
|
38
80
|
|
|
39
|
-
res.json({
|
|
40
|
-
success: true,
|
|
41
|
-
message: "File created",
|
|
42
|
-
filename
|
|
43
|
-
});
|
|
44
81
|
});
|
|
45
82
|
|
|
46
|
-
/*
|
|
47
|
-
SAVE FILE
|
|
48
|
-
POST /save
|
|
49
|
-
*/
|
|
50
83
|
app.post("/save", (req, res) => {
|
|
51
|
-
const { filename, content } = req.body;
|
|
52
84
|
|
|
53
|
-
|
|
54
|
-
return res.status(400).json({ error: "filename required" });
|
|
55
|
-
}
|
|
85
|
+
const { filename, content } = req.body;
|
|
56
86
|
|
|
57
|
-
const filePath = path.join(
|
|
87
|
+
const filePath = path.join(ROOT, filename);
|
|
58
88
|
|
|
59
89
|
fs.writeFileSync(filePath, content || "");
|
|
60
90
|
|
|
61
|
-
res.json({
|
|
62
|
-
success: true,
|
|
63
|
-
message: "File saved"
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
/*
|
|
68
|
-
LIST FILES
|
|
69
|
-
GET /list
|
|
70
|
-
*/
|
|
71
|
-
app.get("/list", (req, res) => {
|
|
72
|
-
const files = fs.readdirSync(FILE_DIR);
|
|
91
|
+
res.json({ success: true });
|
|
73
92
|
|
|
74
|
-
res.json({
|
|
75
|
-
success: true,
|
|
76
|
-
files
|
|
77
|
-
});
|
|
78
93
|
});
|
|
79
94
|
|
|
80
|
-
|
|
81
|
-
LOAD FILE CONTENT
|
|
82
|
-
GET /load?filename=test.js
|
|
83
|
-
*/
|
|
84
|
-
app.get("/load", (req, res) => {
|
|
85
|
-
const filename = req.query.filename;
|
|
95
|
+
app.post("/create", (req, res) => {
|
|
86
96
|
|
|
87
|
-
|
|
88
|
-
return res.status(400).json({ error: "filename required" });
|
|
89
|
-
}
|
|
97
|
+
const { filename } = req.body;
|
|
90
98
|
|
|
91
|
-
const filePath = path.join(
|
|
99
|
+
const filePath = path.join(ROOT, filename);
|
|
92
100
|
|
|
93
|
-
|
|
94
|
-
return res.status(404).json({ error: "File not found" });
|
|
95
|
-
}
|
|
101
|
+
fs.writeFileSync(filePath, "");
|
|
96
102
|
|
|
97
|
-
|
|
103
|
+
res.json({ success: true });
|
|
98
104
|
|
|
99
|
-
res.json({
|
|
100
|
-
success: true,
|
|
101
|
-
filename,
|
|
102
|
-
content
|
|
103
|
-
});
|
|
104
105
|
});
|
|
105
106
|
|
|
106
|
-
/*
|
|
107
|
-
DELETE FILE
|
|
108
|
-
DELETE /delete
|
|
109
|
-
*/
|
|
110
107
|
app.delete("/delete", (req, res) => {
|
|
108
|
+
|
|
111
109
|
const { filename } = req.body;
|
|
112
110
|
|
|
113
|
-
|
|
114
|
-
return res.status(400).json({ error: "filename required" });
|
|
115
|
-
}
|
|
111
|
+
const filePath = path.join(ROOT, filename);
|
|
116
112
|
|
|
117
|
-
|
|
113
|
+
if (fs.existsSync(filePath)) {
|
|
114
|
+
|
|
115
|
+
fs.unlinkSync(filePath);
|
|
118
116
|
|
|
119
|
-
if (!fs.existsSync(filePath)) {
|
|
120
|
-
return res.status(404).json({ error: "File not found" });
|
|
121
117
|
}
|
|
122
118
|
|
|
123
|
-
|
|
119
|
+
res.json({ success: true });
|
|
124
120
|
|
|
125
|
-
res.json({
|
|
126
|
-
success: true,
|
|
127
|
-
message: "File deleted"
|
|
128
|
-
});
|
|
129
121
|
});
|
|
130
122
|
|
|
131
|
-
/*
|
|
132
|
-
|
|
133
|
-
POST /execute
|
|
134
|
-
*/
|
|
123
|
+
/* ---------------- EXECUTE COMMAND ---------------- */
|
|
124
|
+
|
|
135
125
|
app.post("/execute", (req, res) => {
|
|
126
|
+
|
|
136
127
|
const { command } = req.body;
|
|
137
128
|
|
|
138
129
|
if (!command) {
|
|
139
|
-
|
|
130
|
+
|
|
131
|
+
return res.json({ error: "Command required" });
|
|
132
|
+
|
|
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
|
+
|
|
140
178
|
}
|
|
141
179
|
|
|
142
|
-
|
|
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
|
+
});
|
|
143
202
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
success: false,
|
|
147
|
-
error: error.message,
|
|
148
|
-
stdout,
|
|
149
|
-
stderr
|
|
203
|
+
proc.stdout.on("data", data => {
|
|
204
|
+
ws.send(data.toString());
|
|
150
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;
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
} else {
|
|
217
|
+
|
|
218
|
+
proc.stdin.write(command);
|
|
219
|
+
|
|
151
220
|
}
|
|
152
221
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
ws.on("close", () => {
|
|
225
|
+
|
|
226
|
+
if (proc) proc.kill();
|
|
227
|
+
|
|
228
|
+
console.log("š“ Terminal disconnected");
|
|
158
229
|
|
|
159
230
|
});
|
|
160
|
-
});
|
|
161
231
|
|
|
162
|
-
/*
|
|
163
|
-
START SERVER
|
|
164
|
-
*/
|
|
165
|
-
app.listen(PORT, () => {
|
|
166
|
-
console.log(`š CodeMon Config running at http://localhost:${PORT}`);
|
|
167
232
|
});
|
package/package.json
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "code-mon-config",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Code Mon Code Space CLI",
|
|
5
|
+
"main": "index.js",
|
|
5
6
|
"bin": {
|
|
6
7
|
"code-mon-config": "./index.js"
|
|
7
8
|
},
|
|
8
9
|
"scripts": {
|
|
9
10
|
"start": "node index.js"
|
|
10
11
|
},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"codemon",
|
|
13
|
-
"terminal",
|
|
14
|
-
"local-api",
|
|
15
|
-
"dev-tools"
|
|
16
|
-
],
|
|
17
|
-
"author": "Your Name",
|
|
18
|
-
"license": "MIT",
|
|
19
12
|
"dependencies": {
|
|
20
13
|
"cors": "^2.8.6",
|
|
21
14
|
"express": "^4.22.1",
|
|
15
|
+
"open": "^10.2.0",
|
|
22
16
|
"ws": "^8.19.0"
|
|
23
|
-
},
|
|
24
|
-
"engines": {
|
|
25
|
-
"node": ">=16"
|
|
26
17
|
}
|
|
27
18
|
}
|