-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimage.js
executable file
·164 lines (127 loc) · 3.94 KB
/
optimage.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
#!/usr/bin/env node
const sharp = require('sharp');
const glob = require('glob');
class Optimage {
constructor(path, ignore, configPath) {
const { load, merge } = this.constructor;
this.config = merge({
path,
ignore,
cache: {},
options: {},
png: {},
webp: {},
jpeg: {},
gif: {},
}, load(configPath));
this.cores = sharp.concurrency();
this.length = 0;
this.files = null;
this.onFiles = this.onFiles.bind(this);
this.next = this.next.bind(this);
sharp.cache(this.config.cache);
}
static merge(defaultConfig, customConfig) {
const config = {};
for (const [key, value] of Object.entries(defaultConfig)) {
if (typeof value === 'object') {
config[key] = Object.assign(value, customConfig[key] || {});
} else {
config[key] = value || customConfig[key];
}
}
return config;
}
static load(path) {
if (!path) {
return {};
}
try {
return require(path);
} catch (error) {
try {
return require(`${process.cwd()}/${path}`);
} catch (error) {
throw new Error(`Could not load config file "${configPath}"`);
}
}
}
exec() {
const { path, ignore } = this.config;
if (!path) {
throw new Error('You must provide a path as first argument or through the "path" config key.');
}
glob(path, { ignore }, this.onFiles);
}
onFiles(error, files) {
if (error) {
throw new Error(error);
}
this.files = files;
this.length = files.length;
console.info(`Optimizing ${this.length} files with ${this.cores} cores...`);
for (let i = 0; i < this.cores; i++) {
setTimeout(this.next, 0);
}
}
next(file = undefined) {
const { length: total } = this;
const { length: remainging } = this.files;
// Previous treated file
if (file) {
const treated = total - remainging;
this.display(`File ${treated}/${total}: ${(treated/total * 100).toFixed(2)}% - ${file}`);
}
// Queue is empty
if (!remainging) {
this.cores--;
if (this.cores === 0) {
this.clear();
this.constructor.success('Done!');
}
return;
}
// Treat next file
this.optimize(this.files.shift(), this.next);
}
optimize(file, callback) {
const { options, jpeg, png, gif, webp } = this.config;
const write = (error, buffer) => sharp(buffer).toFile(file, () => callback(file));
switch (file.split('.').pop()) {
case 'jpeg':
case 'jpg':
return sharp(file).jpeg({ ...options, ...jpeg }).toBuffer(write);
case 'png':
return sharp(file).png({ ...options, ...png }).toBuffer(write);
case 'gif':
return sharp(file).gif({ ...options, ...gif }).toBuffer(write);
case 'webp':
return sharp(file).webp({ ...options, ...webp }).toBuffer(write);
default:
throw new Error(`Unsupported image type "${file}".`);
}
}
clear() {
process.stdout.clearLine();
process.stdout.cursorTo(0);
}
display(message) {
this.clear();
process.stdout.write(message);
}
static success(message) {
console.info(message);
process.exit(0);
}
static fail(message) {
console.error(message);
process.exit(1);
}
}
const { _: args, config: configPath } = require('minimist')(process.argv.slice(2));
const [ path, ignore ] = args;
try {
(new Optimage(path, ignore, configPath)).exec();
} catch (error) {
Optimage.fail(error);
}