-
Notifications
You must be signed in to change notification settings - Fork 21
/
cli.js
545 lines (487 loc) · 17 KB
/
cli.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*!
* The MIT License (MIT)
*
* Copyright (c) 2019 Mark van Seventer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Strict mode.
'use strict'
// Package modules.
const sharp = require('sharp')
const yargs = require('yargs')
// Local modules.
const constants = require('./constants')
const pkg = require('../package.json')
const queue = require('./queue')
// Configure.
const IS_TEXT_TERMINAL = process.stdin.isTTY
// Options.
const _global = 'Global Options'
const optimize = 'Optimization Options'
const options = {
// Global options.
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#png
compressionLevel: {
alias: 'c',
desc: 'zlib compression level',
defaultDescription: 9,
group: _global,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#toformat
format: {
alias: 'f',
choices: ['input', ...constants.FORMAT],
default: 'input',
desc: 'Force output to a given format',
group: _global,
nargs: 1
},
// @see https://sharp.pixelplumbing.com/en/stable/api-constructor/
input: {
alias: 'i',
defaultDescription: 'stdin',
demand: IS_TEXT_TERMINAL,
desc: 'Path to (an) image file(s)',
group: _global,
implies: 'output',
normalize: true,
type: 'array'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-input/#limitinputpixels
limitInputPixels: {
alias: 'l',
defaultDescription: 0x3FFF * 0x3FFF,
desc: 'Do not process input images where the number of pixels (width x height) exceeds this limit',
group: _global,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/
output: {
alias: 'o',
defaultDescription: 'stdout',
demand: IS_TEXT_TERMINAL,
desc: 'Directory or URI template to write the image files to',
group: _global,
nargs: 1,
normalize: true,
type: 'string'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#png
progressive: {
alias: 'p',
desc: 'Use progressive (interlace) scan',
group: _global,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#webp
quality: {
alias: 'q',
desc: 'Quality',
defaultDescription: '80',
group: _global,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#withmetadata
withMetadata: {
alias: 'm',
desc: 'Include all metadata (EXIF, XMP, IPTC) from the input image in the output image',
global: true,
group: _global,
type: 'boolean'
},
// Optimization options.
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#png
adaptiveFiltering: {
desc: 'Use adaptive row filtering',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#webp
alphaQuality: {
desc: 'Quality of alpha layer',
defaultDescription: '80',
group: optimize,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
chromaSubsampling: {
desc: 'Set to "4:4:4" to prevent chroma subsampling when quality <= 90',
defaultDescription: '4:2:0',
group: optimize,
nargs: 1,
type: 'string'
},
// @see https://sharp.dimens.io/en/stable/api-output/#png
colors: {
alias: 'colours',
defaultDescription: 256,
desc: 'Maximum number of palette entries',
group: optimize,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
compression: {
choices: constants.TIFF_COMPRESSION,
default: 'jpeg',
desc: 'Compression options',
group: optimize,
nargs: 1,
type: 'string'
},
// @see https://sharp.dimens.io/en/stable/api-output/#png
dither: {
desc: 'Level of Floyd-Steinberg error diffusion',
defaultDescription: '1.0',
group: optimize,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#heif
hcompression: {
choices: constants.HEIF_COMPRESSION,
default: 'hevc',
desc: 'Compression format',
group: optimize,
nargs: 1,
type: 'string'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#webp
lossless: {
desc: 'Use lossless compression mode',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#webp
nearLossless: {
desc: 'use near_lossless compression mode',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
optimise: {
alias: 'optimize',
desc: 'Apply optimiseScans, overshootDeringing, and trellisQuantisation',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
optimiseCoding: {
alias: 'optimizeCoding',
default: true,
desc: 'Optimise Huffman coding tables',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
optimiseScans: {
alias: 'optimizeScans',
desc: 'Optimise progressive scans',
group: optimize,
implies: 'progressive',
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
overshootDeringing: {
desc: 'Apply overshoot deringing',
group: optimize,
type: 'boolean'
},
// @see https://sharp.dimens.io/en/stable/api-output/#png
palette: {
desc: 'Quantise to a palette-based image with alpha transparency support',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
predictor: {
choices: constants.TIFF_PREDICTOR,
default: 'horizontal',
desc: 'Compression predictor',
group: optimize,
nargs: 1,
type: 'string'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
pyramid: {
desc: 'Write an image pyramid',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
quantisationTable: {
alias: 'quantizationTable',
defaultDescription: '0',
desc: 'Quantization table to use',
group: optimize,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#webp
reductionEffort: {
defaultDescription: 4,
desc: 'Level of CPU effort to reduce file size',
group: optimize,
nargs: 1,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-input/#sequentialread
sequentialRead: {
desc: 'An advanced setting that switches the libvips access method to VIPS_ACCESS_SEQUENTIAL',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#webp
smartSubsample: {
desc: 'High quality chroma subsampling',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
squash: {
desc: 'Squash 8-bit images down to 1 bit',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tile
tileBackground: {
defaultDescription: 'rgba(255, 255, 255, 1)',
desc: 'Background colour, parsed by the color module',
group: optimize,
type: 'string'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
tileHeight: {
desc: 'Vertical tile size',
group: optimize,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
tileWidth: {
desc: 'Horizontal tile size',
group: optimize,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
trellisQuantisation: {
desc: 'Apply trellis quantisation',
group: optimize,
type: 'boolean'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
xres: {
defaultDescription: '1.0',
desc: 'Horizontal resolution',
group: optimize,
type: 'number'
},
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
yres: {
defaultDescription: '1.0',
desc: 'Vertical resolution',
group: optimize,
type: 'number'
}
}
// Configure.
const cli = yargs
.strict()
.usage('$0 <options> [command..]')
.options(options)
.example('$0 -i ./input.jpg -o ./out resize 300 200', 'out/input.jpg will be a 300 pixels wide and 200 pixels high image containing a scaled and cropped version of input.jpg')
.example('$0 -i ./input.jpg -o ./out -mq90 rotate 180 -- resize 300 -- flatten "#ff6600" -- composite ./overlay.png --gravity southeast -- sharpen', 'out/input.jpg will be an upside down, 300px wide, alpha channel flattened onto orange background, composited with overlay.png with SE gravity, sharpened, with metadata, 90% quality version of input.jpg')
.epilog('For more information on available options, please visit https://sharp.pixelplumbing.com/')
.showHelpOnFail(false)
// Built-in options.
.help().alias('help', 'h')
.version(pkg.version).alias('version', 'v')
.group(['help', 'version'], 'Misc. Options')
// Commands.
// Avoid `yargs.commandDir()` as it uses insertion order, not alphabetical.
.command(require('../cmd/channel-manipulation/bandbool'))
.command(require('../cmd/operations/blur'))
.command(require('../cmd/operations/boolean'))
.command(require('../cmd/compositing/composite'))
.command(require('../cmd/operations/convolve'))
.command(require('../cmd/channel-manipulation/ensure-alpha'))
.command(require('../cmd/resizing/extend'))
.command(require('../cmd/resizing/extract'))
.command(require('../cmd/channel-manipulation/extract'))
.command(require('../cmd/operations/flatten'))
.command(require('../cmd/operations/flip'))
.command(require('../cmd/operations/flop'))
.command(require('../cmd/operations/gamma'))
.command(require('../cmd/colour-manipulation/greyscale'))
.command(require('../cmd/channel-manipulation/join'))
.command(require('../cmd/operations/linear'))
.command(require('../cmd/operations/median'))
.command(require('../cmd/operations/modulate'))
.command(require('../cmd/operations/negate'))
.command(require('../cmd/operations/normalise'))
.command(require('../cmd/operations/recomb'))
.command(require('../cmd/channel-manipulation/remove-alpha'))
.command(require('../cmd/resizing/resize'))
.command(require('../cmd/operations/rotate'))
.command(require('../cmd/operations/sharpen'))
.command(require('../cmd/operations/threshold'))
.command(require('../cmd/colour-manipulation/tint'))
.command(require('../cmd/output'))
.command(require('../cmd/colour-manipulation/tocolourspace'))
.command(require('../cmd/resizing/trim'))
// Override `cli.parse` to handle global options.
const originalParse = cli.parse
cli.parse = (argv, context, callback) => {
// Context is optional.
if (typeof context === 'function') {
callback = context
context = { }
}
// Parse and return the arguments.
return originalParse(argv, context, (err, args, output) => {
// Handle arguments.
// NOTE Use queue.unshift to apply global options first.
if (args !== null) {
// Require at least one input file.
// NOTE: check here b/c https://github.com/yargs/yargs/issues/403
if (args.input && args.input.length === 0) {
err = new Error('Not enough arguments following: i, input')
}
// Global Options.
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#toformat
if (args.format !== options.format.default) {
queue.unshift(['format', (sharp) => sharp.toFormat(args.format)])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#heif
const { heif } = sharp.format
if (args.hcompression !== options.hcompression.default || // HEIF-specific.
// Ensure libheif is installed before applying generic options.
(heif.input && heif.input.file && (args.lossless || args.quality))) {
queue.unshift(['heif', (sharp) => {
return sharp.heif({
compression: args.hcompression,
lossless: args.lossless,
quality: args.quality
})
}])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#jpeg
if (
args.chromaSubsampling ||
args.optimise ||
args.optimiseCoding !== true ||
args.optimiseScans ||
args.overshootDeringing ||
args.progressive ||
args.quantisationTable ||
args.quality ||
args.trellisQuantisation
) {
queue.unshift(['jpeg', (sharp) => {
return sharp.jpeg({
chromaSubsampling: args.chromaSubsampling,
force: false,
optimiseCoding: args.optimiseCoding,
optimiseScans: args.optimise || args.optimiseScans,
overshootDeringing: args.optimise || args.overshootDeringing,
progressive: args.progressive,
quality: args.quality,
quantisationTable: args.quantisationTable,
trellisQuantisation: args.optimise || args.trellisQuantisation
})
}])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-input/#limitinputpixels
if (undefined !== args.limitInputPixels) {
queue.unshift(['limitInputPixels', (sharp) => sharp.limitInputPixels(args.limitInputPixels)])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#png
if (args.adaptiveFiltering || args.colors || args.compressionLevel ||
args.dither || args.palette || args.progressive) {
queue.unshift(['png', (sharp) => {
return sharp.png({
adaptiveFiltering: args.adaptiveFiltering,
colors: args.colors,
compressionLevel: args.compressionLevel,
dither: args.dither,
force: false,
palette: args.palette,
progressive: args.progressive
})
}])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-input/#sequentialread
if (args.sequentialRead) {
queue.unshift(['sequentialRead', (sharp) => sharp.sequentialRead()])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#tiff
if (args.compression !== options.compression.default ||
args.predictor !== options.predictor.default ||
args.pyramid || args.quality || args.squash || args.tileBackground ||
args.tileHeight || args.tileWidth || args.xres || args.yres) {
queue.unshift(['tiff', (sharp) => {
return sharp.tiff({
background: args.tileBackground,
compression: args.compression,
force: false,
predictor: args.predictor,
pyramid: args.pyramid,
quality: args.quality,
squash: args.squash,
tile: args.tileWidth !== undefined || args.tileHeight !== undefined,
tileHeight: args.tileHeight || args.tileWidth,
tileWidth: args.tileWidth || args.tileHeight,
xres: args.xres,
yres: args.yres
})
}])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#webp
if (args.alphaQuality || args.quality || args.lossless ||
args.nearLossless || args.reductionEffort || args.smartSubsample) {
queue.unshift(['webp', (sharp) => {
return sharp.webp({
alphaQuality: args.alphaQuality,
force: false,
lossless: args.lossless,
nearLossless: args.nearLossless,
quality: args.quality,
reductionEffort: args.reductionEffort,
smartSubsample: args.smartSubsample
})
}])
}
// @see https://sharp.pixelplumbing.com/en/stable/api-output/#withmetadata
if (args.withMetadata) {
queue.unshift(['withMetadata', (sharp) => sharp.withMetadata()])
}
}
// Invoke original.
return callback(err, args, output)
})
}
// Exports.
module.exports = cli