@depup/sharp-cli 5.2.0-depup.0 → 6.0.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 (50) hide show
  1. package/.github/workflows/ci.yml +30 -0
  2. package/.prettierignore +2 -0
  3. package/.prettierrc +1 -0
  4. package/CHANGELOG.md +40 -8
  5. package/README.md +6 -9
  6. package/bin/cli.js +5 -5
  7. package/changes.json +5 -17
  8. package/cmd/channel-manipulation/bandbool.js +24 -19
  9. package/cmd/channel-manipulation/ensure-alpha.js +27 -22
  10. package/cmd/channel-manipulation/extract-channel.js +23 -19
  11. package/cmd/channel-manipulation/join-channel.js +19 -20
  12. package/cmd/channel-manipulation/remove-alpha.js +12 -15
  13. package/cmd/colour-manipulation/greyscale.js +16 -16
  14. package/cmd/colour-manipulation/pipeline-colourspace.js +26 -22
  15. package/cmd/colour-manipulation/tint.js +17 -19
  16. package/cmd/colour-manipulation/tocolourspace.js +22 -21
  17. package/cmd/compositing/composite.js +133 -102
  18. package/cmd/operations/affine.js +57 -51
  19. package/cmd/operations/blur.js +44 -41
  20. package/cmd/operations/boolean.js +22 -21
  21. package/cmd/operations/clahe.js +37 -37
  22. package/cmd/operations/convolve.js +52 -48
  23. package/cmd/operations/dilate.js +58 -0
  24. package/cmd/operations/erode.js +58 -0
  25. package/cmd/operations/flatten.js +23 -24
  26. package/cmd/operations/flip.js +12 -15
  27. package/cmd/operations/flop.js +12 -15
  28. package/cmd/operations/gamma.js +26 -25
  29. package/cmd/operations/linear.js +30 -29
  30. package/cmd/operations/median.js +18 -22
  31. package/cmd/operations/modulate.js +42 -31
  32. package/cmd/operations/negate.js +17 -20
  33. package/cmd/operations/normalise.js +28 -25
  34. package/cmd/operations/recomb.js +34 -32
  35. package/cmd/operations/rotate.js +31 -29
  36. package/cmd/operations/sharpen.js +49 -45
  37. package/cmd/operations/threshold.js +29 -29
  38. package/cmd/operations/unflatten.js +14 -16
  39. package/cmd/output.js +61 -56
  40. package/cmd/resizing/extend.js +55 -49
  41. package/cmd/resizing/extract.js +32 -33
  42. package/cmd/resizing/resize.js +79 -56
  43. package/cmd/resizing/trim.js +59 -36
  44. package/{lib/queue.js → eslint.config.mjs} +21 -17
  45. package/lib/cli.js +637 -383
  46. package/lib/constants.js +24 -18
  47. package/lib/convert.js +106 -73
  48. package/lib/index.js +56 -23
  49. package/lib/utils.js +50 -0
  50. package/package.json +27 -37
package/lib/constants.js CHANGED
@@ -21,35 +21,41 @@
21
21
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- // Strict mode.
25
- 'use strict'
26
-
27
24
  // Package modules.
28
- const sharp = require('sharp')
25
+ import sharp from "sharp";
29
26
 
30
27
  // Exports.
