-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·95 lines (81 loc) · 2.62 KB
/
bin.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
#!/usr/bin/env node
var prompt = require('prompt')
, fs = require('fs')
, findR = require('./lib/find-recursive')
, path = require('path')
, Imagemin = require('image-min')
, jpegRecompress = require('imagemin-jpeg-recompress')
;
prompt.message = '[jpegcompress]'.green;
// prompt.delimiter = ' > '.green;
var schema = {
properties: {
folder: {
name: 'folder',
description: 'define your folder (default current)',
message: 'the given folder does not exists',
default: '.',
conform: function(value) {
return fs.existsSync(value);
}
},
// imagemin-jpeg-recompress configuration
progressive: {
name: 'progressive',
description: '[jpeg] use progressive (Y/n)',
pattern: /^(y|n)$/,
default: 'y',
before: function(value) {
return (value === 'y') ? true : false;
}
},
maxQuality: {
name: 'maxQuality',
description: '[jpeg] define max quality',
pattern: /[0-9]{1,3}/,
default: 90,
before: function(value) {
value = parseInt(value, 10);
if (value > 100) { value = 100 }
if (value < 0) { value = 0 }
return value;
}
},
optimizationLevel: {
name: 'optimizationLevel',
description: '[png] define optimization level',
pattern: /[0-7]{1}/,
default: 3
}
}
}
prompt.start();
prompt.get(schema, function(err, result) {
if (err) { return; }
var jpgFiles = findR(result.folder, /(.jpg|.jpeg)$/i);
var pngFiles = findR(result.folder, /.png$/);
console.log('[jpegcompress] %s .png to compress', pngFiles.length);
console.log('[jpegcompress] %s .jpg to compress', jpgFiles.length);
jpgFiles.forEach(function(filename) {
var file = path.join(result.folder, filename);
var imagemin = new Imagemin()
.src(file)
.dest(file)
.use(jpegRecompress({
progressive: result.progressive,
loops: 3,
max: result.maxQuality
}));
imagemin.optimize();
});
pngFiles.forEach(function(filename) {
var file = path.join(result.folder, filename);
var imagemin = new Imagemin()
.src(file)
.dest(file)
.use(Imagemin.svgo())
.use(Imagemin.pngquant())
.use(Imagemin.optipng({ optimizationLevel: result.optimizationlevel }));
imagemin.optimize();
});
});