forked from imagemin/imagemin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·112 lines (94 loc) · 2.19 KB
/
cli.js
File metadata and controls
executable file
·112 lines (94 loc) · 2.19 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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var Imagemin = require('./');
var nopt = require('nopt');
var pkg = require('./package.json');
var stdin = require('get-stdin');
/**
* Options
*/
var opts = nopt({
help: Boolean,
interlaced: Boolean,
optimizationLevel: String,
out: String,
progressive: Boolean,
version: Boolean
}, {
h: '--help',
i: '--interlaced',
l: '--optimizationLevel',
o: '--out',
p: '--progressive',
v: '--version'
});
/**
* Help screen
*/
function help() {
console.log(pkg.description);
console.log('');
console.log('Usage');
console.log(' $ imagemin <file>');
console.log(' $ cat <file> | imagemin');
console.log('');
console.log('Example');
console.log(' $ imagemin foo.png > foo-optimized.png');
console.log(' $ cat foo.png | imagemin > foo-optimized.png');
console.log('');
console.log('Options');
console.log(' -i, --interlaced Extract archive files on download');
console.log(' -o, --optimizationLevel <number> Path to download or extract the files to');
console.log(' -p, --progressive Strip path segments from root when extracting');
}
/**
* Show help
*/
if (opts.help) {
help();
return;
}
/**
* Show package version
*/
if (opts.version) {
console.log(pkg.version);
return;
}
/**
* Run
*/
function run(input) {
var imagemin = new Imagemin()
.src(input)
.use(Imagemin.gifsicle(opts.interlaced))
.use(Imagemin.jpegtran(opts.progressive))
.use(Imagemin.optipng(opts.optimizationLevel))
.use(Imagemin.svgo());
imagemin.optimize(function (err, file) {
if (err) {
return console.log(err);
}
process.stdout.write(file.contents);
});
}
/**
* Apply arguments
*/
if (process.stdin.isTTY) {
var input = opts.argv.remain;
if (input.length > 1) {
return console.log('Only one input file allowed');
}
fs.readFile(input[0], function (err, data) {
if (err) {
return console.log(err);
}
run(data);
});
} else {
stdin(function (data) {
run(data);
});
}