-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (71 loc) · 1.92 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
const {
texts,
mainTableOptions
} = require("./variables")
const { TableLogData } = require("./tools/tableLogData")
const { readFile } = require("fs").promises
const readline = require("readline")
const path = require('path')
const { exit, cwd } = require("process")
const { resolve } = require("path")
const { exec } = require('child_process')
let mainTable = null
// keys listener
const initKeyEvents = () => {
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on("keypress", (str, key) => {
if (key.name === "up") {
mainTable.moveUp()
updateConsole()
}
if (key.name === "down") {
mainTable.moveDn()
updateConsole()
}
if (key.ctrl && key.name === "c" || key.name === "q" || key.name === "escape") {
exit()
}
if (key.ctrl && key.name === "s") {
console.log(mainTable.data[mainTable.active].command)
}
if (key.name === "return") {
bash(mainTable.data[mainTable.active].command)
}
})
}
const updateConsole = () => {
console.clear()
console.log("\n" + cwd() + "\n" + texts.ctrlC + "\n" + texts.ctrlS + "\n")
mainTable.renderTable()
}
const bash = (command) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(`stdout: ${stdout}`)
return stdout
})
}
async function loadConfig() {
const data = await readFile(path.resolve(__dirname, "helper.json"), "utf8", (err, data) => {
if (err) {
console.log("Error loading file: ", err)
}
})
return JSON.parse(data)
}
const init = async () => {
data = await loadConfig()
mainTable = new TableLogData(data, mainTableOptions)
updateConsole()
initKeyEvents()
}
console.log("Loading data...")
init()