-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-deploy-patch.js
53 lines (45 loc) · 1.45 KB
/
github-deploy-patch.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
/* this is just needed to patch the github pages deployment
* because github pages does not support relative paths
*/
const fs = require("fs");
const path = require("path");
(async () => {
const appConfig = JSON.parse(await readAsync("./app.config.json"));
const pkgConfig = JSON.parse(await readAsync("./package.json"));
const ROOTS = ["css", "media", pkgConfig.name];
const {uri_root} = appConfig;
if(!uri_root) {
throw new Error("`uri_root` not found in app.config.json");
}
let data = await readAsync("./dist/index.html");
if(uri_root !== "") {
for(const root of ROOTS) {
data = data.replaceAll(`"/${root}`, `"/${uri_root}/${root}`);
data = data.replaceAll(`'/${root}`, `'/${uri_root}/${root}`);
}
}
await writeAsync("./dist/index.html", data);
await writeAsync("./dist/404.html", data);
})();
function readAsync(src) {
return new Promise((resolve, reject) => {
fs.readFile(path.resolve(src), "utf-8", (err, data) => {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
function writeAsync(dest, data) {
return new Promise((resolve, reject) => {
fs.writeFile(path.resolve(dest), data, { encoding: "utf8", flag: "w", }, (err) => {
if(err) {
reject(err);
} else {
resolve();
}
});
});
}