File size: 3,878 Bytes
91d9d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var { getTime } = require("./utils.js");
var { existsSync } = require("fs");

function color(text) {
    return {
        black: "\x1b[30m" + text + "\x1b[0m",
        red: "\x1b[31m" + text + "\x1b[0m",
        green: "\x1b[32m" + text + "\x1b[0m",
        yellow: "\x1b[33m" + text + "\x1b[0m",
        blue: "\x1b[34m" + text + "\x1b[0m",
        magenta: "\x1b[35m" + text + "\x1b[0m",
        cyan: "\x1b[36m" + text + "\x1b[0m",
        white: "\x1b[37m" + text + "\x1b[0m",
        bgBlack: "\x1b[40m" + text + "\x1b[0m",
        bgRed: "\x1b[41m" + text + "\x1b[0m",
        bgGreen: "\x1b[42m" + text + "\x1b[0m",
        bgYellow: "\x1b[43m" + text + "\x1b[0m",
        bgBlue: "\x1b[44m" + text + "\x1b[0m",
        bgMagenta: "\x1b[45m" + text + "\x1b[0m",
        bgCyan: "\x1b[46m" + text + "\x1b[0m",
        bgWhite: "\x1b[47m" + text + "\x1b[0m"
    }
}

function parseDir(dir, inputs = []) {
    try {
        var { language } = require("../../config.json").systemOptions;
        var languagePath = __dirname + "/../languages/" + language + ".json";

        if (!existsSync(languagePath))
            languagePath = __dirname + "/../languages/vi-VN.json";

        if (/^([^.]+)(\.[^.]+)*$/.test(dir)) {
            var languageData = require(languagePath);
            var ArrayKey = dir.split(".");
            var content = languageData;

            for (var key of ArrayKey)
                content = content[key];

            if (inputs.length > 0) {
                for (var index = 1; index <= inputs.length; index++)
                    content = content.replace("%" + index, inputs[index - 1]);
            }

            return content;
        }
        return dir;
    } catch (error) {
        console.log(error);
        return dir;
    }
}

function info(dir, ...inputs) {
    inputs = !inputs ? [] : inputs;
    var time = getTime();
    var content = parseDir(dir, inputs);
    var output = color("[ " + time + " ] ").green + content;
    return console.log(output);
}

function warn(dir, ...inputs) {
    inputs = !inputs ? [] : inputs;
    var time = getTime();
    var content = parseDir(dir, inputs);
    var output = color("[ " + time + " ] ").yellow + content;
    return console.log(output);
}

function error(dir, ...inputs) {
    inputs = !inputs ? [] : inputs;
    var time = getTime();
    var content = parseDir(dir, inputs);
    var output = color("[ " + time + " ] ").red + content;
    return console.log(output);
}

function wall() {
    var content = color("====================================================").blue;
    return console.log(content);
}

function input(dir, col = "green", timeout = 30000) {
    var time = getTime();
    var content = parseDir(dir, []);
    var output = color("[ " + time + " ] ")[col] + content;
    process.stdout.write(output);
    return new Promise((resolve, reject) => {
        var data = "";
        var inputReceived = false;
        var timer = setTimeout(() => {
            if (!inputReceived) {
                process.stdout.write("\n");
                resolve(data.trim());
            }
        }, timeout);
        process.stdin.setEncoding("utf8");
        process.stdin.on("data", (chunk) => {
            clearTimeout(timer);
            data += chunk;
            inputReceived = true;
            process.stdin.pause();
            resolve(data.trim());
        });
        process.stdin.on("error", (err) => {
            clearTimeout(timer);
            reject(err);
        });
        process.stdin.on("end", () => {
            clearTimeout(timer);
            resolve(data.trim());
        });
        process.stdin.resume();
    });
}

module.exports = {
    info,
    warn,
    error,
    wall,
    color,
    input,
    parseDir
}