repl.it
@LukeZeller/

NBShell JS Beta

Express

No description

fork
loading
Files
  • index.js
  • bigimage.jpg
  • changelog.txt
  • favicon.ico
  • image.jpg
  • imagescr.nbs
  • index.html
  • nbshell-old.js
  • script.nbs
  • style.css
  • Packager files
  • package-lock.json
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//NBShell JS Server Copyright Luke Zeller 2019

//dependencies
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const fs = require('fs');
var lbl = require('line-by-line');

//init express
app.use(express.static(__dirname));

//send index.html by default
app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});

app.get('/favicon', function (req, res) {
  res.sendFile(__dirname + '/favicon.ico');
});

//on connect or diconnect, log
io.on('connection', function (socket) {
  console.log('[' + socket.id + ']: connected');
  socket.on('disconnect', function () {
    console.log('[' + socket.id + ']: disconnected');
  });
});

//while connected, do
io.on('connection', function (socket) {

  //when an input is sent, interperet it
  socket.on('input', function (input) {
    interperet(input, socket, false);
  });

  //when a log is called for, log the data
  socket.on('log', function (log) {
    console.log("[" + socket.id + "]: " + log);
  });

  //input requested
  socket.on('input', function (isScript) {
    console.log(isScript);
    reqInput(socket, isScript);
  });
});

//on connection, send start text
io.on('connection', function (socket) {
  io.to(socket.id).emit('clear');
  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');
});

//start server
http.listen(3000, function () {
  console.log('[server]: server started on port 3000');
});

