-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimise.js
266 lines (207 loc) · 7 KB
/
optimise.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const { Log, Exception, MANIFEST_PATH, CONFIG_PATH, fs, asyncForEach } = require('./utilities');
const tinify = require('tinify');
const uniq = require('lodash/uniq');
const filter = require('lodash/filter');
const map = require('lodash/map');
const find = require('lodash/find');
const crypto = require('crypto');
const glob = require('glob-all');
const svgo = new (require('svgo'))({
plugins: [
{ cleanupIDs: true },
{ prefixIds: true },
{ prefixClassNames: true },
{ removeHiddenElems: false },
{ removeViewBox: false },
]
});
if (!fs.existsSync(CONFIG_PATH)) {
return Exception('Cannot find config - make sure you run "npx minimage init" first.');
}
if (!fs.existsSync(MANIFEST_PATH)) {
return Exception('Cannot find manifest - make sure you run "npx minimage init" first.');
}
const manifest = require(MANIFEST_PATH);
const config = require(CONFIG_PATH);
// Adds backwards support for configs without "compress_if_larger_than_in_kb"
if (!('compress_if_larger_than_in_kb' in config)) {
config.compress_if_larger_than_in_kb = 0;
}
class Processor
{
constructor(config, manifest)
{
this.config = config;
this.manifest = manifest;
this.images = [];
this.queue = [];
this.currentIndex = 0;
}
prepareImages()
{
Log(`Minimage: Scanning ${this.config.paths.length} directories for files...`);
this.images = glob.sync(this.config.paths);
this.images = uniq(this.images);
this.images = filter(this.images, path => this.config.exclusions.indexOf(path) === -1);
Log(`Minimage: Found ${this.images.length} image(s) in total`);
this.queue = map(this.images, path => {
return this.parsePath(path, true);
});
this.queue = filter(this.queue, file => {
// It's normally null if the file has been skipped during this.parsePath();
if (!file) {
return false;
}
const previous = find(this.manifest, f => f.path === file.path);
if (!previous) {
return true;
}
if (previous.hash !== file.hash) {
return true;
}
return false;
});
Log(`Minimage: Found ${this.queue.length} new/updated image(s) to compress`);
return this;
}
parsePath(path, allowConditions)
{
const data = fs.readFileSync(path);
const hash = crypto
.createHash('md5')
.update(data, 'utf8')
.digest('hex');
const sizeInKb = Math.ceil(data.length / 1000);
// Everything inside here is allowed to reject the file.
if (allowConditions) {
// Only allow in files of the correct size.
if (sizeInKb <= this.config.compress_if_larger_than_in_kb) {
Log(`Minimage: Skipped ${path} as only ${sizeInKb}KB which is smaller than ${this.config.compress_if_larger_than_in_kb}KB`);
return null;
}
}
return {
path,
hash,
size: `${sizeInKb}KB`,
};
};
async startOptimising()
{
await asyncForEach(this.queue, async item => {
try {
this.currentIndex++;
await this.optimiseItem(item);
await this.updateManifest(item);
} catch (error) {
Exception(error);
return process.exit(1);
}
});
Log('Minimage: All images processed');
this.cleanupManifest();
return this;
}
optimiseItem(item)
{
if (item.path.indexOf('.svg') !== -1) {
return this.optimiseSvg(item);
}
return this.optimiseBitmap(item);
}
optimiseSvg({ path })
{
return new Promise(async (done, failed) => {
Log(`SVGO: Optimising ${path} :: ${this.currentIndex}/${this.queue.length}`);
try {
const originalXML = fs.readFileSync(path, 'utf8');
const { data } = await svgo.optimize(originalXML, { path });
fs.writeFile(path, data, () => done(path));
} catch (error) {
Log(error);
return failed(path);
}
})
}
optimiseBitmap({ path })
{
return new Promise((done, failed) => {
try {
Log(`TinyPNG: Compressing ${path} :: ${this.currentIndex}/${this.queue.length}`);
tinify.fromFile(path).toFile(path, error => error ? failed(error.message) : done(path))
} catch (error) {
Log(error);
return failed(path);
}
})
}
updateManifest({ path, size })
{
return new Promise(async (done, failed) => {
const file = this.parsePath(path);
const previous = find(this.manifest, f => f.path === file.path);
const toolName = path.indexOf('.svg') === -1 ? 'TinyPNG' : 'SVGO';
Log(`${toolName}: Reduced from ${size} to ${file.size}`);
if (!previous) {
this.manifest.push(file);
} else {
Object.assign(previous, file);
}
try {
await this.saveManifest(path);
return done(path);
} catch (error) {
Log(error);
return failed(path);
}
})
}
async cleanupManifest()
{
if (this.images.length === this.manifest.length) {
return this;
}
Log(`Minimage: Manifest contains ${this.manifest.length} files - but only ${this.images.length} images exist - Cleaning manifest now`);
this.manifest = filter(this.manifest, file => {
return find(this.images, f => f === file.path);
});
await this.saveManifest();
Log('Minimage: Manifest cleaned 👍')
}
saveManifest(data)
{
return new Promise((done, failed) => {
fs.writeFile(MANIFEST_PATH, JSON.stringify(this.manifest, null, 4), error => {
if (error) {
Log(error);
return failed(data);
}
return done(data);
});
})
}
finish()
{
Log(`Minimage: ${this.queue.length} image(s) successfully optimised`);
return this;
}
}
Log('TinyPNG: Connecting...');
tinify.key = process.env.TINYPNG_KEY || config.tinypng_key;
tinify.validate(err => {
if (err) throw err;
if (tinify.compressionCount >= 500) {
return Exception(
`TinyPNG: You've used ${
tinify.compressionCount
} compressions up - define a new api key for more`
);
} else {
let left = 500 - tinify.compressionCount;
Log(`TinyPNG: ${left} compressions left this month`);
}
const processor = new Processor(config, manifest);
processor
.prepareImages()
.startOptimising();
});