31
- module.exports = {
28
+ export default {
32
29
  BLEND: Object.keys(sharp.blend),
33
30
  BOOL: Object.keys(sharp.bool),
34
- CHANNEL: ['red', 'green', 'blue', 'alpha'],
31
+ CHANNEL: ["red", "green", "blue", "alpha"],
35
32
  COLOURSPACE: Object.keys(sharp.colourspace),
36
- CONTAINER: ['fs', 'zip'],
37
- DEPTH: ['onepixel', 'onetile', 'one'],
38
- EXTEND_WITH: ['background', 'copy', 'repeat', 'mirror'],
39
- FAIL_ON: ['none', 'truncated', 'error', 'warning'],
33
+ CONTAINER: ["fs", "zip"],
34
+ DEPTH: ["onepixel", "onetile", "one"],
35
+ EXTEND_WITH: ["background", "copy", "repeat", "mirror"],
36
+ FAIL_ON: ["none", "truncated", "error", "warning"],
40
37
  FIT: Object.keys(sharp.fit),
41
- FORMAT: ['avif', 'gif', 'heif', 'jpeg', 'jpg', 'png', 'raw', 'tiff', 'webp'],
38
+ FORMAT: ["avif", "gif", "heif", "jpeg", "png", "tiff", "webp"],
42
39
  GRAVITY: Object.keys(sharp.gravity),
43
- HEIF_COMPRESSION: ['hevc', 'av1'],
40
+ HEIF_COMPRESSION: ["hevc", "av1"],
44
41
  INTERPOLATORS: Object.keys(sharp.interpolators),
45
42
  KERNEL: Object.keys(sharp.kernel),
46
- LAYOUT: ['dz', 'google', 'iiif', 'zoomify'],
43
+ LAYOUT: ["dz", "google", "iiif", "iiif3", "zoomify"],
47
44
  POSITION: Object.keys(sharp.position),
48
- PRESETS: ['default', 'photo', 'picture', 'drawing', 'icon', 'text'],
49
- RESOLUTION_UNIT: ['cm', 'inch'],
45
+ PRESETS: ["default", "photo", "picture", "drawing", "icon", "text"],
46
+ RESOLUTION_UNIT: ["cm", "inch"],
50
47
  STRATEGY: Object.keys(sharp.strategy),
51
48
  TIFF_COMPRESSION: [
52
- 'ccittfax4', 'deflate', 'jpeg', 'jp2k', 'lzw', 'none', 'packbits', 'webp', 'zstd'
49
+ "ccittfax4",
50
+ "deflate",
51
+ "jpeg",
52
+ "jp2k",
53
+ "lzw",
54
+ "none",
55
+ "packbits",
56
+ "webp",
57
+ "zstd",
53
58
  ],
54
- TIFF_PREDICTOR: ['float', 'horizontal', 'none']
55
- }
59
+ TIFF_PREDICTOR: ["float", "horizontal", "none"],
60
+ TUNE: ["auto", "iq", "psnr", "ssim"],
61
+ };
package/lib/convert.js CHANGED
@@ -21,101 +21,134 @@
21
21
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- // Strict mode.
25
- 'use strict'
26
-
27
24
  // Standard lib.
28
- const fs = require('fs')
29
- const path = require('path')
25
+ import { createReadStream } from "node:fs";
26
+ import path from "node:path";
27
+ import { pipeline } from "node:stream/promises";
30
28
 
31
29
  // Package modules.
32
- const bubbleError = require('bubble-stream-error')
33
- const { globSync } = require('glob')
34
- const isDirectory = require('is-directory')
35
- const sharp = require('sharp')
30
+ import { globSync } from "glob";
31
+ import sharp from "sharp";
36
32
 
37
33
  // Local modules.
38
- const queue = require('./queue')
34
+ import { drain, isDirectory } from "./utils.js";
39
35
 
40
36
  // Configure.
41
37
  const EXTENSIONS = {
42
- avif: '.avif',
43
- dz: '', // Determined by tile.container.
44
- gif: '.gif',
45
- heif: '.avif',
46
- jpeg: '.jpg',
47
- png: '.png',
48
- tiff: '.tiff',
49
- webp: '.webp'
50
- }
38
+ avif: ".avif",
39
+ dz: "", // Determined by tile.container.
40
+ gif: ".gif",
41
+ heif: ".avif",
42
+ jpeg: ".jpg",
43
+ png: ".png",
44
+ tiff: ".tiff",
45
+ webp: ".webp",
46
+ };
47
+ const FORMATS = {
48
+ ".avif": "avif",
49
+ ".gif": "gif",
50
+ ".jpg": "jpeg",
51
+ ".jpeg": "jpeg",
52
+ ".png": "png",
53
+ ".tif": "tiff",
54
+ ".tiff": "tiff",
55
+ ".webp": "webp",
56
+ };
51
57
 
52
58
  // Exports.