//interperet the input
async function interperet(input, clientSocket, isScript) {

  //split the command into seperate parts
  var splitInput = input.split(" ");

  var i;
      for (i = 0; i < splitInput.length; i++) {
        clientSocket.emit('writeLine', splitInput[i].includes('"'));
      };

  //check string against commands

  switch (splitInput[0]) {

    case "help":

      //write help menu 
      clientSocket.emit('writeLine', "Help");
      clientSocket.emit('writeLine', "help - displays the help screen");
      clientSocket.emit('writeLine', "print [text] - prints [text]");
      clientSocket.emit('writeLine', "log [text] - logs [text] to server");
      clientSocket.emit('writeLine', "clear - clears the screen");
      clientSocket.emit('writeLine', "read [file] - reads a [file]");
      clientSocket.emit('writeLine', "dir - reads the dir");
      clientSocket.emit('writeLine', "text-color [color] - sets text color to [color]");
      clientSocket.emit('writeLine', "bg-color [color] - sets background color to [color]");
      clientSocket.emit('writeLine', "font - displays available fonts");
      clientSocket.emit('writeLine', "font [font] - sets font to [font]");
      clientSocket.emit('writeLine', "[script] - runs [script].nbs");
      reqInput(clientSocket, isScript);
      break;
    case "print":

      //remove the command from the input array
      splitInput.shift();

      //print every item in splitInput 
      clientSocket.emit('writeLine', splitInput.join(" "));
      reqInput(clientSocket, isScript);
      break;
    case "log":

      //remove the command from the input array
      splitInput.shift();

      //log every item in splitInput
      console.log("[" + clientSocket.id + "]: " + splitInput.join(" "));
      reqInput(clientSocket, isScript);
      break;
    case "clear":

      //clear the screen
      clientSocket.emit('clear');
      reqInput(clientSocket, isScript);
      break;
    case "read":

      //open the file and read

      lr = new lbl(splitInput[1]);

      lr.on('error', function (err) {
	      // 'err' contains error object
        clientSocket.emit('writeLine', "Error opening file.");
        reqInput(clientSocket, isScript);
      });

      lr.on('line', function (line) {
        // pause emitting of lines...
        lr.pause();
              
        clientSocket.emit('writeLine', line);

        // ...and continue emitting lines.
        lr.resume();
              
      });

      lr.on('end', function () {
        // All lines are read, file is closed now.
        reqInput(clientSocket, isScript);
      });
      break;
    case "dir":

      //read the dir, get items array
      fs.readdir(__dirname, function (err, items) {

        //write what dir is read
        clientSocket.emit('writeLine', "Index of: " + __dirname);

        //write each item, with data
        var i;
        for (i = 0; i < items.length; i++) {

          //get current item
          var stat = fs.statSync(items[i])

          //format size
          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"
          };

          //get what type of file items[i] is
          var type = "";
          var isFile = stat.isFile();

          //set type
          if (stat.isDirectory() == true) {

            //&nbsp adds a space, to keep items in line
            type = "[dir]&nbsp";
          }
          else if (isFile == true) {
            type = "[file]";
          }
          else {

            //&nbsp adds a space, to keep items in line
            type = "[err]&nbsp";

            console.log("[server]: error finding type for " + items[i]);

          }

          //write line, items[i] and data
          clientSocket.emit('writeLine', type + " " + items[i] + " (" + size + ")");
        }
        reqInput(clientSocket, isScript);
      });
      break;
    case "text-color":

      //set the text color
      clientSocket.emit('text-color', splitInput[1]);
      reqInput(clientSocket, isScript);
      break;
    case "bg-color":

      //set the bg color
      clientSocket.emit('bg-color', splitInput[1]);
      reqInput(clientSocket, isScript);
      break;
    case "font":

      //if no font specified, list fonts
      if (splitInput.length == 1) {
        clientSocket.emit('writeLine', "Availible fonts:");
        clientSocket.emit('writeLine', "Web-safe fonts (Arial, Helvetica, Courier)");
        clientSocket.emit('writeLine', "Web-safe font styles (serif, sans-serif)");
        clientSocket.emit('writeLine', "Ubuntu Mono (default)");
        clientSocket.emit('writeLine', "Source Code Pro");

      } else {

        //set font
        splitInput.shift();
        clientSocket.emit('font', splitInput.join(" "));
      }
      reqInput(clientSocket, isScript);
      break;
    case "image":
      //let image load, cont while waiting, reserve space for it
      
      if (splitInput.length === 1) {
        clientSocket.emit('writeLine', "Please specify an image.");
        reqInput(clientSocket, isScript);
        break;
      }

      fs.readFile(splitInput[1], function(err, buf){
        if (err) {
          clientSocket.emit('writeLine', "Image '" + splitInput[1] + "' not found.");
          reqInput(clientSocket, isScript);
        } else {
          clientSocket.emit('imagePrep', isScript);
          clientSocket.emit('image', { image: true, buffer: buf.toString('base64') })
          console.log('[server]: image "' + splitInput[1] + '" sent');
        }
      });
      break;
    case "args":

      clientSocket.emit('writeLine', "Args: " + splitInput.length);

      var i;
      for (i = 0; i < splitInput.length; i++) {
        clientSocket.emit('writeLine', i + ": " + splitInput[i]);
      };
      
      reqInput(clientSocket, isScript);
      break;
    default:

      if (splitInput[0] === "") {
        if (splitInput.length === 1) {
          reqInput(clientSocket, isScript);
          break;
        }
        //tell the user their input is wrong 
        clientSocket.emit('writeLine', "Command or script '" + splitInput[0] + "' not found.");
        reqInput(clientSocket, isScript);
        break;
      }

      //read the dir, get items array
      fs.readdir(__dirname, function (err, items) {

        //check each item
        var i;
        var found = false;
        for (i = 0; i < items.length; i++) {

          //get current item
          var stat = fs.statSync(items[i])

          //TODO make compat w/ ex that are not 3 char
          var fileNoEx = items[i].slice(0, items[i].indexOf("."));

          if (splitInput[0] === fileNoEx) {
            found = true;
            clientSocket.emit('writeLine', "script: " + fileNoEx + ".nbs");

            //init line-by-line
            lr = new lbl(splitInput[0] + '.nbs');

            lr.on('line', function (line) {
              // pause emitting of lines...
              lr.pause();
              
              interperet(line, clientSocket, true);

              //function resume() {
                // ...and continue emitting lines.
                lr.resume();
              //};

              
              
            });

            lr.on('end', function () {
              // All lines are read, file is closed now.
              reqInput(clientSocket, isScript);
            });



          }
        }

        if (found === false) {
          //tell the user their input is wrong 
          clientSocket.emit('writeLine', "Command or script '" + splitInput[0] + "' not found.");
          reqInput(clientSocket, isScript);
        }

      });


  }
}
function reqInput(clientSocket, isScript) {
  if (isScript === false) {
    clientSocket.emit('reqInput');
  } else {
    //resume();
  }

}
?