forked from altmp/altv-js-module-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-deps.js
152 lines (140 loc) · 4.62 KB
/
update-deps.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
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
// clang-format off
// Updates the dependencies of the client and server
// Usage: node tools/update-deps.js [scope=client|server|all] [mode=debug|release]
const fs = require("fs/promises");
const { createWriteStream } = require("fs");
const pathUtil = require("path");
const crypto = require("crypto");
const childProcess = require("child_process");
const https = require("https");
// Script arguments
const SCOPE = process.argv[2] ?? "all";
if (SCOPE !== "client" && SCOPE !== "server" && SCOPE !== "all") {
console.error("Invalid scope");
process.exit(1);
}
const DEBUG = process.argv[3] === "debug";
// Constants
const CDN_URL = "https://cdn.alt-mp.com";
const CLIENT_FILES = [
{
name: "v8_monolith.lib",
path: "client/deps/v8/lib/Release",
urlPath: "deps/v8/{BRANCH}/x64_win32/Release",
os: "x64_win32",
},
{
name: "v8_monolith.lib",
path: "client/deps/v8/lib/Debug",
urlPath: "deps/v8/{BRANCH}/x64_win32/Debug",
os: "x64_win32",
debugOnly: true,
},
];
const SERVER_FILES = [
{
name: "libnodev2.lib",
path: "server/deps/nodejs/lib/Release",
urlPath: "deps/nodejs/v2/{BRANCH}/x64_win32/Release",
os: "x64_win32",
},
{
name: "libnodev2.dll",
path: "server/deps/nodejs/lib/Release",
urlPath: "deps/nodejs/v2/{BRANCH}/x64_win32/Release",
os: "x64_win32",
},
{
name: "libnodev2.lib",
path: "server/deps/nodejs/lib/Debug",
urlPath: "deps/nodejs/v2/{BRANCH}/x64_win32/Debug",
os: "x64_win32",
debugOnly: true,
},
{
name: "libnodev2.dll",
path: "server/deps/nodejs/lib/Debug",
urlPath: "deps/nodejs/v2/{BRANCH}/x64_win32/Debug",
os: "x64_win32",
debugOnly: true,
},
{
name: "libnodev2.so",
path: "server/deps/nodejs/lib",
urlPath: "deps/nodejs/v2/{BRANCH}/x64_linux",
os: "x64_linux",
},
];
(async () => {
const files =
SCOPE === "client" ? CLIENT_FILES : SCOPE === "server" ? SERVER_FILES : CLIENT_FILES.concat(SERVER_FILES);
for (const file of files) {
if (file.debugOnly && !DEBUG) continue;
if (file.os !== getOSName()) continue;
const fileUrlPath = file.urlPath.replace("{BRANCH}", getGitBranch()).replace("{OS}", getOSName());
const url = `${CDN_URL}/${fileUrlPath}/${file.name}`;
const name = file.nameOverride ? file.nameOverride : file.name;
const path = pathUtil.resolve(__dirname, "../", file.path, name);
let hash = "INVALID";
const fileExists = await doesFileExist(path);
if (fileExists) hash = await getFileHash(path);
const cdnInfo = await getCDNInfo(url);
if (cdnInfo.hashList[file.name] === hash) continue;
console.log(`Downloading ${name}... (Version: ${cdnInfo.version})`);
await downloadFile(url, path);
}
})();
function downloadFile(url, path) {
console.log("downloadFile", url);
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
const stream = createWriteStream(path, { flags: "w" });
res.pipe(stream);
stream.on("finish", resolve);
stream.on("error", reject);
})
.on("error", reject);
});
}
/**
* @param {string} path
* @returns { Promise<{ version: string, hashList: { [name: string]: string }, sizeList: { [name: string]: string } }> }
*/
function getCDNInfo(path) {
console.log("getCDNInfo", path);
return new Promise((resolve, reject) => {
const basePath = pathUtil.dirname(path);
https
.get(`${basePath}/update.json`, (res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => resolve(JSON.parse(data)));
})
.on("error", reject);
});
}
async function getFileHash(file) {
console.log("getFileHash", file);
const content = await fs.readFile(file);
return crypto.createHash("sha1").update(content).digest("hex");
}
function getGitBranch() {
let branch = "dev";
const gitBranch = childProcess.execSync("git rev-parse --abbrev-ref HEAD", { encoding: "utf8" });
if (gitBranch === "rc") branch = "rc";
else if (gitBranch === "release") branch = "release";
return branch;
}
function getOSName() {
return process.platform === "win32" ? "x64_win32" : "x64_linux";
}
async function doesFileExist(path) {
console.log("doesFileExist", path);
try {
await fs.stat(path);
return true;
} catch {
return false;
}
}