forked from ByMykel/CSGO-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.js
74 lines (63 loc) · 2.14 KB
/
group.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
import fs from "fs";
import path from "path";
import { LANGUAGES_URL } from "./constants.js";
import { getManifestId } from "./services/main.js";
const inputFilePathsTemplate = [
"./public/api/{lang}/agents.json",
"./public/api/{lang}/collectibles.json",
"./public/api/{lang}/collections.json",
"./public/api/{lang}/crates.json",
"./public/api/{lang}/graffiti.json",
"./public/api/{lang}/keys.json",
"./public/api/{lang}/music_kits.json",
"./public/api/{lang}/patches.json",
"./public/api/{lang}/skins_not_grouped.json",
"./public/api/{lang}/stickers.json",
"./public/api/{lang}/tools.json",
];
let existingManifestId = "";
const latestManifestId = await getManifestId();
try {
existingManifestId = fs.readFileSync("./manifestIdGroup.txt");
} catch (err) {
if (err.code != "ENOENT") {
throw err;
}
}
if (existingManifestId == latestManifestId) {
console.log("Latest manifest Id matches existing manifest Id, exiting");
process.exit(0);
} else {
console.log(
"Latest manifest Id does not match existing manifest Id, generating new data."
);
}
for (let langObj of LANGUAGES_URL) {
const lang = langObj.folder;
const allData = {};
const inputFilePaths = inputFilePathsTemplate.map((templatePath) =>
templatePath.replace("{lang}", lang)
);
for (let filePath of inputFilePaths) {
const fullPath = path.join(process.cwd(), filePath);
// Check if file exists before reading
if (fs.existsSync(fullPath)) {
const fileData = JSON.parse(fs.readFileSync(fullPath, "utf-8"));
if (Array.isArray(fileData)) {
fileData.forEach((item) => {
allData[item.id] = item;
});
}
} else {
console.warn(`File ${fullPath} not found.`);
}
}
const outputFilePath = `./public/api/${lang}/all.json`;
fs.writeFileSync(outputFilePath, JSON.stringify(allData, null, 4));
console.log(`all.json for ${lang} has been generated.`);
}
try {
fs.writeFileSync("./manifestIdGroup.txt", latestManifestId.toString());
} catch (err) {
throw err;
}