-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathgroup.js
More file actions
110 lines (92 loc) · 3.34 KB
/
group.js
File metadata and controls
110 lines (92 loc) · 3.34 KB
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
import fs from "fs";
import path from "path";
import { getLanguages, parseLanguagesArg } from "./utils/languages.js";
import { getManifestId, getImagesJsonSha } from "./services/main.js";
const args = process.argv.slice(2);
const isForce = args.includes("--force");
const codes = parseLanguagesArg(args);
if (codes.length === 0) {
console.error("Error: no languages specified. Pass --languages en,ru,uk (comma-separated folder codes).");
process.exit(1);
}
const languages = getLanguages(codes);
if (languages.length === 0) {
console.error(`Error: none of the provided codes match known languages: ${codes.join(", ")}`);
process.exit(1);
}
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}/sticker_slabs.json",
"./public/api/{lang}/keychains.json",
"./public/api/{lang}/tools.json",
];
let existingManifestId = "";
let existingImagesSha = "";
const [latestManifestId, latestImagesSha] = await Promise.all([getManifestId(), getImagesJsonSha()]);
try {
existingManifestId = fs.readFileSync("./manifestIdGroup.txt", "utf-8");
} catch (err) {
if (err.code != "ENOENT") {
throw err;
}
}
try {
existingImagesSha = fs.readFileSync("./imagesShaGroup.txt", "utf-8");
} catch (err) {
if (err.code != "ENOENT") {
throw err;
}
}
if (isForce) {
console.log("Force flag detected, generating new data regardless of manifest Ids");
} else {
const manifestChanged = existingManifestId !== latestManifestId;
const imagesChanged = existingImagesSha !== latestImagesSha;
if (!manifestChanged && !imagesChanged) {
console.log("No changes detected in manifest or images.json, exiting");
process.exit(0);
}
if (manifestChanged) {
console.log("Manifest Id changed, generating new data.");
}
if (imagesChanged) {
console.log("images.json changed, generating new data.");
}
}
for (let langObj of languages) {
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));
console.log(`all.json for ${lang} has been generated.`);
}
try {
fs.writeFileSync("./manifestIdGroup.txt", latestManifestId.toString());
fs.writeFileSync("./imagesShaGroup.txt", latestImagesSha.toString());
} catch (err) {
throw err;
}