forked from electron/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.js
38 lines (32 loc) · 1.03 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
const sharp = require('sharp')
const path = require('path')
const fs = require('fs')
const recursiveReadSync = require('recursive-readdir-sync')
const icons = recursiveReadSync(path.join(__dirname, '../apps'))
.filter(file => file.match(/icon\.png/))
process.stdout.write(`Resizing ${icons.length} icons...`)
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)
.max()
.toFormat('png')
.toFile(newFile)
}
const resizes = icons.map(icon => resize(icon, 32))
.concat(icons.map(icon => resize(icon, 64)))
.concat(icons.map(icon => resize(icon, 128)))
Promise.all(resizes)
.then(function (results) {
process.stdout.write(` Done.`)
process.exit()
})
.catch(function (err) {
console.error('Error resizing icons!')
console.error(err)
process.exit()
})