-
Notifications
You must be signed in to change notification settings - Fork 43
/
tasks.js
82 lines (72 loc) · 1.5 KB
/
tasks.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
/**
* Starts the application
* This is the function that is run when the app starts
*
* It prints a welcome line, and then a line with "----",
* then nothing.
*
* @param {string} name the name of the app
* @returns {void}
*/
function startApp(name){
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', onDataReceived);
console.log(`Welcome to ${name}'s application!`)
console.log("--------------------")
}
/**
* Decides what to do depending on the data that was received
* This function receives the input sent by the user.
*
* For example, if the user entered
* ```
* node tasks.js batata
* ```
*
* The text received would be "batata"
* This function then directs to other functions
*
* @param {string} text data typed by the user
* @returns {void}
*/
function onDataReceived(text) {
if (text === 'quit\n') {
quit();
}
else if(text === 'hello\n'){
hello();
}
else{
unknownCommand(text);
}
}
/**
* prints "unknown command"
* This function is supposed to run when all other commands have failed
*
* @param {string} c the text received
* @returns {void}
*/
function unknownCommand(c){
console.log('unknown command: "'+c.trim()+'"')
}
/**
* Says hello
*
* @returns {void}
*/
function hello(){
console.log('hello!')
}
/**
* Exits the application
*
* @returns {void}
*/
function quit(){
console.log('Quitting now, goodbye!')
process.exit();
}
// The following line starts the application
startApp("Jad Sarout")