node-automator 1.4.29 → 1.4.30

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.
@@ -10,34 +10,52 @@ const Jimp = require("jimp");
10
10
  class CompressCommand extends BaseCommand {
11
11
  async execute() {
12
12
  const data = this.selfData;
13
- const src = get_fst_file(data.src);
14
- const dstFile = get_full_path(data.dst, "FILE");
15
- fs.copyFileSync(src, dstFile);
16
- const dstDir = _path.dirname(dstFile);
17
- if (!fs.existsSync(dstDir)) {
18
- fs.mkdirSync(dstDir, { recursive: true });
19
- }
20
- if (data.resize) {
21
- const { w, h, scale } = data.resize;
22
- const result = await Jimp.read(dstFile);
23
- if (w && h) {
24
- result.resize(w, h);
13
+ const options = { quality: 80, ...data.options };
14
+ if (data.mode === "sharp") {
15
+ const sharp = require("sharp");
16
+ const data = this.selfData;
17
+ const src = get_fst_file(data.src);
18
+ const dst = get_full_path(data.dst, "FILE");
19
+ let format = data.format;
20
+ if (!format) {
21
+ format = dst.split(".").pop();
22
+ }
23
+ format = format.toLowerCase();
24
+ if (format === "jpg") {
25
+ format = "jpeg";
26
+ }
27
+ const fileContent = fs.readFileSync(src);
28
+ await sharp(fileContent)[format](options).toFile(dst);
29
+ } else {
30
+ const src = get_fst_file(data.src);
31
+ const dstFile = get_full_path(data.dst, "FILE");
32
+ fs.copyFileSync(src, dstFile);
33
+ const dstDir = _path.dirname(dstFile);
34
+ if (!fs.existsSync(dstDir)) {
35
+ fs.mkdirSync(dstDir, { recursive: true });
25
36
  }
26
- if (scale && scale != 1) {
27
- result.scale(scale);
37
+ if (data.resize) {
38
+ const { w, h, scale } = data.resize;
39
+ const result = await Jimp.read(dstFile);
40
+ if (w && h) {
41
+ result.resize(w, h);
42
+ }
43
+ if (scale && scale != 1) {
44
+ result.scale(scale);
45
+ }
46
+ await result.writeAsync(dstFile);
28
47
  }
29
- await result.writeAsync(dstFile);
48
+ const quality = options?.quality || 80;
49
+ await imagemin([dstFile], {
50
+ destination: dstDir,
51
+ plugins: [
52
+ imageminMozjpeg({ quality: quality }), // 压缩 JPG 图片
53
+ imageminPngquant({
54
+ quality: [quality / 100, quality / 100],
55
+ }),
56
+ ],
57
+ });
30
58
  }
31
- const quality = data.options?.quality || 80;
32
- await imagemin([dstFile], {
33
- destination: dstDir,
34
- plugins: [
35
- imageminMozjpeg({ quality: quality }), // 压缩 JPG 图片
36
- imageminPngquant({
37
- quality: [quality / 100, quality / 100],
38
- }),
39
- ],
40
- });
41
59
  }
42
60
  }
43
61
 
@@ -36,6 +36,10 @@ class TinifyCommand extends BaseCommand {
36
36
  }
37
37
  }
38
38
  await tinifyData.toFile(dstFile);
39
+ const compressionCount = tinify.compressionCount;
40
+ return {
41
+ compressionCount,
42
+ };
39
43
  }
40
44
 
41
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-automator",
3
- "version": "1.4.29",
3
+ "version": "1.4.30",
4
4
  "description": "Execute automation with yaml configuration(compatible with json)",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -61,6 +61,7 @@
61
61
  "pinyin": "^4.0.0",
62
62
  "request": "^2.88.2",
63
63
  "request-progress": "^3.0.0",
64
+ "sharp": "^0.34.5",
64
65
  "split-file": "^2.3.0",
65
66
  "spreadsheet-column": "^1.1.1",
66
67
  "strip-json-comments": "^3.1.1",
@@ -25,6 +25,7 @@ async function exec_shell(data) {
25
25
  const instant_stdout = data.instant_stdout;
26
26
  const capture_stdout = isStdOut() || data.capture_stdout;
27
27
  const acceptableErrors = data.acceptableErrors || [];
28
+ const acceptableCodes = data.acceptableCodes || [0];
28
29
  const quiet = !data.verbose;
29
30
  const result = await new Promise((resolve, _reject) => {
30
31
  /** @type {string[]} */
@@ -100,7 +101,7 @@ async function exec_shell(data) {
100
101
  });
101
102
  // child.on('close', code => {
102
103
  child.on("exit", (code) => {
103
- if (!data.ignore_code && code !== 0) {
104
+ if (!data.ignore_code && !acceptableCodes.includes(code)) {
104
105
  const errMsg = !raw_output
105
106
  ? iconv.decode(Buffer.concat(errors), encoding)
106
107
  : Buffer.concat(errors).toString();