@depup/sharp-cli 5.3.0-depup.0 → 6.1.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 (48) hide show
  1. package/.github/workflows/ci.yml +30 -0
  2. package/CHANGELOG.md +26 -0
  3. package/README.md +6 -9
  4. package/bin/cli.js +5 -5
  5. package/changes.json +4 -16
  6. package/cmd/channel-manipulation/bandbool.js +6 -7
  7. package/cmd/channel-manipulation/ensure-alpha.js +5 -8
  8. package/cmd/channel-manipulation/extract-channel.js +3 -7
  9. package/cmd/channel-manipulation/join-channel.js +5 -8
  10. package/cmd/channel-manipulation/remove-alpha.js +2 -8
  11. package/cmd/colour-manipulation/greyscale.js +2 -8
  12. package/cmd/colour-manipulation/pipeline-colourspace.js +4 -8
  13. package/cmd/colour-manipulation/tint.js +3 -8
  14. package/cmd/colour-manipulation/tocolourspace.js +3 -7
  15. package/cmd/compositing/composite.js +10 -7
  16. package/cmd/operations/affine.js +9 -11
  17. package/cmd/operations/blur.js +3 -8
  18. package/cmd/operations/boolean.js +3 -7
  19. package/cmd/operations/clahe.js +4 -9
  20. package/cmd/operations/convolve.js +5 -9
  21. package/cmd/operations/dilate.js +2 -8
  22. package/cmd/operations/erode.js +2 -8
  23. package/cmd/operations/flatten.js +2 -8
  24. package/cmd/operations/flip.js +3 -8
  25. package/cmd/operations/flop.js +3 -8
  26. package/cmd/operations/gamma.js +2 -8
  27. package/cmd/operations/linear.js +2 -8
  28. package/cmd/operations/median.js +2 -8
  29. package/cmd/operations/modulate.js +10 -9
  30. package/cmd/operations/negate.js +2 -8
  31. package/cmd/operations/normalise.js +5 -9
  32. package/cmd/operations/recomb.js +2 -8
  33. package/cmd/operations/rotate.js +2 -8
  34. package/cmd/operations/sharpen.js +8 -9
  35. package/cmd/operations/threshold.js +2 -8
  36. package/cmd/operations/unflatten.js +2 -8
  37. package/cmd/output.js +7 -10
  38. package/cmd/resizing/extend.js +4 -10
  39. package/cmd/resizing/extract.js +3 -9
  40. package/cmd/resizing/resize.js +30 -16
  41. package/cmd/resizing/trim.js +12 -8
  42. package/{lib/queue.js → eslint.config.mjs} +20 -17
  43. package/lib/cli.js +473 -333
  44. package/lib/constants.js +4 -6
  45. package/lib/convert.js +120 -56
  46. package/lib/index.js +46 -17
  47. package/lib/utils.js +50 -0
  48. package/package.json +32 -34
package/lib/constants.js CHANGED
@@ -21,14 +21,11 @@
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
31
  CHANNEL: ["red", "green", "blue", "alpha"],
@@ -38,7 +35,7 @@ module.exports = {
38
35
  EXTEND_WITH: ["background", "copy", "repeat", "mirror"],
39
36
  FAIL_ON: ["none", "truncated", "error", "warning"],
40
37
  FIT: Object.keys(sharp.fit),
41
- FORMAT: ["avif", "gif", "heif", "jpeg", "jpg", "png", "tiff", "webp"],
38
+ FORMAT: ["avif", "gif", "heif", "jpeg", "png", "tiff", "webp"],
42
39
  GRAVITY: Object.keys(sharp.gravity),
43
40
  HEIF_COMPRESSION: ["hevc", "av1"],
44
41
  INTERPOLATORS: Object.keys(sharp.interpolators),
@@ -60,4 +57,5 @@ module.exports = {
60
57
  "zstd",
61
58
  ],
62
59
  TIFF_PREDICTOR: ["float", "horizontal", "none"],
60
+ TUNE: ["auto", "iq", "psnr", "ssim"],
63
61
  };
