-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
39 lines (35 loc) · 1006 Bytes
/
commands.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
//import fs library
const fs = require("fs");
//print data
function done(output) {
process.stdout.write(output);
process.stdout.write("\nprompt > ");
}
// checks if the command is tail command of linux
function evaluateCmd(userInput) {
const userInputArray = userInput.split(" ");
const command = userInputArray[0];
if (command == "tail") {
tail(userInputArray.slice(1));
} else process.stdout.write("Typed command is not accurate");
}
const tail = fullPath => {
const fileName = fullPath[1];
const N = fullPath[0];
fs.readFile(fileName, (err, data) => {
if (err) throw err;
var text = data.toString("utf8");
var slicedText = text
.split("\n")
.slice(-N)
.join("\n");
var bufferText = Buffer.from(slicedText, "utf8");
done(bufferText);
});
};
process.stdout.write("prompt > ");
// This is triggers after a user types in a line
process.stdin.on("data", userInput => {
userInput = userInput.toString().trim();
evaluateCmd(userInput);
});