-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (67 loc) · 1.91 KB
/
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
'use strict'
// Built-in
const os = require('os')
const readline = require('readline')
// For printing colored console texts.
const colors = require('colors')
// Custom service
let CommandService = require('./service')
// Getting uname and hostname
const uname = os.userInfo().username
const hostname = os.hostname()
// Setting current path
let currentPath = __dirname
// Interface initiate for readline
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: getPromptLine(),
historySize: 5,
removeHistoryDuplicates: true
})
// Registering SIGNINT Listener
process.on('SIGINT', () => {
console.log('SIGINT');
CommandService.EXIT();
});
// ===================================================================
// Begins
// ===================================================================
// Display introduction text
CommandService.SHOW_INTRODUCTION()
// Print default CLI line initially
rl.prompt();
// Handles input commands from consoles
rl.on('line', (input) => {
input = input.trim()
// Extracting command and parameter
let values = input.split(' ').filter(val => val)
let [cmd, param] = values
switch (cmd) {
case '':
break
case 'pwd': CommandService.PWD(currentPath)
break
case 'clear': CommandService.CLEAR()
break
case 'cd': currentPath = CommandService.CD(currentPath, param)
break
case 'ls': CommandService.LS(currentPath)
break
case 'hostname': CommandService.HOSTNAME(hostname)
break
case 'uname': CommandService.UNAME(uname)
break
case 'reset': currentPath = CommandService.RESET()
break
case 'exit': CommandService.EXIT()
break
default: CommandService.NOT_SUPPORTED()
break
}
rl.setPrompt(getPromptLine())
rl.prompt();
})
function getPromptLine() {
return colors.green(`${uname}@${hostname}`) + `:` + colors.blue(`~${currentPath}]`) + ` $ `;
}