forked from imagemin/imagemin-pngquant
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·58 lines (46 loc) · 1.02 KB
/
index.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
'use strict';
const execBuffer = require('exec-buffer');
const isPng = require('is-png');
const pngquant = require('pngquant-bin-static');
module.exports = opts => buf => {
opts = Object.assign({}, opts);
if (!Buffer.isBuffer(buf)) {
return Promise.reject(new TypeError('Expected a buffer'));
}
if (!isPng(buf)) {
return Promise.resolve(buf);
}
const args = [
'--output', execBuffer.output,
execBuffer.input
];
if (opts.floyd && typeof opts.floyd === 'number') {
args.push(`--floyd=${opts.floyd}`);
}
if (opts.floyd && typeof opts.floyd === 'boolean') {
args.push('--floyd');
}
if (opts.nofs) {
args.push('--nofs');
}
if (opts.posterize) {
args.push('--posterize', opts.posterize);
}
if (opts.quality) {
args.push('--quality', opts.quality);
}
if (opts.speed) {
args.push('--speed', opts.speed);
}
if (opts.verbose) {
args.push('--verbose');
}
return execBuffer({
input: buf,
bin: pngquant,
args
}).catch(err => {
err.message = err.stderr || err.message;
throw err;
});
};