forked from tomatolicious/tinypng-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinypng-cli.js
More file actions
309 lines (247 loc) · 10.1 KB
/
tinypng-cli.js
File metadata and controls
309 lines (247 loc) · 10.1 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env node
const fs = require('fs');
const request = require('request');
const minimatch = require('minimatch');
const glob = require('glob');
const uniq = require('array-uniq');
const chalk = require('chalk');
const pretty = require('prettysize');
const imageinfo = require('imageinfo');
const md5 = require('js-md5');
const emptyFolder = require('empty-folder');
var argv = require('minimist')(process.argv.slice(2));
var home = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var version = require('./package.json').version;
var cacheDirectory = home + '/.tinypng-cache/';
if (argv.v || argv.version) {
console.log(version);
} else if (argv.h || argv.help) {
console.log(
'Usage\n' +
' tinypng <path>\n' +
'\n' +
'Example\n' +
' tinypng .\n' +
' tinypng assets/img\n' +
' tinypng assets/img/test.png\n' +
' tinypng assets/img/test.jpg\n' +
'\n' +
'Options\n' +
' -k, --key Provide an API key\n' +
' -r, --recursive Walk given directory recursively\n' +
' --width Resize an image to a specified width\n' +
' --height Resize an image to a specified height\n' +
' --minimal Minimal compression we accept (in %)\n' +
' --resize-mode Specify the resize method to use (scale, fit or cover)\n' +
' --if-larger-than Optimize only if width or height is bigger than the provided value (in pixels)\n' +
' --if-bigger-than Optimize only if file weight if bigger than the provided value (in bytes)\n' +
' --clear-cache Clear cache and stop\n' +
' --bypass-cache Bypass cache and force optimisation\n' +
' -v, --version Show installed version\n' +
' -V, --verbose Verbose mode\n' +
' -h, --help Show help'
);
} else {
console.log(chalk.underline.bold('TinyPNG CLI'));
console.log('v' + version + '\n');
var files = argv._.length ? argv._ : ['.'];
var key = '';
var resize = {};
var resize_modes = ['scale', 'fit', 'cover'];
var verbose = false;
var bypassCache = false;
if (argv.k || argv.key) {
key = typeof(argv.k || argv.key) === 'string' ? (argv.k || argv.key).trim() : '';
} else if (fs.existsSync(home + '/.tinypng')) {
key = fs.readFileSync(home + '/.tinypng', 'utf8').trim();
}
if (!fs.existsSync(cacheDirectory)) {
fs.mkdirSync(cacheDirectory);
console.log(chalk.bold(`Cache directory does not exist:`) + ` creating ${cacheDirectory}`);
}
if (argv['clear-cache']) {
if (fs.existsSync(cacheDirectory)) {
emptyFolder(cacheDirectory, false, (o) => {
if (o.error) {
console.log(chalk.bold(`Cannot empty cache directory: ` + err));
return;
}
});
}
console.log(chalk.bold(`Cache is clean`));
process.exit(-1);
}
if (argv['bypass-cache']) {
bypassCache = true;
}
if (argv.V || argv.verbose) {
console.log('. Verbose mode is ON');
verbose = true;
}
if (argv.width) {
if (typeof(argv.width) === 'number') {
resize.width = argv.width;
} else {
console.log(chalk.bold.red('Invalid width specified. Please specify a numeric value only.'));
process.exit(-1);
}
}
if (argv.height) {
if (typeof(argv.height) === 'number') {
resize.height = argv.height;
} else {
console.log(chalk.bold.red('Invalid height specified. Please specify a numeric value only.'));
process.exit(-1);
}
}
if (argv.minimal) {
if (typeof(argv.minimal) === 'number') {
resize.minimal = argv.minimal;
console.log(`. Acceptable minimal efficiency is set to ${resize.minimal}%`);
} else {
console.log(chalk.bold.red('Invalid minimal specified. Please specify a numeric value only.'));
process.exit(-1);
}
}
if (argv['resize-method']) {
if (typeof(argv['resize-method']) === 'string' && resize_modes.includes(argv['resize-method'].toLowerCase())) {
if (argv['resize-method'] != 'scale' && resize.width === undefined || argv['resize-method'] != 'scale' && resize.height === undefined) {
console.log(chalk.bold.red('This resize mode requires you to specify a width and a height.'));
} else {
resize.method = argv['resize-method'];
}
} else {
console.log(chalk.bold.red(`Invalid resize mode specified. Valid modes are: ${resize_modes.join(', ')}`));
process.exit(-1);
}
}
if (argv['if-larger-than']) {
if (typeof(argv['if-larger-than']) === 'number') {
resize.maxSize = argv['if-larger-than'];
console.log('. Image(s) should be larger than ' + resize.maxSize + ' pixels (width or height) to be processed\n');
} else {
console.log(chalk.bold.red(`Invalid size specified. Please use a number (in pixels).`));
process.exit(-1);
}
}
if (argv['if-bigger-than']) {
if (typeof(argv['if-bigger-than']) === 'number') {
resize.maxWeight = argv['if-bigger-than'];
console.log('. Files should be bigger than ' + pretty(resize.maxWeight) + ' to be processed\n');
} else {
console.log(chalk.bold.red(`Invalid weight specified. Please use a number (in bytes).`));
process.exit(-1);
}
}
if (key.length === 0) {
console.log(chalk.bold.red('No API key specified. You can get one at ' + chalk.underline('https://tinypng.com/developers') + '.'));
process.exit(-1);
} else {
var images = [];
files.forEach(function(file) {
if (fs.existsSync(file)) {
if (fs.lstatSync(file).isDirectory()) {
images = images.concat(glob.sync(file + (argv.r || argv.recursive ? '/**' : '') + '/*.+(png|jpg|jpeg|PNG|JPG|JPEG)'));
} else if (minimatch(file, '*.+(png|jpg|jpeg|PNG|JPG|JPEG)', {
matchBase: true
})) {
images.push(file);
}
}
});
var unique = uniq(images);
if (unique.length === 0) {
console.log(chalk.bold.red('\u2718 No PNG or JPEG images found.'));
} else {
var optimizedCounter;
process.on('exit', function() {
if (optimizedCounter) console.log(chalk.bold.green('\u2714 Number of optimized files this month: ' + optimizedCounter));
});
console.log(chalk.bold.green('\u2714 Found ' + unique.length + ' image' + (unique.length === 1 ? '' : 's')) + '\n');
console.log(chalk.bold('Processing...'));
unique.forEach(function(file) {
fs.readFile(file, function(err, data) {
if (err) throw err;
var push = true;
var info = imageinfo(data);
if (resize.maxSize) {
var thisMaxSize = Math.max(info.width, info.height);
if (thisMaxSize < resize.maxSize) {
if (verbose) console.log(chalk.red('\u0021 Image is too small (' + info.width + 'x' + info.height + '): `' + file + '`'));
push = false;
}
}
if (resize.maxWeight) {
if (data.length < resize.maxWeight) {
if (verbose) console.log(chalk.red('\u0021 File is too light ' + chalk.bold(pretty(data.length)) + ': `' + file + '`'));
push = false;
}
}
var hash = md5(file + JSON.stringify(resize));
if (fs.existsSync(`${cacheDirectory}/${hash}`) && bypassCache === false) {
if (verbose) console.log(chalk.red(`\u0021 File already packed: ${file}`));
push = false;
}
if (push === true) {
fs.createReadStream(file).pipe(request.post('https://api.tinify.com/shrink', {
auth: {
'user': 'api',
'pass': key
}
}, function(error, response, body) {
try {
body = JSON.parse(body);
} catch (e) {
console.log(chalk.red('\u2718 Not a valid JSON response for `' + file + '`'));
return;
}
if (!error && response) {
if (response.statusCode === 201) {
optimizedCounter = response.headers['compression-count'];
var efficiency = Math.round(100 - 100 / body.input.size * body.output.size);
var save = true;
if (efficiency < resize.minimal) save = false;
if (body.output.size < body.input.size && save === true) {
console.log(chalk.green('\u2714 Panda just saved you ' +
chalk.bold(pretty(body.input.size - body.output.size) + ` (${efficiency}%)`) +
` for ${file}`));
fs.writeFile(cacheDirectory + hash, hash, (error) => {
if (error) {
console.log(chalk.red('\u2718 Cannot write cache file'));
}
});
if (resize.hasOwnProperty('height') || resize.hasOwnProperty('width')) {
request.get(body.output.url, {
auth: {
'user': 'api',
'pass': key
},
json: {
'resize': resize
}
}).pipe(fs.createWriteStream(file));
} else {
request.get(body.output.url).pipe(fs.createWriteStream(file));
}
} else {
if (verbose) console.log(chalk.yellow('\u2718 Couldn’t compress ' + chalk.bold(file) + ' any further'));
}
} else {
if (body.error === 'TooManyRequests') {
console.log(chalk.red('\u2718 Compression failed for `' + file + '` as your monthly limit has been exceeded'));
} else if (body.error === 'Unauthorized') {
console.log(chalk.red('\u2718 Compression failed for `' + file + '` as your credentials are invalid'));
} else {
console.log(chalk.red('\u2718 Compression failed for `' + file + '`'));
}
}
} else {
console.log(chalk.red('\u2718 Got no response for `' + file + '`'));
}
}));
}
});
});
}
}
}