local-cmd-runner 1.0.1 → 1.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "local-cmd-runner",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Run local shell commands from a web page",
5
5
  "homepage": "https://github.com/codewoow/local_cmd#readme",
6
6
  "bugs": {
package/public/app.js CHANGED
@@ -1,4 +1,4 @@
1
- const socket = io();
1
+ const socket = io({ path: '/cmd/socket.io' });
2
2
 
3
3
  const cmdInput = document.getElementById('cmdInput');
4
4
  const runBtn = document.getElementById('runBtn');
package/public/index.html CHANGED
@@ -45,7 +45,7 @@
45
45
  </div>
46
46
  </div>
47
47
 
48
- <script src="/socket.io/socket.io.js"></script>
48
+ <script src="/cmd/socket.io/socket.io.js"></script>
49
49
  <script src="app.js"></script>
50
50
  </body>
51
51
 
package/server.js CHANGED
@@ -7,38 +7,39 @@ const os = require('os');
7
7
 
8
8
  const app = express();
9
9
  const httpServer = createServer(app);
10
- const io = new Server(httpServer);
10
+ const io = new Server(httpServer, { path: '/cmd/socket.io' });
11
11
 
12
- const PORT = process.env.PORT || 3000;
12
+ const PORT = process.env.PORT || 3500;
13
13
 
14
- app.use(express.static(path.join(__dirname, 'public')));
14
+ app.use('/cmd', express.static(path.join(__dirname, 'public')));
15
+ app.get('/', (req, res) => res.redirect('/cmd/'));
15
16
 
16
17
  io.on('connection', (socket) => {
17
18
  console.log('Client connected');
18
-
19
+
19
20
  socket.on('run_command', (payload) => {
20
21
  // If the old app.js sends a string, handle it. Otherwise handle the new object format.
21
22
  const cmd = typeof payload === 'string' ? payload : payload.cmd;
22
23
  const runId = typeof payload === 'string' ? cmd : payload.runId;
23
24
  console.log(`Running command: ${cmd}`);
24
-
25
+
25
26
  // execute the command inside the native OS shell
26
27
  const child = spawn(cmd, [], { shell: true });
27
-
28
+
28
29
  socket.emit('cmd_start', { id: cmd, runId: runId });
29
30
 
30
31
  child.stdout.on('data', (data) => {
31
32
  socket.emit('cmd_output', { type: 'stdout', data: data.toString(), id: cmd, runId: runId });
32
33
  });
33
-
34
+
34
35
  child.stderr.on('data', (data) => {
35
36
  socket.emit('cmd_output', { type: 'stderr', data: data.toString(), id: cmd, runId: runId });
36
37
  });
37
-
38
+
38
39
  child.on('close', (code) => {
39
40
  socket.emit('cmd_close', { code, id: cmd, runId: runId });
40
41
  });
41
-
42
+
42
43
  child.on('error', (err) => {
43
44
  socket.emit('cmd_error', { error: err.toString(), id: cmd, runId: runId });
44
45
  });
@@ -46,9 +47,9 @@ io.on('connection', (socket) => {
46
47
  });
47
48
 
48
49
  httpServer.listen(PORT, () => {
49
- const url = `http://localhost:${PORT}`;
50
+ const url = `http://localhost:${PORT}/cmd/`;
50
51
  console.log(`Server is running at ${url}`);
51
-
52
+
52
53
  // Use platform-specific command to open the browser
53
54
  const startCmd = os.platform() === 'win32' ? 'start' : (os.platform() === 'darwin' ? 'open' : 'xdg-open');
54
55
  spawn(startCmd, [url], { shell: true });