-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (107 loc) · 2.73 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
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
import {Buffer} from 'node:buffer';
import execBuffer from 'exec-buffer';
import isPng from 'is-png';
import oxipng from 'oxipng-bin';
const imageminOxipng = (options = {}) => input => {
options = {...options};
if (!Buffer.isBuffer(input)) {
return Promise.reject(new TypeError(`Expected \`input\` to be of type \`Buffer\` but received type \`${typeof input}\``));
}
if (!isPng(input)) {
return Promise.resolve(input);
}
const args = [];
[ 'optimization', 'optimisation', 'opt', 'o' ].forEach(k => {
if (typeof options[k] === 'string' && options[k] === 'max') {
args.push('-o', options[k]);
} else if (
typeof options[k] === 'number' &&
0 <= options[k] &&
6 >= options[k]
) {
args.push('-o', options[k]);
}
});
[ 'preserve', 'p' ].forEach(k => {
if (typeof options[k] !== 'undefined' && Boolean(options[k])) {
args.push('-p');
}
});
[ 'check', 'c' ].forEach(k => {
if (typeof options[k] !== 'undefined' && Boolean(options[k])) {
args.push('-c');
}
});
if (typeof options.strip === 'string') {
args.push('--strip', options.strip);
} else if (typeof options.strip === 'object' && Array.isArray(options.strip)) {
args.push('--strip', options.strip.join(','));
} else if (typeof options.s !== 'undefined' && Boolean(options.s)) {
args.push('-s');
} else if (typeof options.keep === 'object' && Array.isArray(options.keep)) {
args.push('--keep', options.keep.join(','));
}
[ 'alpha', 'a' ].forEach(k => {
if (typeof options[k] !== 'undefined' && Boolean(options[k])) {
args.push('-a');
}
});
[ 'interlace', 'i' ].forEach(k => {
if (typeof options[k] === 'number') {
args.push('-i', options[k]);
}
});
[ 'filters', 'f' ].forEach(k => {
if (
typeof options[k] !== 'undefined' &&
Array.isArray(options[k]) &&
options[k].filter(v => 0 <= v && 9 >= v).length > 0
) {
args.push('-f', options[k].filter(v => 0 <= v && 9 >= v).join(','));
}
});
[ 'zc' ].forEach(k => {
if (
typeof options[k] === 'number' &&
1 <= options[k] &&
12 >= options[k]
) {
args.push('--zc', options[k]);
}
});
[
'fast',
'nb',
'nc',
'np',
'ng',
'nx',
'nz',
'fix',
'force',
].forEach(k => {
if (typeof options[k] !== 'undefined' && Boolean(options[k])) {
args.push(`--${k}`);
}
});
[ 'zopfli', 'Z' ].forEach(k => {
if (typeof options[k] !== 'undefined' && Boolean(options[k])) {
args.push('-Z');
}
});
[ 'timeout' ].forEach(k => {
if (typeof options[k] === 'number') {
args.push('--timeout', options[k]);
}
});
args.push('--out', execBuffer.output, execBuffer.input);
return execBuffer({
input,
bin: oxipng,
args
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
});
};
export default imageminOxipng;