File size: 2,438 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
var uri = "https://raw.githubusercontent.com/GiaKhang1810/mira-bot-v1/main/";
var axios = require("axios");
var path = require("path");
var fs = require("fs");
var dir = path.resolve(__dirname, "..") + "/";
var log = require("./lib/log");
var { execSync } = require("child_process");
var changelog;

function getExtension(param) {
    var ext = path.extname(param).toLowerCase().split(".").pop();
    switch (ext) {
        case "json":
            return "json";
        case "js":
        case "css":
        case "ejs":
            return "text";
        default:
            return "binary";
    }
}

async function readChangelog() {
    var res = await axios.get(uri + "/changelog.json");
    changelog = res.data;
    fs.writeFileSync(dir + "/changelog.json", JSON.stringify(changelog, null, 4));
    changelog = changelog[Object.keys(changelog).pop()];
    return;
}

async function Writer() {
    for (var param in changelog) {
        var ext = getExtension(param);
        var url = uri + param;
        var res = await axios.get(url, { responseType: ext === "binary" ? "arraybuffer" : "text" });
        var data;
        var pathWrite = path.resolve(dir, param);
        var Dir = path.dirname(pathWrite);
        if (!fs.existsSync(Dir))
            fs.mkdirSync(Dir, { recursive: true });

        switch (ext) {
            case "json":
                data = JSON.stringify(JSON.parse(res.data), null, 4);
                break;
            case "text":
                data = res.data;
                break;
            default:
                data = Buffer.from(res.data, "utf-8");
                break;
        }
        fs.writeFileSync(pathWrite, data);
    }
    return;
}

async function finishedUpdate() {
    var url = uri + "package.json";
    var res = await axios.get(url);
    var data = JSON.stringify(res.data, null, 4);
    var pathWrite = path.resolve(dir, "package.json");
    fs.writeFileSync(pathWrite, data);
    execSync("npm install", {
        cwd: __dirname,
        stdio: "inherit",
        shell: true
    });
    return;
}

(async () => {
    try {
        await readChangelog();
        await Writer();
        await finishedUpdate();
        process.exit(2);
    } catch (error) {
        log.error("updater.error", error.message);
        console.log(error);
        return process.exit(1);
    }
})();