-
Notifications
You must be signed in to change notification settings - Fork 79
/
promise.js
63 lines (46 loc) · 1.35 KB
/
promise.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
const compressImages = require("./index");
const EMPTY_ENGINE = { engine: false, command: false };
const STANDARD_PARAMS = {
compress_force: false,
statistic: true,
autoupdate: true,
};
const ENGINES = ["jpg", "png", "svg", "gif"];
const constructParams = (options) => {
const args = [];
args.push(options.source);
args.push(options.destination);
args.push(options.params || STANDARD_PARAMS);
args.push(options.globOptions || false);
const inputEngines = options.enginesSetup;
if (!inputEngines || !Object.keys(inputEngines).length) {
console.error("You have to specify atleast one engine!");
process.exit(1);
}
for (const engine of ENGINES) {
let data;
if (inputEngines[engine]) {
data = inputEngines[engine];
} else {
data = { ...EMPTY_ENGINE };
}
args.push({ [engine]: data });
}
return [args, options.onProgress];
};
const compress = (options) => {
const [params, callback] = constructParams(options);
const errors = [];
const statistics = [];
return new Promise((res) => {
compressImages(...params, (error, completed, statistic) => {
if (error) {
errors.push(error);
}
statistics.push(statistic);
if (callback) callback(error, statistic, completed);
if (completed) res({ statistics, errors });
});
});
};
module.exports = { compress };