-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (124 loc) · 4.72 KB
/
index.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
153
const axios = require('axios');
const fs = require('fs-extra');
const path = require('path');
const cron = require('node-cron');
const { exec } = require('child_process');
const config = require('./config.json');
const ARTIFACT_FOLDER = __dirname;
const OS_TYPE = config.Settings.Type.toLowerCase();
async function getLatestRelease() {
try {
const response = await axios.get("https://api.github.com/repos/citizenfx/fivem/git/refs/tags", {
headers: { "accept": "application/vnd.github.v3+json" }
});
const refs = response.data;
const latestRef = refs.filter(ref => ref.ref.includes("refs/tags/v1.0.0")).pop();
if (!latestRef) {
throw new Error("Could not find latest release.");
}
const tagResponse = await axios.get(latestRef.object.url, {
headers: { "accept": "application/vnd.github.v3+json" }
});
const version = tagResponse.data.tag.replace("v1.0.0.", "");
const hash = tagResponse.data.object.sha;
let uri = "";
if (OS_TYPE === "linux") {
uri = `https://runtime.fivem.net/artifacts/fivem/build_proot_linux/master/${version}-${hash}/fx.tar.xz`;
}
if (OS_TYPE === "windows") {
uri = `https://runtime.fivem.net/artifacts/fivem/build_server_windows/master/${version}-${hash}/server.7z`;
}
return {
uri,
version
};
} catch (error) {
throw new Error(`Error fetching latest release: ${error.message}`);
}
}
async function updateServer() {
console.log("### FiveM Automatic Updater ###");
let currentVersion = 0;
try {
const currentVersionPath = path.join(ARTIFACT_FOLDER, "current-version");
if (fs.existsSync(currentVersionPath)) {
currentVersion = parseInt(await fs.readFile(currentVersionPath, 'utf8'), 10);
}
} catch (error) {
console.error(`Error reading current version: ${error.message}`);
}
console.log(`Detected version is ${currentVersion}...`);
let latestRelease;
try {
latestRelease = await getLatestRelease();
} catch (error) {
console.error(`Error fetching latest release: ${error.message}`);
return;
}
console.log(`The current version on server is ${latestRelease.version}. Checking the artifacts server.`);
if (latestRelease.version !== currentVersion) {
const dest = path.join(ARTIFACT_FOLDER, `${latestRelease.version}.tar.xz`);
console.log(`Downloading artifact ${latestRelease.version} located at ${latestRelease.uri}`);
try {
await downloadFile(latestRelease.uri, dest);
console.log("Extracting new artifact");
await extractArchive(dest, ARTIFACT_FOLDER);
await fs.writeFile(path.join(ARTIFACT_FOLDER, "current-version"), latestRelease.version.toString());
await fs.remove(dest);
console.log("Update completed.");
} catch (error) {
console.error(`Error during update: ${error.message}`);
}
} else {
console.log("FXServer is up to date.");
}
}
async function extractArchive(src, dest) {
try {
if (OS_TYPE === "windows") {
await execCommand(`"${__dirname}/7zr.exe" x "${src}" -o"${dest}/fxserver" -y`);
} else if (OS_TYPE === "linux") {
await execCommand(`tar -xJf "${src}" -C "${dest}"`);
}
} catch (error) {
throw new Error(`Error extracting archive: ${error.message}`);
}
}
function execCommand(command) {
console.log(`Executing command: ${command}`);
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`);
reject(error);
} else {
console.log(`Command output: ${stdout.trim()}`);
resolve(stdout.trim());
}
});
});
}
cron.schedule('0 2 * * *', async () => {
console.log('Running the FiveM Automatic Updater...');
await updateServer();
});
console.log('FiveM Automatic Updater scheduled to run every day at 2:00 AM.');
async function downloadFile(url, dest) {
const writer = fs.createWriteStream(dest);
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
if (OS_TYPE === "windows") {
downloadFile("https://www.7-zip.org/a/7zr.exe", __dirname + "/7zr.exe");
}
updateServer().catch(error => {
console.error(`An error occurred: ${error.message}`);
});