summaryrefslogtreecommitdiff
path: root/build.js
blob: 4ea396772bacc64f2e7ef1b7393d13bfadd2abf0 (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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const cp = require('child_process');
const fs = require('fs');
const path = require('path');

const port = ("42" + Math.random().toString().split(".")[1]).substring(0, 5);

console.log("Refreshing projects...");
cp.execSync("php refresh-projects.php", { stdio: "inherit", cwd: "includes" });

console.log("Backing up .vercel...");
cp.execSync("mv ./out/.vercel ./.vercel");

console.log("Removing old build...");
if (fs.existsSync("./out")) fs.rmSync("./out", { recursive: true });
fs.mkdirSync("./out");

console.log("Restoring .vercel...");
cp.execSync("mv ./.vercel ./out/.vercel");

console.log("Copying assets...");
cp.execSync("cp -r ./assets ./out/assets");

async function getFiles(dir) {
    const dirEnt = await fs.promises.readdir(dir, { withFileTypes: true });

    const files = await Promise.all(dirEnt.map((dirent) => {
        const res = path.resolve(dir, dirent.name);
        return dirent.isDirectory() ? getFiles(res) : res;
    }));

    return Array.prototype.concat(...files);
}

console.log("Starting PHP internal server on port " + port + "...");
let p = cp.execFile("php", [ "-S", "127.0.0.1:" + port, "router.php" ]);

let config = {
    headers: [
        {
            source: "/assets/(.*)",
            headers: [
                {
                    key: "Cache-Control",
                    value: "public, s-maxage=3600, maxage=86400, stale-while-revalidate=300, stale-if-error=86400"
                }
            ]
        },
        {
            source: "/warrant",
            headers: [
                {
                    key: "Content-Type",
                    value: "text/plain"
                }
            ]
        },
        {
            source: "/announcement",
            headers: [
                {
                    key: "Content-Type",
                    value: "text/plain"
                }
            ]
        },
        {
            source: "/version",
            headers: [
                {
                    key: "Content-Type",
                    value: "text/plain"
                }
            ]
        },
        {
            source: "/pubkey",
            headers: [
                {
                    key: "Content-Type",
                    value: "text/plain"
                }
            ]
        },
        {
            source: "/(.*)",
            headers: [
                {
                    key: "Cache-Control",
                    value: "public, s-maxage=60, maxage=300, stale-while-revalidate=60, stale-if-error=300"
                }
            ]
        }
    ]
};

let continued = false;
let waiter = setInterval(async () => {
    try {
        await fetch("http://127.0.0.1:" + port + "/warrant");
        if (continued) return; continued = true;
        clearInterval(waiter);
        console.log("Server is up, continuing.");

        console.log("Analyzing source code...");
        let files = (await getFiles(".")).map(i => i.substring(process.cwd().length + 1)).filter(i => !i.startsWith(".") && !i.startsWith("assets/") && !i.startsWith("includes/") && !i.startsWith("out/"));

        console.log("Gathering list of projects...");
        let projects = require('./includes/data/projects.json').map(i => "projects/" + (i["name"] ?? i["id"]));

        console.log("Gathering list of pages...");
        let pages = [...projects, ...files.filter(i => i.endsWith("/index.php")), "404.html", ""];
        let fullPages = pages.map(k => {
            if (k.endsWith("/index.php")) return k.substring(0, k.length - 10);
            return k;
        }).map(j => "/" + j);
        console.log("Found " + fullPages.length + " pages to render");

        let index = 0;
        let total = fullPages.length;

        for (let page of fullPages) {
            process.stdout.clearLine(null);
            process.stdout.cursorTo(0);
            process.stdout.write("Rendering: " + page + " (" + ((index / total) * 100).toFixed(1) + "% complete)");

            async function doRequest() {
                try {
                    return await fetch("http://127.0.0.1:" + port + page, { redirect: "manual", headers: { "User-Agent": "Mozilla/5.0 (+Starshine/1.0)" } });
                } catch (e) {
                    p.kill("SIGKILL");
                    p = cp.execFile("php", [ "-S", "127.0.0.1:" + port, "router.php" ]);

                    function wait() {
                        return new Promise((res) => {
                            let waiter = setInterval(async () => {
                                try {
                                    await fetch("http://127.0.0.1:" + port + "/warrant");
                                    clearInterval(waiter);
                                    res();
                                } catch (e) {}
                            });
                        });
                    }

                    await wait();
                    return await doRequest();
                }
            }

            let res = await doRequest();

            if (res.status === 302 || res.status === 301 || res.status === 308 || res.status === 307) {
                if (!config['redirects']) config['redirects'] = [];
                config['redirects'].push({
                    source: page,
                    destination: res.headers.get("location").replace("http://127.0.0.1:42934/", "")
                });
            } else {
                if (page.endsWith("/404.html")) {
                    if (!fs.existsSync("./out" + path.dirname(page))) fs.mkdirSync("./out" + path.dirname(page), { recursive: true });
                    fs.writeFileSync("./out" + page, await res.text());
                    if (!config['rewrites']) config['rewrites'] = [];
                    config['rewrites'].push({
                        source: "/" + page.split("/")[0] + "/(.*)",
                        destination: "/" + page
                    });
                } else {
                    if (!fs.existsSync("./out" + page)) fs.mkdirSync("./out" + page, { recursive: true });
                    fs.writeFileSync("./out" + page + "/index.html", await res.text());
                    if (!config['rewrites']) config['rewrites'] = [];
                    config['rewrites'].push({
                        source: page + "/index.html",
                        destination: page
                    });
                }
            }

            index++;
        }

        fs.writeFileSync("./out/warrant", fs.readFileSync("./warrant"));
        fs.writeFileSync("./out/version", fs.readFileSync("./version"));
        fs.writeFileSync("./out/pubkey", fs.readFileSync("./pubkey"));
        fs.writeFileSync("./out/announcement", fs.readFileSync("./announcement"));

        fs.writeFileSync("./out/vercel.json", JSON.stringify(config, null, 2));
        process.stdout.clearLine(null);
        process.stdout.cursorTo(0);
        console.log("Export completed!");
        p.kill();

        console.log("Deploying to Vercel...");
        cp.execSync("vercel --prod", { cwd: "./out", stdio: "inherit" });

        process.exit();
    } catch (e) {
        if (continued) {
            throw e;
        }
    }
});