@depup/sharp-cli 5.2.0-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/.gitattributes +1 -0
  2. package/CHANGELOG.md +336 -0
  3. package/LICENSE.txt +20 -0
  4. package/README.md +35 -0
  5. package/bin/cli.js +32 -0
  6. package/changes.json +26 -0
  7. package/cmd/channel-manipulation/bandbool.js +61 -0
  8. package/cmd/channel-manipulation/ensure-alpha.js +62 -0
  9. package/cmd/channel-manipulation/extract-channel.js +61 -0
  10. package/cmd/channel-manipulation/join-channel.js +60 -0
  11. package/cmd/channel-manipulation/remove-alpha.js +49 -0
  12. package/cmd/colour-manipulation/greyscale.js +50 -0
  13. package/cmd/colour-manipulation/pipeline-colourspace.js +63 -0
  14. package/cmd/colour-manipulation/tint.js +58 -0
  15. package/cmd/colour-manipulation/tocolourspace.js +63 -0
  16. package/cmd/compositing/composite.js +187 -0
  17. package/cmd/operations/affine.js +110 -0
  18. package/cmd/operations/blur.js +87 -0
  19. package/cmd/operations/boolean.js +66 -0
  20. package/cmd/operations/clahe.js +82 -0
  21. package/cmd/operations/convolve.js +103 -0
  22. package/cmd/operations/flatten.js +63 -0
  23. package/cmd/operations/flip.js +49 -0
  24. package/cmd/operations/flop.js +49 -0
  25. package/cmd/operations/gamma.js +66 -0
  26. package/cmd/operations/linear.js +73 -0
  27. package/cmd/operations/median.js +62 -0
  28. package/cmd/operations/modulate.js +78 -0
  29. package/cmd/operations/negate.js +60 -0
  30. package/cmd/operations/normalise.js +69 -0
  31. package/cmd/operations/recomb.js +74 -0
  32. package/cmd/operations/rotate.js +72 -0
  33. package/cmd/operations/sharpen.js +109 -0
  34. package/cmd/operations/threshold.js +73 -0
  35. package/cmd/operations/unflatten.js +49 -0
  36. package/cmd/output.js +125 -0
  37. package/cmd/resizing/extend.js +102 -0
  38. package/cmd/resizing/extract.js +79 -0
  39. package/cmd/resizing/resize.js +124 -0
  40. package/cmd/resizing/trim.js +82 -0
  41. package/lib/cli.js +800 -0
  42. package/lib/constants.js +55 -0
  43. package/lib/convert.js +121 -0
  44. package/lib/index.js +58 -0
  45. package/lib/queue.js +44 -0
  46. package/package.json +84 -0