53
- module.exports = {
59
+ export default {
54
60
  // Convert a list of files.
55
- files: (input, output, options) => {
61
+ files: async (input, output, context) => {
56
62
  // Resolve files.
57
- const files = input.reduce((list, input) => {
58
- return list.concat(globSync(input, { absolute: true }))
59
- }, [])
60
-
63
+ const files = input.flatMap((input) => globSync(input, { absolute: true }));
61
64
  if (files.length === 0) {
62
- return Promise.reject(new Error('No input files'))
65
+ throw new Error("No input files");
63
66
  }
64
67
 
65
68
  // Process files.
66
- const isBatch = files.length > 1
69
+ const isBatch = files.length > 1;
67
70
  const promises = files.map((src) => {
68
- // Create pipeline.
69
- const transformer = queue.drain(sharp(options))
71
+ const image = sharp(context.options);
72
+ return pipeline(createReadStream(src), image)
73
+ .then(() => image.metadata())
74
+ .then((metadata) => {
75
+ // Process output as a template.
76
+ const parts = path.parse(src);
77
+ const regex = /\{(root|dir|base|ext|name)\}/g;
78
+ let dest = output;
79
+ let match;
80
+ while ((match = regex.exec(output)) !== null) {
81
+ const [search, prop] = match;
82
+ dest = dest.replace(search, parts[prop]);
83
+ }
84
+ dest = path.resolve(dest);
70
85
 
71
- // Process output as a template.
72
- const parts = path.parse(src)
73
- const regex = /\{(root|dir|base|ext|name)\}/g
74
- let dest = output
75
- let match
76
- while ((match = regex.exec(output)) !== null) {
77
- const [search, prop] = match
78
- dest = dest.replace(search, parts[prop])
79
- }
80
- dest = path.resolve(dest)
86
+ // If output was not a template, assume dest is a directory when using
87
+ // batch processing.
88
+ const outputAssumeDir = dest === path.resolve(output) && isBatch;
89
+ const outputIsDir = outputAssumeDir || isDirectory(dest);
90
+ const format =
91
+ context.format ??
92
+ (outputIsDir ? null : FORMATS[path.extname(dest).toLowerCase()]) ??
93
+ metadata.format;
94
+ if (outputIsDir) {
95
+ const defaultExt = path.extname(src);
96
+ dest = path.format({
97
+ dir: dest,
98
+ name: path.basename(src, defaultExt),
99
+ ext: format in EXTENSIONS ? EXTENSIONS[format] : defaultExt,
100
+ });
101
+ }
81
102
 
82
- // If output was not a template, assume dest is a directory when using
83
- // batch processing.
84
- const outputAssumeDir = dest === path.resolve(output) && isBatch
85
- if (outputAssumeDir || isDirectory.sync(dest)) {
86
- const defaultExt = path.extname(src)
87
- const desiredExt = transformer.options.formatOut
88
- dest = path.format({
89
- dir: dest,
90
- name: path.basename(src, defaultExt),
91
- ext: desiredExt in EXTENSIONS ? EXTENSIONS[desiredExt] : defaultExt
92
- })
93
- }
103
+ const inputMetadata = { ...metadata, path: src };
104
+ const transformer = drain(context.queue, image, {
105
+ format,
106
+ metadata: inputMetadata,
107
+ });
94
108
 
95
- // Write, attach info and return.
96
- fs.createReadStream(src).pipe(transformer)
97
- return transformer
98
- .toFile(dest)
99
- .then((info) => Object.assign(info, { src, path: dest }))
100
- })
101
- return Promise.all(promises)
109
+ const promise = context.dry
110
+ ? transformer
111
+ .toBuffer({ resolveWithObject: true })
112
+ .then(({ info }) => info)
113
+ : transformer.toFile(dest);
114
+ return promise.then((info) => ({
115
+ input: inputMetadata,
116
+ output: { ...info, path: dest },
117
+ }));
118
+ });
119
+ });
120
+ return Promise.allSettled(promises);
102
121
  },
103
122
 
104
123
  // Convert a stream.
105
- stream: (inStream, outStream, options) => {
106
- return new Promise((resolve, reject) => {
107
- // Create pipeline.
108
- const transformer = queue.drain(sharp(options))
124
+ stream: async (inStream, outStream, context) => {
125
+ const image = sharp(context.options);
126
+ return pipeline(inStream, image)
127
+ .then(() => image.metadata())
128
+ .then((metadata) => {
129
+ const inputMetadata = { ...metadata, path: "stdin" };
130
+ const transformer = drain(context.queue, image, {
131
+ format: context.format ?? metadata.format,
132
+ metadata: inputMetadata,
133
+ });
109
134
 
110
- // Gather return value.
111
- const info = { }
112
- transformer.on('info', (_info) => Object.assign(info, _info))
135
+ if (context.dry) {
136
+ return transformer
137
+ .toBuffer({ resolveWithObject: true })
138
+ .then(({ info }) => ({
139
+ input: inputMetadata,
140
+ output: { ...info, path: "stdout" },
141
+ }));
142
+ }
113
143
 
114
- // Pipe, and return as promise.
115
- bubbleError(inStream, transformer, outStream)
116
- inStream.pipe(transformer).pipe(outStream)
117
- outStream.once('error', reject)
118
- outStream.on('finish', () => resolve(info))
119
- })
120
- }
121
- }
144
+ // Gather return value.
145
+ const info = {};
146
+ transformer.on("info", (_info) => Object.assign(info, _info));
147
+
148
+ return pipeline(transformer, outStream).then(() => ({
149
+ input: inputMetadata,
150
+ output: { ...info, path: "stdout" },
151
+ }));
152
+ });
153
+ },
154
+ };
package/lib/index.js CHANGED
@@ -21,38 +21,71 @@
21
21
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- // Strict mode.
25
- 'use strict'
26
-
27
- // Package modules.
28
- const pick = require('lodash.pick')
29
-
30
24
  // Local modules.
