-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·204 lines (179 loc) · 4.61 KB
/
Copy pathcli.js
File metadata and controls
executable file
·204 lines (179 loc) · 4.61 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
#!/usr/bin/env node
const arrify = require('arrify');
const meow = require('meow');
const sharp = require('sharp');
const Promise = require("bluebird");
const getStdin = require('get-stdin');
const ora = require('ora');
const plur = require('plur');
const stripIndent = require('strip-indent');
const imagemin = require('imagemin');
const readChunk = require('read-chunk');
const imageType = require('image-type');
const path = require('path');
const fs = require('fs');
const cli = meow(`
Usage
$ picplus -c <path|glob> ... --out=build [--plugin=<name> ...]
$ picplus -r <path|file|glob>... --out=build [--width=<number> ...]
Options
-v, --version Print picplus version
-c, --compress Compress the images
-p, --plugin Override the imagemin default plugins
-q, --quality Set the quaility
-r, --resize Resize the images
-w, --width Geometric scaling the image to the width
-o, --out Output directory
Examples
$ picplus -c images/* --out=build
$ picplus -c --plugin=pngquant images/* --out=build
$ picplus -r --width=100 images/* --out=build
`, {
flags: {
version: {
type: 'boolean',
alias: 'v'
},
compress: {
type: 'boolean',
alias: 'c'
},
resize: {
type: 'boolean',
alias: 'r'
},
plugin: {
type: 'string',
alias: 'p'
},
out: {
type: 'string',
alias: 'o'
},
width: {
type: 'string',
alias: 'w'
},
force: {
type: 'boolean',
alias: 'f'
}
}
});
const DEFAULT_PLUGINS = [
'gifsicle',
'jpegtran',
'optipng',
'svgo'
];
const SHARP_SUPPORTED_RESIZE = [
'jpg',
'jpeg',
'png',
'gif',
'webp',
'tiff',
'tif'
];
function resolvePath(p) {
return path.resolve(process.cwd(), p);
}
if (cli.flags.out && !fs.existsSync(resolvePath(cli.flags.out))) {
fs.mkdirSync(resolvePath(cli.flags.out));
}
const requirePlugins = plugins => plugins.map(x => {
try {
return require(`imagemin-${x}`)();
} catch (err) {
console.error(stripIndent(`
Unknown plugin: ${x}
Did you forget to install the plugin?
You can install it with:
$ npm install -g imagemin-${x}
`).trim());
process.exit(1);
}
});
const compressRun = (input, opts) => {
opts = Object.assign({ plugin: DEFAULT_PLUGINS }, opts);
const use = requirePlugins(arrify(opts.plugin));
const spinner = ora('Minifying images');
// if (Buffer.isBuffer(input)) {
// imagemin.buffer(input, { use }).then(buf => process.stdout.write(buf));
// return;
// }
if(!opts.out && opts.force) {
opts.out = process.cwd();
}
if (opts.out) {
spinner.start();
}
imagemin(input, opts.out, { use })
.then(files => {
if (!opts.out && files.length === 0) {
console.error('Cannot be compressed !');
return;
}
if (!opts.out && files.length >= 1) {
console.error('Please specify a `--out` or override by `--force`');
process.exit(1);
}
// if (!opts.out) {
// process.stdout.write(files[0].data);
// return;
// }
spinner.stop();
console.log(`${files.length} ${plur('image', files.length)} minified`);
})
.catch(err => {
spinner.stop();
throw err;
});
};
const resizeHelper = (file, opts) => {
try {
let outdir = resolvePath(opts.out +'/'+ opts.width + '_' + path.basename(file));
// Can detect image types: jpg、png、gif、webp、tif、bmp、jxr、psd
let imgext = imageType(readChunk.sync(file, 0, 12)).ext;
if (SHARP_SUPPORTED_RESIZE.includes(imgext)) {
sharp(file).resize(+opts.width).toFile(`${outdir}`).then(data => {
console.log(`${file} resized ${data.width}*${data.height}`)
}).catch(err => {
console.error(`${file} ${err}`);
});
}
} catch(e) {
console.error(`${file} the file specified was an invalid or unsupported file format`);
}
};
const resizeRun = (input, opts) => {
if(!opts.out) {
console.error('Please specify a `--out` or `-o`');
process.exit(1);
}
for(let i = 0; i < input.length; i++) {
resizeHelper(input[i], opts);
}
};
if (!cli.input.length && process.stdin.isTTY) {
console.error('Please input some files !');
process.exit(1);
}
// Compress
if (cli.flags.compress && !cli.flags.resize) {
if (cli.input.length) {
compressRun(cli.input, cli.flags);
}
// else {
// getStdin.buffer().then(buf => compressRun(buf, cli.flags));
// }
}
// Resize
if (cli.flags.resize) {
if (cli.input.length) {
resizeRun(cli.input, cli.flags);
}
// else {
// getStdin.buffer().then(buf => run(buf, cli.flags));
// }
}