package/lib/cli.js ADDED
@@ -0,0 +1,800 @@
1
+ /*!
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2019 Mark van Seventer
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ * this software and associated documentation files (the "Software"), to deal in
8
+ * the Software without restriction, including without limitation the rights to
9
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
+ * the Software, and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ // Strict mode.
25
+ 'use strict'
26
+
27
+ // Package modules.
28
+ const pick = require('lodash.pick')
29
+ const sharp = require('sharp')
30
+ const yargs = require('yargs')
31
+
32
+ // Local modules.
33
+ const constants = require('./constants')
34
+ const pkg = require('../package.json')
35
+ const queue = require('./queue')
36
+
37
+ // Configure.
38
+ const IS_TEXT_TERMINAL = process.stdin.isTTY
39
+
40
+ // Options.
41
+ const global = 'Global Options'
42
+ const input = 'Input Options'
43
+ const optimize = 'Optimization Options'
44
+ const output = 'Output Options'
45
+
46
+ const globalOptions = {
47
+ // @see https://sharp.pixelplumbing.com/api-constructor/
48
+ input: {
49
+ alias: 'i',
50
+ defaultDescription: 'stdin',
51
+ demand: IS_TEXT_TERMINAL,
52
+ desc: 'Path to (an) image file(s)',
53
+ group: global,
54
+ implies: 'output',
55
+ type: 'array'
56
+ },
57
+
58
+ // @see https://sharp.pixelplumbing.com/api-output/
59
+ output: {
60
+ alias: 'o',
61
+ defaultDescription: 'stdout',
62
+ demand: IS_TEXT_TERMINAL,
63
+ desc: 'Directory or URI template to write the image files to',
64
+ group: global,
65
+ type: 'string'
66
+ },
67
+
68
+ // @see https://sharp.pixelplumbing.com/api-output#timeout
69
+ timeout: {
70
+ desc: 'Number of seconds after which processing will be stopped',
71
+ group: global,
72
+ type: 'number'
73
+ }
74
+ }
75
+
76
+ // @see https://sharp.pixelplumbing.com/api-constructor
77
+ const inputOptions = {
78
+ animated: {
79
+ desc: 'Read all frames/pages of an animated image',
80
+ group: input,
81
+ type: 'boolean'
82
+ },
83
+ autoOrient: {
84
+ desc: 'Rotate/flip the image to match EXIF Orientation, if any',
85
+ group: input,
86
+ type: 'boolean'
87
+ },
88
+ failOn: {
89
+ choices: constants.FAIL_ON,
90
+ defaultDescription: 'warning',
91
+ desc: 'Level of sensitivity to invalid images',
92
+ group: input
93
+ },
94
+ density: {
95
+ desc: 'DPI for vector images',
96
+ defaultDescription: 72,
97
+ group: input,
98
+ type: 'number'
99
+ },
100
+ ignoreIcc: {
101
+ default: false,
102
+ desc: 'Should the embedded ICC profile, if any, be ignored',
103
+ group: input,
104
+ type: 'boolean'
105
+ },
106
+ level: {
107
+ desc: 'Level to extract from a multi-level input (OpenSlide), zero based',
108
+ defaultDescription: 0,
109
+ group: input,
110
+ type: 'number'
111
+ },
112
+ limitInputPixels: {
113
+ defaultDescription: 0x3FFF * 0x3FFF,
114
+ desc: 'Do not process input images where the number of pixels (width x height) exceeds this limit',
115
+ group: input,
116
+ type: 'number'
117
+ },
118
+ page: {
119
+ defaultDescription: 0,
120
+ desc: 'Page number to start extracting from for multi-page input',
121
+ group: input,
122
+ type: 'number'
123
+ },
124
+ pages: {
125
+ defaultDescription: 1,
126
+ desc: 'Number of pages to extract for multi-page input',
127
+ group: input,
128
+ type: 'number'
129
+ },
130
+ pdfBackground: {
131
+ desc: 'Background colour to use when PDF is partially transparent',
132
+ group: input,
133
+ type: 'string'
134
+ },
135
+ sequentialRead: {
136
+ default: false,
137
+ desc: 'Use sequential rather than random access where possible',
138
+ group: input,
139
+ type: 'boolean'
140
+ },
141
+ subifd: {
142
+ defaultDescription: -1,
143
+ desc: 'subIFD to extract for OME-TIFF',
144
+ group: input,
145
+ type: 'number'
146
+ },
147
+ unlimited: {
148
+ desc: 'Remove safety features that help prevent memory exhaustion',
149
+ group: input,
150
+ type: 'boolean'
151
+ }
152
+ }
153
+
154
+ const outputOptions = {
155
+ // @see https://sharp.pixelplumbing.com/api-output#png
156
+ compressionLevel: {
157
+ alias: 'c',
158
+ desc: 'zlib compression level',
159
+ defaultDescription: 6,
160
+ group: output,
161
+ type: 'number'
162
+ },
163
+
164
+ // @see https://sharp.pixelplumbing.com/api-output#toformat
165
+ format: {
166
+ alias: 'f',
167
+ choices: constants.FORMAT,
168
+ defaultDescription: 'input',
169
+ desc: 'Force output to a given format',
170
+ group: output
171
+ },
172
+
173
+ // @see https://sharp.pixelplumbing.com/api-output#withmetadata
174
+ metadata: {
175
+ alias: ['m', 'withMetadata'],
176
+ desc: 'Include all metadata (EXIF, XMP, IPTC) from the input image in the output image',
177
+ group: output,
178
+ type: 'boolean'
179
+ },
180
+ 'metadata.density': {
181
+ desc: 'Number of pixels per inch (DPI)',
182
+ group: output,
183
+ type: 'number'
184
+ },
185
+ 'metadata.exif': {
186
+ defaultDescription: '{}',
187
+ desc: 'Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data',
188
+ group: output,
189
+ type: 'object'
190
+ },
191
+ 'metadata.icc': {
192
+ defaultDescription: 'sRGB',
193
+ desc: 'Filesystem path to output ICC profile',
194
+ group: output,
195
+ type: 'string'
196
+ },
197
+ 'metadata.orientation': {
198
+ desc: 'Used to update the EXIF Orientation tag',
199
+ group: output,
200
+ type: 'number'
201
+ },
202
+
203
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
204
+ // @see https://sharp.pixelplumbing.com/api-output#png
205
+ progressive: {
206
+ alias: 'p',
207
+ desc: 'Use progressive (interlace) scan',
208
+ group: output,
209
+ type: 'boolean'
210
+ },
211
+
212
+ // @see https://sharp.pixelplumbing.com/api-output#avif
213
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
214
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
215
+ // @see https://sharp.pixelplumbing.com/api-output#webp
216
+ quality: {
217
+ alias: 'q',
218
+ desc: 'Quality',
219
+ defaultDescription: '80',
220
+ group: output,
221
+ type: 'number'
222
+ }
223
+ }
224
+
225
+ const optimizationOptions = {
226
+ // @see https://sharp.pixelplumbing.com/api-output#png
227
+ adaptiveFiltering: {
228
+ desc: 'Use adaptive row filtering',
229
+ group: optimize,
230
+ type: 'boolean'
231
+ },
232
+
233
+ // @see https://sharp.pixelplumbing.com/api-output#webp
234
+ alphaQuality: {
235
+ desc: 'Quality of alpha layer',
236
+ defaultDescription: '80',
237
+ group: optimize,
238
+ type: 'number'
239
+ },
240
+
241
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
242
+ bitdepth: {
243
+ choices: [1, 2, 4, 8],
244
+ defaultDescription: 8,
245
+ desc: 'Reduce bitdepth to 1, 2, or 4 bit',
246
+ group: optimize
247
+ },
248
+
249
+ // @see https://sharp.pixelplumbing.com/api-output#avif
250
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
251
+ chromaSubsampling: {
252
+ desc: 'Set to "4:4:4" to prevent chroma subsampling when quality <= 90',
253
+ defaultDescription: '4:4:4 (AVIF) / 4:2:0',
254
+ group: optimize,
255
+ type: 'string'
256
+ },
257
+
258
+ // @see https://sharp.pixelplumbing.com/api-output#gif
259
+ // @see https://sharp.dimens.io/api-output#png
260
+ colors: {
261
+ alias: 'colours',
262
+ defaultDescription: 256,
263
+ desc: 'Maximum number of palette entries',
264
+ group: optimize,
265
+ type: 'number'
266
+ },
267
+
268
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
269
+ compression: {
270
+ choices: constants.TIFF_COMPRESSION,
271
+ default: 'jpeg',
272
+ desc: 'Compression options',
273
+ group: optimize
274
+ },
275
+
276
+ // @see https://sharp.pixelplumbing.com/api-output#gif
277
+ delay: {
278
+ desc: 'Delay(s) between animation frames',
279
+ group: optimize,
280
+ type: 'number'
281
+ },
282
+
283
+ // @see https://sharp.pixelplumbing.com/api-output#gif
284
+ // @see https://sharp.dimens.io/api-output#png
285
+ dither: {
286
+ desc: 'Level of Floyd-Steinberg error diffusion',
287
+ defaultDescription: '1.0',
288
+ group: optimize,
289
+ type: 'number'
290
+ },
291
+
292
+ // @see https://sharp.pixelplumbing.com/api-output#avif
293
+ // @see https://sharp.pixelplumbing.com/api-output#gif
294
+ // @see https://sharp.pixelplumbing.com/api-output#heif
295
+ // @see https://sharp.pixelplumbing.com/api-output#png
296
+ // @see https://sharp.pixelplumbing.com/api-output#webp
297
+ effort: {
298
+ defaultDescription: '7 (GIF, PNG) / 4',
299
+ desc: 'Level of CPU effort to reduce file size',
300
+ group: optimize,
301
+ type: 'number'
302
+ },
303
+
304
+ // @see https://sharp.pixelplumbing.com/api-output#heif
305
+ hbitdepth: {
306
+ choices: [8, 10, 12],
307
+ defaultDescription: 8,
308
+ desc: 'Set bitdepth to 8, 10, or 12 bit',
309
+ group: optimize
310
+ },
311
+
312
+ // @see https://sharp.pixelplumbing.com/api-output#heif
313
+ hcompression: {
314
+ choices: constants.HEIF_COMPRESSION,
315
+ default: 'av1',
316
+ desc: 'Compression format',
317
+ group: optimize
318
+ },
319
+
320
+ // @see https://sharp.pixelplumbing.com/api-output#gif
321
+ interFrameMaxError: {
322
+ desc: 'Maximum inter-frame error for transparency',
323
+ group: optimize,
324
+ type: 'number'
325
+ },
326
+
327
+ // @see https://sharp.pixelplumbing.com/api-output#gif
328
+ interPaletteMaxError: {
329
+ desc: 'Maximum inter-palette error for palette reuse',
330
+ group: optimize,
331
+ type: 'number'
332
+ },
333
+
334
+ // @see https://sharp.pixelplumbing.com/api-output#gif
335
+ loop: {
336
+ default: 0,
337
+ desc: 'Number of animation iterations',
338
+ group: optimize,
339
+ type: 'number'
340
+ },
341
+
342
+ // @see https://sharp.pixelplumbing.com/api-output#avif
343
+ // @see https://sharp.pixelplumbing.com/api-output#webp
344
+ lossless: {
345
+ desc: 'Use lossless compression mode',
346
+ group: optimize,
347
+ type: 'boolean'
348
+ },
349
+
350
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
351
+ miniswhite: {
352
+ desc: 'Write 1-bit images as miniswhite',
353
+ group: optimize,
354
+ type: 'boolean'
355
+ },
356
+
357
+ // @see https://sharp.pixelplumbing.com/api-output#webp
358
+ minSize: {
359
+ desc: 'Prevent use of animation key frames to minimize file size',
360
+ group: optimize,
361
+ type: 'boolean'
362
+ },
363
+
364
+ // @see https://sharp.pixelplumbing.com/api-output#webp
365
+ mixed: {
366
+ desc: 'Allow mixture of lossy and lossless animation frames',
367
+ group: optimize,
368
+ type: 'boolean'
369
+ },
370
+
371
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
372
+ mozjpeg: {
373
+ desc: 'Use mozjpeg defaults',
374
+ group: optimize,
375
+ type: 'boolean'
376
+ },
377
+
378
+ // @see https://sharp.pixelplumbing.com/api-output#webp
379
+ nearLossless: {
380
+ desc: 'Use near_lossless compression mode',
381
+ group: optimize,
382
+ type: 'boolean'
383
+ },
384
+
385
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
386
+ optimise: {
387
+ alias: 'optimize',
388
+ desc: 'Apply optimiseScans, overshootDeringing, and trellisQuantisation',
389
+ group: optimize,
390
+ type: 'boolean'
391
+ },
392
+
393
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
394
+ optimiseCoding: {
395
+ alias: 'optimizeCoding',
396
+ default: true,
397
+ desc: 'Optimise Huffman coding tables',
398
+ group: optimize,
399
+ type: 'boolean'
400
+ },
401
+
402
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
403
+ optimiseScans: {
404
+ alias: 'optimizeScans',
405
+ desc: 'Optimise progressive scans',
406
+ group: optimize,
407
+ implies: 'progressive',
408
+ type: 'boolean'
409
+ },
410
+
411
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
412
+ overshootDeringing: {
413
+ desc: 'Apply overshoot deringing',
414
+ group: optimize,
415
+ type: 'boolean'
416
+ },
417
+
418
+ // @see https://sharp.dimens.io/api-output#png
419
+ palette: {
420
+ desc: 'Quantise to a palette-based image with alpha transparency support',
421
+ group: optimize,
422
+ type: 'boolean'
423
+ },
424
+
425
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
426
+ predictor: {
427
+ choices: constants.TIFF_PREDICTOR,
428
+ default: 'horizontal',
429
+ desc: 'Compression predictor',
430
+ group: optimize
431
+ },
432
+
433
+ // @see https://sharp.pixelplumbing.com/api-output#webp
434
+ preset: {
435
+ choices: constants.PRESETS,
436
+ default: 'default',
437
+ desc: 'Named preset for preprocessing/filtering',
438
+ group: optimize
439
+ },
440
+
441
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
442
+ pyramid: {
443
+ desc: 'Write an image pyramid',
444
+ group: optimize,
445
+ type: 'boolean'
446
+ },
447
+
448
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
449
+ quantisationTable: {
450
+ alias: 'quantizationTable',
451
+ defaultDescription: '0',
452
+ desc: 'Quantization table to use',
453
+ group: optimize,
454
+ type: 'number'
455
+ },
456
+
457
+ // @see https://sharp.pixelplumbing.com/api-output#gif
458
+ reuse: {
459
+ alias: ['reoptimise', 'reoptimize'],
460
+ desc: 'Always generate new palettes (slow)',
461
+ group: optimize,
462
+ type: 'boolean'
463
+ },
464
+
465
+ // @see https://sharp.pixelplumbing.com/api-output
466
+ resolutionUnit: {
467
+ choices: constants.RESOLUTION_UNIT,
468
+ defaultDescription: 'inch',
469
+ desc: 'Resolution unit',
470
+ group: optimize
471
+ },
472
+
473
+ // @see https://sharp.pixelplumbing.com/api-output#webp
474
+ smartDeblock: {
475
+ desc: 'Auto-adjust the deblocking filter, can improve low contrast edges',
476
+ group: optimize,
477
+ type: 'boolean'
478
+ },
479
+
480
+ // @see https://sharp.pixelplumbing.com/api-output#webp
481
+ smartSubsample: {
482
+ desc: 'High quality chroma subsampling',
483
+ group: optimize,
484
+ type: 'boolean'
485
+ },
486
+
487
+ // @see https://sharp.pixelplumbing.com/api-output#tile
488
+ tileBackground: {
489
+ defaultDescription: 'rgba(255, 255, 255, 1)',
490
+ desc: 'Background colour, parsed by the color module',
491
+ group: optimize,
492
+ type: 'string'
493
+ },
494
+
495
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
496
+ tileHeight: {
497
+ desc: 'Vertical tile size',
498
+ group: optimize,
499
+ type: 'number'
500
+ },
501
+
502
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
503
+ tileWidth: {
504
+ desc: 'Horizontal tile size',
505
+ group: optimize,
506
+ type: 'number'
507
+ },
508
+
509
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
510
+ trellisQuantisation: {
511
+ desc: 'Apply trellis quantisation',
512
+ group: optimize,
513
+ type: 'boolean'
514
+ },
515
+
516
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
517
+ xres: {
518
+ defaultDescription: '1.0',
519
+ desc: 'Horizontal resolution',
520
+ group: optimize,
521
+ type: 'number'
522
+ },
523
+
524
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
525
+ yres: {
526
+ defaultDescription: '1.0',
527
+ desc: 'Vertical resolution',
528
+ group: optimize,
529
+ type: 'number'
530
+ }
531
+ }
532
+
533
+ const options = {
534
+ ...globalOptions,
535
+ ...inputOptions,
536
+ ...outputOptions,
537
+ ...optimizationOptions
538
+ }
539
+
540
+ // Configure.
541
+ const cli = yargs
542
+ .parserConfiguration({ 'populate--': true })
543
+ .strict()
544
+ .usage('$0 <options> [command..]')
545
+ .options(options)
546
+ .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')
547
+ .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')
548
+ .example('$0 -i ./input.jpg -o ./out --metadata', 'Include all metadata in the output image')
549
+ .example('$0 -i ./input.jpg -o ./out --metadata.exif.IFD0.Copyright "Wernham Hogg"', 'Set "IFD0-Copyright" in output EXIF metadata')
550
+ .example('$0 -i ./input.jpg -o ./out --metadata.density 96', 'Set output metadata to 96 DPI')
551
+ .epilog('For more information on available options, please visit https://sharp.pixelplumbing.com/')
552
+ .showHelpOnFail(false)
553
+ .wrap(100)
554
+
555
+ // Built-in options.
556
+ .help().alias('help', 'h')
557
+ .version(pkg.version).alias('version', 'v')
558
+ .group(['help', 'version'], 'Misc. Options')
559
+
560
+ // Commands.
561
+ // Avoid `yargs.commandDir()` as it uses insertion order, not alphabetical.
562
+ .command(require('../cmd/operations/affine'))
563
+ .command(require('../cmd/channel-manipulation/bandbool'))
564
+ .command(require('../cmd/operations/blur'))
565
+ .command(require('../cmd/operations/boolean'))
566
+ .command(require('../cmd/operations/clahe'))
567
+ .command(require('../cmd/compositing/composite'))
568
+ .command(require('../cmd/operations/convolve'))
569
+ .command(require('../cmd/channel-manipulation/ensure-alpha'))
570
+ .command(require('../cmd/resizing/extend'))
571
+ .command(require('../cmd/resizing/extract'))
572
+ .command(require('../cmd/channel-manipulation/extract-channel'))
573
+ .command(require('../cmd/operations/flatten'))
574
+ .command(require('../cmd/operations/flip'))
575
+ .command(require('../cmd/operations/flop'))
576
+ .command(require('../cmd/operations/gamma'))
577
+ .command(require('../cmd/colour-manipulation/greyscale'))
578
+ .command(require('../cmd/channel-manipulation/join-channel'))
579
+ .command(require('../cmd/operations/linear'))
580
+ .command(require('../cmd/operations/median'))
581
+ .command(require('../cmd/operations/modulate'))
582
+ .command(require('../cmd/operations/negate'))
583
+ .command(require('../cmd/operations/normalise'))
584
+ .command(require('../cmd/colour-manipulation/pipeline-colourspace'))
585
+ .command(require('../cmd/operations/recomb'))
586
+ .command(require('../cmd/channel-manipulation/remove-alpha'))
587
+ .command(require('../cmd/resizing/resize'))
588
+ .command(require('../cmd/operations/rotate'))
589
+ .command(require('../cmd/operations/sharpen'))
590
+ .command(require('../cmd/operations/threshold'))
591
+ .command(require('../cmd/colour-manipulation/tint'))
592
+ .command(require('../cmd/output'))
593
+ .command(require('../cmd/colour-manipulation/tocolourspace'))
594
+ .command(require('../cmd/resizing/trim'))
595
+ .command(require('../cmd/operations/unflatten'))
596
+
597
+ // Helpers.
598
+ const originalParse = cli.parse.bind(cli)
599
+ const promisifiedParse = (...args) => {
600
+ return new Promise((resolve, reject) => {
601
+ originalParse(...args, (err, argv, output) => {
602
+ if (err) {
603
+ reject(err)
604
+ }
605
+ if (argv.v || argv.help) {
606
+ reject(output)
607
+ }
608
+ resolve(argv)
609
+ })
610
+ })
611
+ }
612
+
613
+ // Exports.
614
+ module.exports = cli
615
+ module.exports.inputOptions = Object.keys(inputOptions)
616
+ module.exports.parse = function recursiveParse (args, context = {}) {
617
+ return promisifiedParse(args, context).then(argv => {
618
+ // Handle arguments.
619
+ // NOTE Use queue.unshift to apply global options first.
620
+
621
+ // Global options.
622
+
623
+ // Require at least one input file.
624
+ // NOTE: check here b/c https://github.com/yargs/yargs/issues/403
625
+ if (argv.input && argv.input.length === 0) {
626
+ throw new Error('Not enough arguments following: i, input')
627
+ }
628
+
629
+ // @see https://sharp.pixelplumbing.com/api-output#timeout
630
+ if (argv.timeout) {
631
+ queue.unshift(['timeout', (sharp) => sharp.timeout({ seconds: argv.timeout })])
632
+ }
633
+
634
+ // Output options.
635
+
636
+ // @see https://sharp.pixelplumbing.com/api-output#toformat
637
+ if (argv.format) {
638
+ queue.unshift(['format', (sharp) => sharp.toFormat(argv.format, { compression: argv.hcompression })])
639
+ }
640
+
641
+ // @see https://sharp.pixelplumbing.com/api-output#withmetadata
642
+ if (argv.metadata) {
643
+ queue.unshift(['withMetadata', (sharp) => sharp.withMetadata(argv.metadata)])
644
+ }
645
+
646
+ // @see https://sharp.pixelplumbing.com/api-output#heif
647
+ const { heif } = sharp.format
648
+ if (argv.hcompression !== optimizationOptions.hcompression.default || // HEIF-specific.
649
+ // Ensure libheif is installed before applying generic options.
650
+ (heif.input && heif.input.file &&
651
+ (argv.effort || argv.hbitdepth || argv.lossless || argv.quality))) {
652
+ queue.unshift(['heif', (sharp) => {
653
+ return sharp.heif({
654
+ bitdepth: argv.hbitdepth,
655
+ compression: argv.hcompression,
656
+ effort: argv.effort,
657
+ force: false,
658
+ lossless: argv.lossless,
659
+ quality: argv.quality
660
+ })
661
+ }])
662
+ }
663
+
664
+ // @see https://sharp.pixelplumbing.com/api-output#avif
665
+ if (argv.chromaSubsampling || argv.effort || argv.lossless || argv.quality) {
666
+ queue.unshift(['avif', (sharp) => {
667
+ return sharp.avif({
668
+ chromaSubsampling: argv.chromaSubsampling,
669
+ effort: argv.effort,
670
+ force: false,
671
+ lossless: argv.lossless,
672
+ quality: argv.quality
673
+ })
674
+ }])
675
+ }
676
+
677
+ // @see https://sharp.pixelplumbing.com/api-output#gif
678
+ if (argv.colors || argv.effort || argv.dither || argv.interFrameMaxError ||
679
+ argv.interPaletteMaxError || argv.loop || argv.delay || argv.progressive || argv.reuse) {
680
+ queue.unshift(['gif', (sharp) => {
681
+ return sharp.gif({
682
+ colors: argv.colors,
683
+ force: false,
684
+ effort: argv.effort,
685
+ dither: argv.dither,
686
+ interFrameMaxError: argv.interFrameMaxError,
687
+ interPaletteMaxError: argv.interPaletteMaxError,
688
+ loop: argv.loop,
689
+ delay: argv.delay,
690
+ progressive: argv.progressive,
691
+ reuse: argv.reuse
692
+ })
693
+ }])
694
+ }
695
+
696
+ // @see https://sharp.pixelplumbing.com/api-output#jpeg
697
+ if (
698
+ argv.chromaSubsampling ||
699
+ argv.mozjpeg ||
700
+ argv.optimise ||
701
+ argv.optimiseCoding !== true ||
702
+ argv.optimiseScans ||
703
+ argv.overshootDeringing ||
704
+ argv.progressive ||
705
+ argv.quantisationTable ||
706
+ argv.quality ||
707
+ argv.trellisQuantisation
708
+ ) {
709
+ queue.unshift(['jpeg', (sharp) => {
710
+ return sharp.jpeg({
711
+ chromaSubsampling: argv.chromaSubsampling,
712
+ force: false,
713
+ mozjpeg: argv.mozjpeg,
714
+ optimiseCoding: argv.optimiseCoding,
715
+ optimiseScans: argv.optimise || argv.optimiseScans,
716
+ overshootDeringing: argv.optimise || argv.overshootDeringing,
717
+ progressive: argv.progressive,
718
+ quality: argv.quality,
719
+ quantisationTable: argv.quantisationTable,
720
+ trellisQuantisation: argv.optimise || argv.trellisQuantisation
721
+ })
722
+ }])
723
+ }
724
+
725
+ // @see https://sharp.pixelplumbing.com/api-output#png
726
+ if (argv.adaptiveFiltering || argv.colors || argv.compressionLevel ||
727
+ argv.dither || argv.effort || argv.palette || argv.progressive) {
728
+ queue.unshift(['png', (sharp) => {
729
+ return sharp.png({
730
+ adaptiveFiltering: argv.adaptiveFiltering,
731
+ colors: argv.colors,
732
+ compressionLevel: argv.compressionLevel,
733
+ dither: argv.dither,
734
+ effort: argv.effort,
735
+ force: false,
736
+ palette: argv.palette,
737
+ progressive: argv.progressive
738
+ })
739
+ }])
740
+ }
741
+
742
+ // @see https://sharp.pixelplumbing.com/api-output#tiff
743
+ if (argv.bitdepth ||
744
+ argv.compression !== optimizationOptions.compression.default ||
745
+ argv.predictor !== optimizationOptions.predictor.default ||
746
+ argv.miniswhite || argv.pyramid || argv.quality || argv.resolutionUnit ||
747
+ argv.tileBackground || argv.tileHeight || argv.tileWidth || argv.xres || argv.yres) {
748
+ queue.unshift(['tiff', (sharp) => {
749
+ return sharp.tiff({
750
+ background: argv.tileBackground,
751
+ bitdepth: argv.bitdepth,
752
+ compression: argv.compression,
753
+ force: false,
754
+ miniswhite: argv.miniswhite,
755
+ predictor: argv.predictor,
756
+ pyramid: argv.pyramid,
757
+ quality: argv.quality,
758
+ resolutionUnit: argv.resolutionUnit,
759
+ tile: argv.tileWidth !== undefined || argv.tileHeight !== undefined,
760
+ tileHeight: argv.tileHeight || argv.tileWidth,
761
+ tileWidth: argv.tileWidth || argv.tileHeight,
762
+ xres: argv.xres,
763
+ yres: argv.yres
764
+ })
765
+ }])
766
+ }
767
+
768
+ // @see https://sharp.pixelplumbing.com/api-output#webp
769
+ if (argv.alphaQuality || argv.quality || argv.lossless || argv.minSize ||
770
+ argv.mixed || argv.nearLossless || argv.effort ||
771
+ argv.preset !== optimizationOptions.preset.default || argv.smartDeblock ||
772
+ argv.smartSubsample) {
773
+ queue.unshift(['webp', (sharp) => {
774
+ return sharp.webp({
775
+ alphaQuality: argv.alphaQuality,
776
+ effort: argv.effort,
777
+ force: false,
778
+ lossless: argv.lossless,
779
+ minSize: argv.minSize,
780
+ mixed: argv.mixed,
781
+ nearLossless: argv.nearLossless,
782
+ preset: argv.preset,
783
+ quality: argv.quality,
784
+ smartDeblock: argv.smartDeblock,
785
+ smartSubsample: argv.smartSubsample
786
+ })
787
+ }])
788
+ }
789
+
790
+ // Invoke with remaining arguments (if any).
791
+ const { '--': remainingargv = [] } = argv
792
+ if (remainingargv.length > 0) {
793
+ return recursiveParse(remainingargv, {
794
+ ...context,
795
+ ...pick(argv, Object.keys(options)) // Retain options.
796
+ })
797
+ }
798
+ return argv
799
+ })
800
+ }