31
- const cli = require('./cli')
32
- const convert = require('./convert')
25
+ import cli, { inputOptions } from "./cli.js";
26
+ import convert from "./convert.js";
27
+ import { pick } from "./utils.js";
33
28
 
34
29
  // Exports.
35
- module.exports = (args, options = { }) => {
36
- const logger = options.logger || console // Cast.
30
+ export default (args, options = {}) => {
31
+ const logger = options.logger || console; // Cast.
37
32
 
38
33
  // Parse arguments and handle i/o.
39
- return cli.parse(args)
40
- .then(argv => {
41
- const options = pick(argv, cli.inputOptions)
34
+ return cli
35
+ .parseAsync(args)
36
+ .then((argv) => {
37
+ const context = {
38
+ dry: argv.dry,
39
+ format: argv.format,
40
+ options: pick(argv, Object.keys(inputOptions)),
41
+ queue: argv["#queue"],
42
+ };
42
43
  if (argv.input) {
43
- return convert.files(argv.input, argv.output, options)
44
+ return convert
45
+ .files(argv.input, argv.output, context)
46
+ .then((results) => {
47
+ // On error, set the code and let the program finish naturally
48
+ const containsError = results.some(
49
+ ({ status }) => status === "rejected",
50
+ );
51
+ if (containsError) process.exitCode = 1;
52
+
53
+ if (argv.print) {
54
+ const output = results.map(({ reason, status, value }) => {
55
+ return status === "fulfilled"
56
+ ? value
57
+ : { error: reason.message };
58
+ });
59
+ logger.log(JSON.stringify(output));
60
+ } else {
61
+ results.forEach((result) => {
62
+ if (result.status === "fulfilled") {
63
+ logger.log(result.value.output.path);
64
+ } else {
65
+ logger.error(`FAILED: ${result.reason.message}`);
66
+ }
67
+ });
68
+ if (containsError) {
69
+ logger.error();
70
+ logger.error("Specify --help for available options");
71
+ }
72
+ }
73
+ });
44
74
  }
45
- return convert.stream(process.stdin, process.stdout, options)
75
+ return convert
76
+ .stream(process.stdin, process.stdout, context)
77
+ .then((output) => {
78
+ if (argv.print) logger.log(JSON.stringify(output));
79
+ });
46
80
  })
47
- .then((output) => output.map((file) => logger.log(file.path)))
48
81
  .catch((err) => {
49
82
  if (err instanceof Error) {
50
- logger.error(err.message)
51
- logger.error()
52
- logger.error('Specify --help for available options')
53
- process.exitCode = 1
83
+ logger.error(err.message);
84
+ logger.error();
85
+ logger.error("Specify --help for available options");
86
+ process.exitCode = 1;
54
87
  } else {
55
- logger.log(err)
88
+ logger.log(err);
56
89
  }
57
- })
58
- }
90
+ });
91
+ };
package/lib/utils.js ADDED
@@ -0,0 +1,50 @@
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
+ // Standard lib.
25
+ import fs from "node:fs";
26
+
27
+ const drain = (queue, transformer, context) => {
28
+ return queue.reduce((pipeline, [, handler]) => {
29
+ return handler(pipeline, context);
30
+ }, transformer);
31
+ };
32
+
33
+ const isDirectory = (path) => {
34
+ try {
35
+ return fs.statSync(path).isDirectory();
36
+ } catch (err) {
37
+ if (err.code === "ENOENT") return false;
38
+ throw err;
39
+ }
40
+ };
41
+
42
+ const pick = (object, keys) => {
43
+ return keys.reduce((result, key) => {
44
+ if (Object.hasOwn(object, key)) result[key] = object[key];
45
+ return result;
46
+ }, {});
47
+ };
48
+
49
+ // Exports.
50
+ export { drain, isDirectory, pick };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@depup/sharp-cli",
3
- "version": "5.2.0-depup.0",
3
+ "version": "6.0.0-depup.0",
4
4
  "description": "CLI for sharp. (with updated dependencies)",
