-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.js
executable file
·88 lines (74 loc) · 1.93 KB
/
console.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
// this file is part of the console.js libary by derzombiiie
// this is libary is under the GPL-3.0-or-later license
const readline = require("readline")
this.registerdcmds = {}
this.registerdcmdstype = {}
this.prefix = ""
this.init = () => {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
process.stdout.write(this.prefix + "> ")
this.rl.on("line", (cmd) => {
this.eval(cmd, _ => process.stdout.write(this.prefix + "> "))
})
return this
}
this.eval = ( cmd, resolve ) => {
debugger
let split = cmd.split(" ")
let comm = split[0]
split.shift()
let args = JSON.parse(JSON.stringify(split));
args.unshift(this.prefix)
if ( !comm ) return resolve() // if cmd empty dont do anything
if ( comm === "FE!") {
process.exit(-1) // Force exit
}
if ( this.registerdcmds[comm] ) {
if( !this.registerdcmdstype[comm] ) { // check if func uses res
this.registerdcmds[comm](args)
if(resolve) resolve()
} else {
this.registerdcmds[comm](args, resolve)
}
} else {
console.log(`Bad command! '${comm}'`)
if(resolve) resolve()
}
}
this.registercmd = ( cmd, callback, res = false ) => {
this.registerdcmds[cmd] = callback
this.registerdcmdstype[cmd] = res
return this
}
this.alias = ( cmd, alias ) => {
this.registerdcmds[alias] = this.registerdcmds[cmd]
this.registerdcmdstype[alias] = this.registerdcmdstype[cmd]
return this
}
this.exitcmds = []
this.registerexit = ( callback ) => {
this.push( callback )
}
// preregisterd commands:
this.registercmd("list", () => {
console.log( Object.keys(this.registerdcmds).sort().join(", ") )
})
this.alias("list", "help")
this.alias("help", "?")
this.registercmd("eval", () => {
try {
console.log(eval(args.join(" ")))
} catch {
console.log("Couldn't execute!")
}
})
this.registercmd("exit", () => {
this.exitcmds.forEach((cb) => {cb()})
setTimeout(_ => process.exit(0), 250)
})
this.registercmd("clear", () => {
console.clear()
})