summaryrefslogtreecommitdiffstats
path: root/extra/download-dist.js
blob: b339ac93005fa7e3e6f5748900fb02eb4e4b7cef (plain)
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
console.log("Downloading dist");
const https = require("https");
const tar = require("tar");

const packageJSON = require("../package.json");
const fs = require("fs");
const version = packageJSON.version;

const filename = "dist.tar.gz";

const url = `https://github.com/louislam/uptime-kuma/releases/download/${version}/${filename}`;
download(url);

/**
 * Downloads the latest version of the dist from a GitHub release.
 * @param {string} url The URL to download from.
 * @returns {void}
 *
 * Generated by Trelent
 */
function download(url) {
    console.log(url);

    https.get(url, (response) => {
        if (response.statusCode === 200) {
            console.log("Extracting dist...");

            if (fs.existsSync("./dist")) {

                if (fs.existsSync("./dist-backup")) {
                    fs.rmSync("./dist-backup", {
                        recursive: true,
                        force: true,
                    });
                }

                fs.renameSync("./dist", "./dist-backup");
            }

            const tarStream = tar.x({
                cwd: "./",
            });

            tarStream.on("close", () => {
                if (fs.existsSync("./dist-backup")) {
                    fs.rmSync("./dist-backup", {
                        recursive: true,
                        force: true,
                    });
                }
                console.log("Done");
                process.exit(0);
            });

            tarStream.on("error", () => {
                if (fs.existsSync("./dist-backup")) {
                    fs.renameSync("./dist-backup", "./dist");
                }
                console.error("Error from tarStream");
            });

            response.pipe(tarStream);
        } else if (response.statusCode === 302) {
            download(response.headers.location);
        } else {
            console.log("dist not found");
        }
    });
}