5
5
  "keywords": [
6
6
  "sharp-cli",
@@ -20,65 +20,55 @@
20
20
  "homepage": "https://github.com/vseventer/sharp-cli",
21
21
  "bugs": "https://github.com/vseventer/sharp-cli/issues",
22
22
  "license": "MIT",
23
+ "type": "module",
23
24
  "author": "Mark van Seventer <mark@vseventer.com>",
24
25
  "repository": "vseventer/sharp-cli",
25
26
  "bin": {
26
27
  "sharp": "bin/cli.js"
27
28
  },
28
- "main": "lib/",
29
+ "main": "lib/index.js",
29
30
  "scripts": {
30
- "pretest": "standard | snazzy",
31
- "test": "nyc mocha",
31
+ "format": "prettier . --write",
32
+ "lint": "eslint",
33
+ "pretest": "npm run lint && prettier . --check",
34
+ "test": "NODE_OPTIONS=--experimental-loader=@istanbuljs/esm-loader-hook nyc mocha",
32
35
  "posttest": "nyc report --reporter=lcov"
33
36
  },
34
37
  "dependencies": {
35
- "bubble-stream-error": "1.0.x",
36
38
  "glob": "^13.0.6",
37
- "is-directory": "^0.3.1",
38
- "lodash.pick": "^4.4.0",
39
- "sharp": "^0.34.5",
40
- "yargs": "^18.0.0"
39
+ "sharp": "0.35.3",
40
+ "yargs": "^18.1.0"
41
41
  },
42
42
  "devDependencies": {
43
- "fs-extra": "11.3.x",
44
- "mocha": "10.8.x",
45
- "must": "0.13.x",
46
- "nyc": "17.1.x",
47
- "sinon": "21.0.x",
48
- "snazzy": "9.0.x",
49
- "standard": "17.1.x",
50
- "tempy": "1.0.x"
43
+ "@eslint/js": "9.39.x",
44
+ "@istanbuljs/esm-loader-hook": "0.3.x",
45
+ "eslint": "9.39.x",
46
+ "fs-extra": "11.4.x",
47
+ "globals": "17.11.x",
48
+ "mocha": "11.8.x",
49
+ "nyc": "18.0.x",
50
+ "prettier": "3.9.x",
51
+ "sinon": "22.1.x",
52
+ "tempy": "3.2.x"
51
53
  },
52
54
  "engines": {
53
- "node": ">=18.17"
55
+ "node": ">=20.10.0"
54
56
  },
55
57
  "depup": {
56
58
  "changes": {
57
59
  "glob": {
58
- "from": "11.0.x",
60
+ "from": "13.0.x",
59
61
  "to": "^13.0.6"
60
62
  },
61
- "is-directory": {
62
- "from": "0.3.x",
63
- "to": "^0.3.1"
64
- },
65
- "lodash.pick": {
66
- "from": "3.1.0",
67
- "to": "^4.4.0"
68
- },
69
- "sharp": {
70
- "from": "0.34.2",
71
- "to": "^0.34.5"
72
- },
73
63
  "yargs": {
74
- "from": "^17.6.2",
75
- "to": "^18.0.0"
64
+ "from": "17.7.x",
65
+ "to": "^18.1.0"
76
66
  }
77
67
  },
78
- "depsUpdated": 5,
68
+ "depsUpdated": 2,
79
69
  "originalPackage": "sharp-cli",
80
- "originalVersion": "5.2.0",
81
- "processedAt": "2026-03-19T03:28:48.584Z",
82
- "smokeTest": "failed"
70
+ "originalVersion": "6.0.0",
71
+ "processedAt": "2026-08-23T00:29:02.257Z",
72
+ "smokeTest": "passed"
83
73
  }
84
74
  }