-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (59 loc) · 2.17 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
#!/usr/bin/env node
require('shelljs/global');
const fs = require("fs");
const path = require('path');
const program = require('commander');
const jimp = require("jimp");
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');
// supported file types.
const exts = ['.jpg', '.jpeg', '.png'];
program
.version('1.0.6')
.option('-s, --scale', 'image scaling')
.option('-e, --extra', 'extra string append to the file name')
.option('-f, --folder', 'original image folder name')
.option('-o, --output', 'output image folder name')
.parse(process.argv);
let scale = program.scale ? parseFloat(program.scale) : 0.5;
let imageFolderName = program.folder || 'images';
let outputFolderName = program.folder || 'output';
let extra = program.extra || '@small';
if (!fs.existsSync(imageFolderName)) {
console.error('The original image folder not exists. Please check again.');
process.exit(1);
}
// read the original image folder.
fs.readdir(imageFolderName, (err, files) => {
let count = 0;
let resizeCount = 0;
files.forEach((name) => {
if (exts.indexOf(path.extname(name)) === -1) {
return;
}
count++;
jimp.read(path.join(imageFolderName, name), (err, lenna) => {
if (err) throw err;
// if output folder not exists, create it.
if (!fs.existsSync(outputFolderName)) {
fs.mkdirSync(outputFolderName);
}
lenna.scale(scale)
.write(path.join(outputFolderName, name.replace(/\.[^/.]+$/, "") + extra + path.extname(name)));
resizeCount++;
// resize complete.
// start compress.
if(count === resizeCount) {
imagemin([outputFolderName + '/*{' + exts.join(',') + '}'], outputFolderName, {
plugins: [
imageminMozjpeg(),
imageminPngquant({quality: '65-80'})
]
}).then(files => {
console.info('Complete!')
});
}
});
});
});