-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.js
45 lines (40 loc) · 932 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
40
41
42
43
44
45
const fs = require('fs');
const roy = require('./roygbiv');
function done(output) {
process.stdout.write(output);
process.stdout.write('\nprompt> ');
}
function evaluateCmd(userInput) {
const userInputArray = userInput.split(' ');
const command = userInputArray[0];
switch (command) {
case 'echo':
commandLibrary.echo(roy.red(userInputArray.slice(1).join(' ')));
break;
case 'cat':
commandLibrary.cat(userInputArray.slice(1));
break;
case 'royecho':
commandLibrary.roy(userInputArray.slice(1))
}
}
const commandLibrary = {
echo: function(userInput) {
done(userInput);
},
cat: function(fullPath) {
const fileName = fullPath[0];
fs.readFile(fileName, (err, data) => {
if (err) {
throw err;
} else {
done(data);
}
})
},
roy: function(userInput) {
done(roy.rainbowMe(userInput.join(' ')));
}
};
exports.commandLibrary = commandLibrary;
exports.evaluateCmd = evaluateCmd;