forked from electron/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.js
executable file
·70 lines (60 loc) · 1.83 KB
/
resize.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
const sharp = require('sharp')
const path = require('path')
const fs = require('fs')
const recursiveReadSync = require('recursive-readdir-sync')
const imagemin = require('imagemin')
const imageminPngquant = require('imagemin-pngquant')
const yaml = require('js-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 = recursiveReadSync(path.join(__dirname, '../apps')).filter(
(file) => file.match(/icon\.png/)
)
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.load(fs.readFileSync(yamlFile))
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()