-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-optimizer-cli.mjs
executable file
·99 lines (83 loc) · 2.59 KB
/
image-optimizer-cli.mjs
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
#! /usr/bin/env node
import fs from 'fs';
import path from 'path';
import yargs from 'yargs';
import imagemin from 'imagemin';
import { hideBin } from 'yargs/helpers';
import imageminSvgo from 'imagemin-svgo';
import imageminJpegtran from 'imagemin-jpegtran';
import imageminPngquant from 'imagemin-pngquant';
import imageminGifsicle from 'imagemin-gifsicle';
const { argv } = yargs(hideBin(process.argv))
.option('source', {
alias: 's',
type: 'string',
requiresArg: true,
describe: 'source directory with images to optimize',
})
.option('destination', {
alias: 'd',
type: 'string',
requiresArg: true,
describe: 'the folder where you want to save the optimized images',
})
.demandOption(['s', 'd'])
.help();
const { source, destination } = argv;
const projectDirectory = process.cwd();
const sourceProjectDirectory = `${projectDirectory}/${source}`;
const destinationProjectDirectory = `${projectDirectory}/${destination}`;
try {
const sourceDirectoryStat = fs.statSync(sourceProjectDirectory);
if (sourceDirectoryStat.isFile()) {
throw new Error('source directory is not a directory');
}
const destinationDirectoryStat = fs.statSync(destinationProjectDirectory);
if (destinationDirectoryStat.isFile()) {
throw new Error('destination directory is not a directory');
}
} catch (e) {
console.error(e);
}
function getFilesizeInBytes(filename) {
const stats = fs.statSync(filename);
return stats.size;
}
function niceBytes(value) {
const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let l = 0;
let n = parseInt(value, 10) || 0;
while (n >= 1024 && ++l) {
n = n / 1024;
}
return n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l];
}
imagemin([`${sourceProjectDirectory}/**/*.{jpg,png,svg,gif}`], {
plugins: [
imageminJpegtran({
progressive: true,
}),
imageminPngquant({
strip: true,
quality: [0.1, 0.6],
}),
imageminSvgo(),
imageminGifsicle({
optimizationLevel: 3,
}),
],
}).then((_files) => {
_files.forEach((_file) => {
const source = path.parse(_file.sourcePath);
const filenameWithExtension = `${source.name}${source.ext}`;
const destinationPath = `${destinationProjectDirectory}/${filenameWithExtension}`;
fs.writeFileSync(destinationPath, _file.data);
const newFileSize = getFilesizeInBytes(destinationPath);
const oldFileSize = getFilesizeInBytes(_file.sourcePath);
console.log(
`Filename: '${filenameWithExtension}' was optimised from '${niceBytes(
oldFileSize,
)}' to '${niceBytes(newFileSize)}'.`,
);
});
});