-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
110 lines (92 loc) · 2.97 KB
/
input.js
File metadata and controls
110 lines (92 loc) · 2.97 KB
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
const { fstat } = require('fs');
const readline = require('readline');
const chalk = require('chalk');
const stdin = process.stdin;
const fs = require('fs');
let connection;
const interface = readline.createInterface({
input: process.stdin,
output: process.stdin
});
const COMMANDS = {
LIST_FILES: new RegExp("ls"),
GET_FILE: new RegExp("getfile"),
LIST_COMMANDS: new RegExp("commands")
};
/**
* Setup User Interface
* Specifically, so that we can handle user input via stdin
*/
const setupInput = function(conn) {
connection = conn;
//stdin.setEncoding('utf8');
handleUserInput(connection);
//return stdin;
interface.setPrompt(">> ");
interface.prompt();
};
const request = (connection, command, args) => {
connection.write(JSON.stringify({
command: command,
arguments: args
}));
};
//Callback for setupInput
const handleUserInput = function(connection) {
console.log("");
console.log("");
console.log(`🚀🚀🚀 Welcome to ${chalk.red.bold("ROCKETFILE")} 🚀🚀🚀`);
console.log("type 'commands' to list all available commands");
console.log("");
interface.on('line', (input) => {
let directory = 'downloads'; //Default directory
const split = input.split(" ");
//First check for the command
if (split[0].match(COMMANDS.LIST_FILES)) {
request(connection, 'list');
} else if (split[0].match(COMMANDS.GET_FILE)) {
if (split.length < 2) {
interface.output.write(chalk.red(`Command requires a file argument\n`));
} else {
const files = './' + directory;
let doesExist = false;
//Add new directory
if (split[2]) {
directory = split[2];
if (!fs.existsSync(split[2])) {
fs.mkdirSync(split[2]);
}
}
for (let file of files) { //Search to see if the file already exists
if (file.includes(split[1])) {
doesExist = true;
}
}
if (doesExist) { //If file doesnt exist, proceed
interface.question('File already exists. Overwrite? (Y/N) ', (input)=>{
if (input === 'Y') {
request(connection, 'getfile', split[1]);
} else if (input === 'N') {
interface.output.write(chalk.red(`File was not downloaded\n`));
} else {
interface.output.write(chalk.red(`Please try again\n`));
}
});
} else {
request(connection, 'getfile', split[1]);
}
}
} else if (split[0].match(COMMANDS.LIST_COMMANDS)) {
interface.output.write(`${chalk.bold(`commands`)}\t\tLists all commands\n`);
interface.output.write(`${chalk.bold(`ls`)}\t\t\tLists files in directory\n`);
interface.output.write(`${chalk.bold(`getfile [filename]`)}\tFetches file with given filename\n`);
} else {
interface.output.write(chalk.red(`Error: Command not recognized\n`));
}
interface.prompt();
});
};
module.exports = {
setupInput,
interface
};