package/lib/convert.js CHANGED
@@ -21,21 +21,18 @@
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");
30
- const { pipeline } = require("stream/promises");
25
+ import { createReadStream } from "node:fs";
26
+ import { mkdir } from "node:fs/promises";
27
+ import path from "node:path";
28
+ import { pipeline } from "node:stream/promises";
31
29
 
32
30
  // Package modules.
33
- const { globSync } = require("glob");
34
- const isDirectory = require("is-directory");
35
- const sharp = require("sharp");
31
+ import { globSync } from "glob";
32
+ import sharp from "sharp";
36
33
 
37
34
  // Local modules.
38
- const queue = require("./queue");
35
+ import { drain, isDirectory } from "./utils.js";
39
36
 
40
37
  // Configure.
41
38
  const EXTENSIONS = {
@@ -48,69 +45,136 @@ const EXTENSIONS = {
48
45
  tiff: ".tiff",
49
46
  webp: ".webp",
50
47
  };
48
+ const FORMATS = {
49
+ ".avif": "avif",
50
+ ".gif": "gif",
51
+ ".jpg": "jpeg",
52
+ ".jpeg": "jpeg",
53
+ ".png": "png",
54
+ ".tif": "tiff",
55
+ ".tiff": "tiff",
56
+ ".webp": "webp",
57
+ };
58
+
59
+ // Resolve the non-magic portion of a glob to use as its relative path root.
60
+ const getGlobRoot = (pattern) => {
61
+ const absolute = path.resolve(pattern);
62
+ const magicIndex = absolute.search(/[*?[{(]/);
63
+ if (magicIndex === -1) {
64
+ return path.dirname(absolute);
65
+ }
66
+
67
+ const prefix = absolute.slice(0, magicIndex);
68
+ return prefix.endsWith(path.sep)
69
+ ? path.resolve(prefix)
70
+ : path.dirname(path.resolve(prefix));
71
+ };
51
72
 
52
73
  // Exports.
53
- module.exports = {
74
+ export default {
54
75
  // Convert a list of files.
55
- files: (input, output, options) => {
76
+ files: async (input, output, context) => {
56
77
  // Resolve files.
57
- const files = input.reduce((list, input) => {
58
- return list.concat(globSync(input, { absolute: true }));
59
- }, []);
60
-
78
+ const files = input.flatMap((pattern) => {
79
+ const root = getGlobRoot(pattern);
80
+ return globSync(pattern, { absolute: true }).map((src) => ({
81
+ root,
82
+ src,
83
+ }));
84
+ });
61
85
  if (files.length === 0) {
62
- return Promise.reject(new Error("No input files"));
86
+ throw new Error("No input files");
63
87
  }
64
88
 
65
89
  // Process files.
66
90
  const isBatch = files.length > 1;
67
- const promises = files.map((src) => {
68
- // Create pipeline.
69
- const transformer = queue.drain(sharp(options));
91
+ const promises = files.map(({ root, src }) => {
92
+ const image = sharp(context.options);
93
+ return pipeline(createReadStream(src), image)
94
+ .then(() => image.metadata())
95
+ .then((metadata) => {
96
+ // Process output as a template.
97
+ const parts = {
98
+ ...path.parse(src),
99
+ path: path.relative(root, path.dirname(src)),
100
+ };
101
+ const regex = /\{(root|dir|path|base|ext|name)\}/g;
102
+ let dest = output;
103
+ let match;
104
+ while ((match = regex.exec(output)) !== null) {
105
+ const [search, prop] = match;
106
+ dest = dest.replace(search, parts[prop]);
107
+ }
108
+ dest = path.resolve(dest);
70
109
 
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);
110
+ // If output was not a template, assume dest is a directory when using
111
+ // batch processing.
112
+ const outputAssumeDir = dest === path.resolve(output) && isBatch;
113
+ const outputIsDir = outputAssumeDir || isDirectory(dest);
114
+ const format =
115
+ context.format ??
116
+ (outputIsDir ? null : FORMATS[path.extname(dest).toLowerCase()]) ??
117
+ metadata.format;
118
+ if (outputIsDir) {
119
+ const defaultExt = path.extname(src);
120
+ dest = path.format({
121
+ dir: dest,
122
+ name: path.basename(src, defaultExt),
123
+ ext: format in EXTENSIONS ? EXTENSIONS[format] : defaultExt,
124
+ });
125
+ }
81
126
 
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
- }
127
+ const inputMetadata = { ...metadata, path: src };
128
+ const transformer = drain(context.queue, image, {
129
+ format,
130
+ metadata: inputMetadata,
131
+ });
94
132
 
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 }));
133
+ const promise = context.dry
134
+ ? transformer
135
+ .toBuffer({ resolveWithObject: true })
136
+ .then(({ info }) => info)
137
+ : mkdir(path.dirname(dest), { recursive: true }).then(() =>
138
+ transformer.toFile(dest),
139
+ );
140
+ return promise.then((info) => ({
141
+ input: inputMetadata,
142
+ output: { ...info, path: dest },
143
+ }));
144
+ });
100
145
  });
101
- return Promise.all(promises);
146
+ return Promise.allSettled(promises);
102
147
  },
103
148
 
104
149
  // Convert a stream.
105
- stream: (inStream, outStream, options) => {
106
- // Create pipeline.
107
- const transformer = queue.drain(sharp(options));
150
+ stream: async (inStream, outStream, context) => {
151
+ const image = sharp(context.options);
152
+ return pipeline(inStream, image)
153
+ .then(() => image.metadata())
154
+ .then((metadata) => {
155
+ const inputMetadata = { ...metadata, path: "stdin" };
156
+ const transformer = drain(context.queue, image, {
157
+ format: context.format ?? metadata.format,
158
+ metadata: inputMetadata,
159
+ });
160
+
161
+ if (context.dry) {
162
+ return transformer
163
+ .toBuffer({ resolveWithObject: true })
164
+ .then(({ info }) => ({
165
+ input: inputMetadata,
166
+ output: { ...info, path: "stdout" },
167
+ }));
168
+ }
108
169
 
109
- // Gather return value.
110
- const info = {};
111
- transformer.on("info", (_info) => Object.assign(info, _info));
170
+ // Gather return value.
171
+ const info = {};
172
+ transformer.on("info", (_info) => Object.assign(info, _info));
112
173
 
113
- // Pipe, and return as promise.
114
- return pipeline(inStream, transformer, outStream).then(() => info);
174
+ return pipeline(transformer, outStream).then(() => ({
175
+ input: inputMetadata,
176
+ output: { ...info, path: "stdout" },
177
+ }));
178
+ });
115
179
  },
116
180
  };
package/lib/index.js CHANGED
@@ -21,33 +21,62 @@
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 = {}) => {
30
+ export default (args, options = {}) => {
36
31
  const logger = options.logger || console; // Cast.
37
32
 
38
33
  // Parse arguments and handle i/o.
39
34
  return cli
40
- .parse(args)
35
+ .parseAsync(args)
41
36
  .then((argv) => {
42
- const options = pick(argv, cli.inputOptions);
37
+ const context = {
38
+ dry: argv.dry,
39
+ format: argv.format,
40
+ options: pick(argv, Object.keys(inputOptions)),
41
+ queue: argv["#queue"],
42
+ };
43
43
  if (argv.input) {
44
- 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
+ });
45
74
  }
46
- return convert.stream(process.stdin, process.stdout, options);
47
- })
48
- .then((output) => {
49
- const info = Array.isArray(output) ? output : [output];
50
- info.forEach((file) => logger.log(file.path));
75
+ return convert
76
+ .stream(process.stdin, process.stdout, context)
77
+ .then((output) => {
78
+ if (argv.print) logger.log(JSON.stringify(output));
79
+ });
51
80
  })
52
81
  .catch((err) => {
53
82
  if (err instanceof Error) {
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.3.0-depup.0",
3
+ "version": "6.1.0-depup.0",
4
4
  "description": "CLI for sharp. (with updated dependencies)",
5
5
  "keywords": [
6
6
  "sharp-cli",
@@ -20,64 +20,62 @@
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
- "repository": "vseventer/sharp-cli",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/vseventer/sharp-cli.git"
28
+ },
25
29
  "bin": {
26
30
  "sharp": "bin/cli.js"
27
31
  },
28
- "main": "lib/",
32
+ "main": "lib/index.js",
29
33
  "scripts": {
30
34
  "format": "prettier . --write",
31
- "pretest": "prettier . --check",
32
- "test": "nyc mocha",
35
+ "lint": "eslint",
36
+ "pretest": "npm run lint && prettier . --check",
37
+ "test": "NODE_OPTIONS=--experimental-loader=@istanbuljs/esm-loader-hook nyc mocha",
33
38
  "posttest": "nyc report --reporter=lcov"
34
39
  },
35
40
  "dependencies": {
36
41
  "glob": "^13.0.6",
37
- "is-directory": "^0.3.1",
38
- "lodash.pick": "^4.4.0",
39
- "sharp": "^0.35.3",
42
+ "sharp": "0.35.4",
40
43
  "yargs": "^18.1.0"
41
44
  },
42
45
  "devDependencies": {
43
- "fs-extra": "11.3.x",
44
- "mocha": "10.8.x",
45
- "must": "0.13.x",
46
- "nyc": "17.1.x",
47
- "prettier": "3.9.6",
48
- "sinon": "21.0.x",
49
- "tempy": "1.0.x"
46
+ "@eslint/js": "9.39.x",
47
+ "@istanbuljs/esm-loader-hook": "0.3.x",
48
+ "eslint": "9.39.x",
49
+ "fs-extra": "11.4.x",
50
+ "globals": "17.11.x",
51
+ "mocha": "11.8.x",
52
+ "nyc": "18.0.x",
53
+ "prettier": "3.9.x",
54
+ "sinon": "22.1.x",
55
+ "tempy": "3.2.x"
56
+ },
57
+ "overrides": {
58
+ "diff": "8.0.3",
59
+ "serialize-javascript": "7.1.1"
50
60
  },
51
61
  "engines": {
52
- "node": ">=18.17"
62
+ "node": ">=20.10.0"
53
63
  },
54
64
  "depup": {
55
65
  "changes": {
56
66
  "glob": {
57
- "from": "10.5.x",
67
+ "from": "13.0.x",
58
68
  "to": "^13.0.6"
59
69
  },
60
- "is-directory": {
61
- "from": "0.3.x",
62
- "to": "^0.3.1"
63
- },
64
- "lodash.pick": {
65
- "from": "3.1.0",
66
- "to": "^4.4.0"
67
- },
68
- "sharp": {
69
- "from": "0.34.5",
70
- "to": "^0.35.3"
71
- },
72
70
  "yargs": {
73
- "from": "^17.6.2",
71
+ "from": "17.7.x",
74
72
  "to": "^18.1.0"
75
73
  }
76
74
  },
77
- "depsUpdated": 5,
75
+ "depsUpdated": 2,
78
76
  "originalPackage": "sharp-cli",
79
- "originalVersion": "5.3.0",
80
- "processedAt": "2026-08-16T00:30:13.494Z",
81
- "smokeTest": "failed"
77
+ "originalVersion": "6.1.0",
78
+ "processedAt": "2026-09-06T01:03:50.312Z",
79
+ "smokeTest": "passed"
82
80
  }
83
81
  }