-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-json.js
42 lines (36 loc) · 1.11 KB
/
update-json.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
const fs = require("fs");
const path = require("path");
function updateJsonWithImageUrls(
inputJsonPath,
imageDir,
baseUrl,
outputJsonPath
) {
// Read the JSON file
const data = fs.readFileSync(inputJsonPath);
const items = JSON.parse(data);
// Get a list of image files in the image directory
const imageFiles = fs.readdirSync(imageDir);
// Iterate through the items
for (const item of items.items) {
// Check if an image file with the same name exists
const imageName = `${item.name.charAt(0).toUpperCase()}${item.name
.slice(1)
.toLowerCase()}.png`;
if (imageFiles.includes(imageName)) {
// Construct the image URL
const imageUrl = `${baseUrl}/${imageName}`;
// Add the imageUrl to the item object
item.imageUrl = imageUrl;
}
}
// Write the updated items array back to the JSON file
fs.writeFileSync(outputJsonPath, JSON.stringify(items, null, 2));
}
// Usage
updateJsonWithImageUrls(
"./brawl-stars/brawl_stars_brawlers.json",
"./brawl-stars/brawler-images",
"https://pixelcrux.com/Brawl_Stars/Images/Brawlers/Med",
"brawlers.json"
);