-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-icons.js
51 lines (40 loc) · 1.44 KB
/
generate-icons.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
const sharp = require("sharp");
const fs = require("fs");
const path = require("path");
// Read the Contents.json file
const contents = require("./KeysVsClicks/Assets.xcassets/AppIcon.appiconset/Contents.json");
// Create a Set to store unique sizes (to avoid processing duplicates)
const processedSizes = new Set();
async function generateIcons() {
try {
const sourceImage =
"./KeysVsClicks/Assets.xcassets/AppIcon.appiconset/1024.png";
// Create a promise array for all resize operations
const resizePromises = contents.images.map(async (image) => {
const expectedSize = parseInt(image["expected-size"]);
// Skip if we've already processed this size
if (processedSizes.has(expectedSize)) {
return;
}
processedSizes.add(expectedSize);
const outputPath = path.join(
"./KeysVsClicks/Assets.xcassets/AppIcon.appiconset",
`${expectedSize}.png`
);
console.log(`Generating ${expectedSize}x${expectedSize} icon...`);
return sharp(sourceImage)
.resize(expectedSize, expectedSize, {
fit: "contain",
background: { r: 0, g: 0, b: 0, alpha: 0 },
})
.png()
.toFile(outputPath);
});
// Wait for all resize operations to complete
await Promise.all(resizePromises);
console.log("All icons generated successfully!");
} catch (error) {
console.error("Error generating icons:", error);
}
}
generateIcons();