This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathicon-magic.js
executable file
·134 lines (129 loc) · 4.11 KB
/
icon-magic.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /usr/bin/env node
const fs = require('fs');
const gm = require('gm');
const path = require('path');
const rimraf = require('rimraf');
const imagemin = require('imagemin');
const imageminZopfli = require('imagemin-zopfli');
const args = process.argv;
const icons = {
32: 'favicon',
60: 'apple-touch',
120: 'apple-touch',
152: 'apple-touch',
167: 'apple-touch',
180: 'apple-touch',
192: 'pwa',
256: 'pwa',
384: 'pwa',
512: 'pwa',
};
function optimizeImage(filename) {
return new Promise((resolve, reject) =>
// https://github.com/imagemin/imagemin
imagemin(
[`.tmp/${filename}`], // input file
'icons', // output dir
{
use: [imageminZopfli({ more: true })], // https://github.com/imagemin/imagemin-zopfli
}
).then(() => {
console.log(`✅ ./${filename} ===> Optimization complete 🎉`);
resolve();
})
);
}
function resizeImage(imageBuffer, size) {
const filename = icons[size] === 'favicon'
? 'favicon.png'
: `${icons[size]}-icon-${size}x${size}.png`;
return new Promise((resolve, reject) =>
// https://github.com/aheckmann/gm
gm(imageBuffer)
// Validation
.format((err, value) => {
if (err) throw new Error(err);
if (value !== 'PNG')
throw new Error(
'❌ Source image must be PNG format. See README.md for details.'
);
})
.size((err, value) => {
if (err) throw new Error(err);
if (value.width !== value.height)
throw new Error(
'❌ Source image must be a square. See README.md for details.'
);
if (value.width < 512)
throw new Error(
'❌ Source image dimensions must be at least 512px x 512px. See README.md for details.'
);
})
// Resizing
.resize(size, size)
.write(`.tmp/${filename}`, err => {
if (err) {
console.error(
`❌ This is bad... I have no idea what's gone wrong.`,
err
);
} else {
console.log(`✅ ./${filename} ===> Resize complete 🎉`);
}
resolve(filename);
})
);
}
// args[0] == node bin path
// args[1] == the icon-magic bin
// args[2] == user-provided image source path
if (fs.existsSync(args[2])) {
// Read image source from filesystem into a buffer
fs.readFile(path.resolve(args[2]), (err, imageBuffer) => {
if (err)
console.error(`❌ This is bad... I have no idea what's gone wrong.`, err);
if (!fs.existsSync('.tmp')) fs.mkdirSync('.tmp');
// if (fs.existsSync('icons')) rimraf('icons', () => {});
// Iterate over our options object `icons` and begin resizing images
console.log('📏 Resizing...');
let resizePass = Object.keys(icons).map(size =>
resizeImage(imageBuffer, size)
);
// Once resized image promise resolves...
Promise.all(resizePass)
.then(resizedImages => {
console.log('💯 Success!\n\n');
// ...pass them into the optimization function
console.log(
'⏱ Optimizing... larger icons will take longer to optimize... use the fifth P!'
);
console.log(
"🚗 Note– order doesn't matter– we've gotta wait for the slowest regardless."
);
let optimizationPass = resizedImages.map(filename =>
optimizeImage(filename)
);
// Once optimization promise resolves...
Promise.all(optimizationPass).then(() => {
console.log('💯 Success! Thanks for being patient.\n\n');
// ...remove the .tmp directory we used earlier
console.log('🗑 Cleaning Up...');
rimraf('.tmp', () => {
console.log(
`💯 Success! ${Object.keys(icons).length} images were created and are available in the "icons" directory.`
);
});
});
})
.catch(err => {
console.error(
`❌ This is bad... I have no idea what's gone wrong.`,
err
);
});
});
} else {
throw Error(
'❌ You must provide a source image to the `icon-magic` command. See README.md for details.'
);
}