@LukeZeller/

NBShell NodeJS Beta

Express

No description

fork
loading
Files
  • index.js
  • index.html
  • nbshell-old.js
  • package-lock.json
  • package.json
  • style.css
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var express = require('express'); 
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const fs = require('fs'); 
app.use(express.static(__dirname));
app.get('/', function(req, res){ 
  res.sendFile(__dirname + '/index.html'); 
});
io.on('connection', function(socket){ 
  console.log('[' + socket.id + ']: connected'); 
  socket.on('disconnect', function(){ 
    console.log('[' + socket.id + ']: disconnected'); 
  }); 
}); 
io.on('connection', function(socket){ 
  socket.on('input', function(input){ 
    interperet(input, socket.id); 
  }); 
  socket.on('log', function(log){ 
    console.log("[" + socket.id + "]: " + log); 
  });
}); 
io.on('connection', function(socket){ 
  io.to(socket.id).emit('writeLine', "NBShell JS v0.1"); 
  io.to(socket.id).emit('writeLine', "Copyright Luke Zeller 2019"); 
  io.to(socket.id).emit('reqInput'); 
}); 
http.listen(3000, function(){ 
  console.log('server started'); 
}); 
function interperet(input, socketId) { 
  var splitInput = input.split(" "); 
  //check string against commands 
  if (splitInput == "help") { 
    //write help menu 
    io.to(socketId).emit('writeLine', "Help"); 
    io.to(socketId).emit('writeLine', "help - displays the help screen"); 
    io.to(socketId).emit('writeLine', "print [text] - prints [text]"); 
    io.to(socketId).emit('writeLine', "log [text] - logs [text] to server"); 
    io.to(socketId).emit('writeLine', "clear - clears the screen"); 
    io.to(socketId).emit('writeLine', "read [file] - reads a [file]"); 
    io.to(socketId).emit('writeLine', "text-color [color] - sets text color to [color]"); 
    io.to(socketId).emit('writeLine', "bg-color [color] - sets background color to [color]"); 
    io.to(socketId).emit('writeLine', "font [font] - sets font to [font]"); 
    io.to(socketId).emit('reqInput'); 
    } 
    else if (splitInput[0] == "print") { 
      splitInput.shift(); 
      //print every item in splitInput 
      io.to(socketId).emit('writeLine', splitInput.join(" ")); 
      io.to(socketId).emit('reqInput'); 
    } 
    else if (splitInput[0] == "log") { 
      splitInput.shift(); 
      //log every item in splitInput
      console.log("[" + socketId + "]: " + splitInput.join(" ")); 
      io.to(socketId).emit('reqInput'); 
      } 
      else if (splitInput[0] == "clear") { 
        io.to(socketId).emit('clear'); 
        io.to(socketId).emit('reqInput'); 
      } 
      else if (splitInput[0] == "read") { 
        fs.readFile(splitInput[1], 'utf8', function(err, contents) { 
          if (err) { 
            io.to(socketId).emit('writeLine', err.toString()); 
          } 
          else { 
            io.to(socketId).emit('writeLine', contents); 
          } 
          io.to(socketId).emit('reqInput'); 
        }); 
      } 
      else if (splitInput[0] == "dir") { 
        fs.readdir(__dirname, function(err, items) { 
          io.to(socketId).emit('writeLine', "Index of: " + __dirname); 
          var i; 
          for (i = 0; i < items.length; i++) { 
            var stat = fs.statSync(items[i]) 
            if (stat.size < 1000) { 
              var sizeNum = stat.size; 
              var size = sizeNum + "B" 
            } 
            else if (stat.size > 1000) { 
              var sizeNum = Math.round(stat.size / 1000);
              var size = sizeNum + "KB" 
            }; 
            var type = "";
            var isFile = stat.isFile(); 
            if (stat.isDirectory() == true) { 
              type = "[dir]"; 
            } 
            else if (isFile == true) { 
              type = "[file]"; 
            } 
            else { 
              type = "[unknown]"; 
            }
            io.to(socketId).emit('writeLine', size + " - " + type + " - " + items[i]);
          } 
          io.to(socketId).emit('reqInput'); 
        }); 
      } 
    else if (splitInput[0] == "text-color") { 
      io.to(socketId).emit('text-color', splitInput[1]); 
      io.to(socketId).emit('reqInput'); 
    } 
    else if (splitInput[0] == "bg-color") { 
      io.to(socketId).emit('bg-color', splitInput[1]); 
      io.to(socketId).emit('reqInput'); 
    } 
    else if (splitInput[0] == "font") { 
      if (splitInput.length == 1) {
        io.to(socketId).emit('writeLine', "Availible fonts:"); 
        io.to(socketId).emit('writeLine', "Web-safe fonts (Arial, Helvetica, Courier)"); 
        io.to(socketId).emit('writeLine', "Ubuntu Mono (default)"); 
        io.to(socketId).emit('writeLine', "Source Code Pro"); 

      } else {
        splitInput.shift(); 
        io.to(socketId).emit('font', splitInput.join(" "));
      }
      io.to(socketId).emit('reqInput'); 
    } 
    else { 
      //tell the user their input is wrong 
      io.to(socketId).emit('writeLine', "Command '" + splitInput[0] + "' not found."); 
      io.to(socketId).emit('reqInput'); 
    } 
}