code-mon-config 1.0.0 → 1.0.1

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 +125 -113
  2. package/package.json +4 -3
package/index.js CHANGED
@@ -1,155 +1,167 @@
1
- #!/usr/bin/env node
1
+ const express = require("express");
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+ const cors = require("cors");
5
+ const { exec } = require("child_process");
2
6
 
3
- const express = require("express")
4
- const cors = require("cors")
5
- const fs = require("fs")
6
- const path = require("path")
7
- const { exec } = require("child_process")
7
+ const app = express();
8
8
 
9
- const app = express()
10
- const PORT = 3210
9
+ app.use(cors());
10
+ app.use(express.json());
11
11
 
12
- // workspace folder
13
- const WORKSPACE = path.join(process.cwd(), "codemon-workspace")
12
+ const PORT = 3000;
13
+ const FILE_DIR = path.join(process.cwd(), "files");
14
14
 
15
- if (!fs.existsSync(WORKSPACE)) {
16
- fs.mkdirSync(WORKSPACE)
15
+ // create files directory if not exists
16
+ if (!fs.existsSync(FILE_DIR)) {
17
+ fs.mkdirSync(FILE_DIR);
17
18
  }
18
19
 
19
- app.use(express.json())
20
+ /*
21
+ CREATE FILE
22
+ POST /create
23
+ */
24
+ app.post("/create", (req, res) => {
25
+ const { filename } = req.body;
20
26
 
21
- // allow requests from any website (you can restrict later)
22
- app.use(cors({
23
- origin: "*"
24
- }))
25
-
26
- // health check
27
- app.get("/", (req, res) => {
28
- res.json({
29
- name: "code-mon-config",
30
- status: "running",
31
- workspace: WORKSPACE
32
- })
33
- })
27
+ if (!filename) {
28
+ return res.status(400).json({ error: "filename required" });
29
+ }
34
30
 
31
+ const filePath = path.join(FILE_DIR, filename);
35
32
 
36
- // 📂 LIST FILES
37
- app.get("/list", (req, res) => {
33
+ if (fs.existsSync(filePath)) {
34
+ return res.status(400).json({ error: "File already exists" });
35
+ }
38
36
 
39
- fs.readdir(WORKSPACE, (err, files) => {
37
+ fs.writeFileSync(filePath, "");
40
38
 
41
- if (err) {
42
- return res.status(500).json({ error: err.message })
43
- }
39
+ res.json({
40
+ success: true,
41
+ message: "File created",
42
+ filename
43
+ });
44
+ });
45
+
46
+ /*
47
+ SAVE FILE
48
+ POST /save
49
+ */
50
+ app.post("/save", (req, res) => {
51
+ const { filename, content } = req.body;
44
52
 
45
- res.json({ files })
53
+ if (!filename) {
54
+ return res.status(400).json({ error: "filename required" });
55
+ }
46
56
 
47
- })
57
+ const filePath = path.join(FILE_DIR, filename);
48
58
 
49
- })
59
+ fs.writeFileSync(filePath, content || "");
50
60
 
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);
51
73
 
52
- // 📄 LOAD FILE
74
+ res.json({
75
+ success: true,
76
+ files
77
+ });
78
+ });
79
+
80
+ /*
81
+ LOAD FILE CONTENT
82
+ GET /load?filename=test.js
83
+ */
53
84
  app.get("/load", (req, res) => {
85
+ const filename = req.query.filename;
54
86
 
55
- const file = req.query.file
56
-
57
- if (!file) {
58
- return res.status(400).json({ error: "file parameter required" })
87
+ if (!filename) {
88
+ return res.status(400).json({ error: "filename required" });
59
89
  }
60
90
 
61
- const filePath = path.join(WORKSPACE, file)
62
-
63
- fs.readFile(filePath, "utf8", (err, data) => {
64
-
65
- if (err) {
66
- return res.status(404).json({ error: "file not found" })
67
- }
68
-
69
- res.json({ content: data })
70
-
71
- })
72
-
73
- })
74
-
75
-
76
- // 💾 SAVE FILE
77
- app.post("/save", (req, res) => {
78
-
79
- const { file, content } = req.body
91
+ const filePath = path.join(FILE_DIR, filename);
80
92
 
81
- if (!file) {
82
- return res.status(400).json({ error: "file required" })
93
+ if (!fs.existsSync(filePath)) {
94
+ return res.status(404).json({ error: "File not found" });
83
95
  }
84
96
 
85
- const filePath = path.join(WORKSPACE, file)
86
-
87
- fs.writeFile(filePath, content || "", err => {
88
-
89
- if (err) {
90
- return res.status(500).json({ error: err.message })
91
- }
92
-
93
- res.json({ status: "saved" })
94
-
95
- })
97
+ const content = fs.readFileSync(filePath, "utf8");
96
98
 
97
- })
98
-
99
-
100
- // 🗑 DELETE FILE
99
+ res.json({
100
+ success: true,
101
+ filename,
102
+ content
103
+ });
104
+ });
105
+
106
+ /*
107
+ DELETE FILE
108
+ DELETE /delete
109
+ */
101
110
  app.delete("/delete", (req, res) => {
111
+ const { filename } = req.body;
102
112
 
103
- const { file } = req.body
104
-
105
- if (!file) {
106
- return res.status(400).json({ error: "file required" })
113
+ if (!filename) {
114
+ return res.status(400).json({ error: "filename required" });
107
115
  }
108
116
 
109
- const filePath = path.join(WORKSPACE, file)
110
-
111
- fs.unlink(filePath, err => {
117
+ const filePath = path.join(FILE_DIR, filename);
112
118
 
113
- if (err) {
114
- return res.status(404).json({ error: "file not found" })
115
- }
116
-
117
- res.json({ status: "deleted" })
118
-
119
- })
120
-
121
- })
119
+ if (!fs.existsSync(filePath)) {
120
+ return res.status(404).json({ error: "File not found" });
121
+ }
122
122
 
123
+ fs.unlinkSync(filePath);
123
124
 
124
- // ⚡ EXECUTE COMMAND
125
+ res.json({
126
+ success: true,
127
+ message: "File deleted"
128
+ });
129
+ });
130
+
131
+ /*
132
+ EXECUTE COMMAND
133
+ POST /execute
134
+ */
125
135
  app.post("/execute", (req, res) => {
126
-
127
- const { command } = req.body
136
+ const { command } = req.body;
128
137
 
129
138
  if (!command) {
130
- return res.status(400).json({ error: "command required" })
139
+ return res.status(400).json({ error: "command required" });
131
140
  }
132
141
 
133
- exec(command, { cwd: WORKSPACE }, (err, stdout, stderr) => {
142
+ exec(command, { cwd: process.cwd() }, (error, stdout, stderr) => {
134
143
 
135
- if (err) {
136
- return res.json({ error: stderr })
144
+ if (error) {
145
+ return res.json({
146
+ success: false,
147
+ error: error.message,
148
+ stdout,
149
+ stderr
150
+ });
137
151
  }
138
152
 
139
- res.json({ output: stdout })
140
-
141
- })
142
-
143
- })
153
+ res.json({
154
+ success: true,
155
+ stdout,
156
+ stderr
157
+ });
144
158
 
159
+ });
160
+ });
145
161
 
146
- // start server
162
+ /*
163
+ START SERVER
164
+ */
147
165
  app.listen(PORT, () => {
148
-
149
- console.log("")
150
- console.log("🚀 CodeMon Config Running")
151
- console.log(`📡 Local API: http://localhost:${PORT}`)
152
- console.log(`📂 Workspace: ${WORKSPACE}`)
153
- console.log("")
154
-
155
- })
166
+ console.log(`🚀 CodeMon Config running at http://localhost:${PORT}`);
167
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-mon-config",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Local configuration and terminal API for CodeMon",
5
5
  "bin": {
6
6
  "code-mon-config": "./index.js"
@@ -17,8 +17,9 @@
17
17
  "author": "Your Name",
18
18
  "license": "MIT",
19
19
  "dependencies": {
20
- "cors": "^2.8.5",
21
- "express": "^4.22.1"
20
+ "cors": "^2.8.6",
21
+ "express": "^4.22.1",
22
+ "ws": "^8.19.0"
22
23
  },
23
24
  "engines": {
24
25
  "node": ">=16"