forked from electron/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize.js
More file actions
executable file
·65 lines (55 loc) · 1.85 KB
/
Copy pathresize.js
File metadata and controls
executable file
·65 lines (55 loc) · 1.85 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
const sharp = require('sharp');
const path = require('path');
const fs = require('fs');
const readdirp = require('readdirp');
const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
const yaml = require('yaml');
async function resize(file, size) {
const newFile = file.replace('.png', `-${size}.png`);
// skip files that are up to date
if (fs.existsSync(newFile) && fs.statSync(newFile).mtime > fs.statSync(file).mtime) {
return Promise.resolve(null);
}
return sharp(fs.readFileSync(file))
.resize(size, size, { fit: 'inside' })
.toFormat('png')
.toBuffer()
.then((buf) => imagemin.buffer(buf))
.then((buf) => imagemin.buffer(buf, { use: [imageminPngquant()] }))
.then((buf) => fs.writeFileSync(newFile, buf));
}
async function main() {
const icons = [];
for await (const entry of readdirp(path.join(__dirname, '../apps'))) {
if (entry.basename.match(/icon\.png/)) {
icons.push(entry.fullPath);
}
}
console.log(`Resizing ${icons.length} icons...`);
const resizes = icons.reduce((acc, icon) => {
const iconName = path.basename(icon);
// skip disabled app
const yamlFile = path.join(icon.replace('-icon.png', '.yml'));
const { disabled } = yaml.parse(fs.readFileSync(yamlFile, 'utf-8'));
if (disabled) {
return acc;
}
return {
...acc,
[iconName]: [resize(icon, 32), resize(icon, 64), resize(icon, 128), resize(icon, 256)],
};
}, {});
for (const icon in resizes) {
const promises = await Promise.allSettled(Object.values(resizes[icon]));
const failed = promises.filter((p) => p.status === 'rejected');
if (failed.length > 0) {
console.error(`🔴 Failed to resize icons for icon "${icon}"!`);
for (const { reason } of failed) {
console.log(reason);
}
process.exit(1);
}
}
}
main();