node-automator 1.4.28 → 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) {
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
 
package/commands/mgr.js CHANGED
@@ -137,6 +137,7 @@ const { MergeFileCommand } = require("./merge_file");
137
137
  const { ZipFolderCommand } = require("./zip_folder");
138
138
  const { EncryptCommand } = require('./encrypt');
139
139
  const { DecryptCommand } = require('./decrypt');
140
+ const { TinifyCommand } = require('./tinify');
140
141
 
141
142
  const globalData = {
142
143
  executed_cfg: [], // 执行过的配置文件
@@ -525,6 +526,7 @@ function init() {
525
526
  register("zip_folder", ZipFolderCommand, false);
526
527
  register("encrypt", EncryptCommand, false);
527
528
  register("decrypt", DecryptCommand, false);
529
+ register("tinify", TinifyCommand, false);
528
530
  }
529
531
 
530
532
  module.exports = {
@@ -0,0 +1,49 @@
1
+ const { BaseCommand } = require("./base");
2
+ const tinify = require("tinify");
3
+ const Jimp = require("jimp");
4
+ const { get_fst_file, get_full_path } = require('../utils/file_tool');
5
+ const { dirname } = require('node:path');
6
+ const fs = require('node:fs');
7
+
8
+ class TinifyCommand extends BaseCommand {
9
+ async execute() {
10
+ const data = this.selfData;
11
+ tinify.key = data.api_key;
12
+ const src = get_fst_file(data.src);
13
+ const dstFile = get_full_path(data.dst, "FILE");
14
+ fs.copyFileSync(src, dstFile);
15
+ const dstDir = dirname(dstFile);
16
+ if (!fs.existsSync(dstDir)) {
17
+ fs.mkdirSync(dstDir, { recursive: true });
18
+ }
19
+ let tinifyData = tinify.fromFile(src);
20
+ if (data.resize) {
21
+ const { w, h, scale } = data.resize;
22
+ if (w && h) {
23
+ tinifyData = tinifyData.resize({
24
+ method: "fit",
25
+ width: w,
26
+ height: h,
27
+ });
28
+ }
29
+ if (scale && scale != 1) {
30
+ const result = await Jimp.read(dstFile);
31
+ const width = result.getWidth();
32
+ tinifyData = tinifyData.scale({
33
+ method: "scale",
34
+ width: width * scale,
35
+ });
36
+ }
37
+ }
38
+ await tinifyData.toFile(dstFile);
39
+ const compressionCount = tinify.compressionCount;
40
+ return {
41
+ compressionCount,
42
+ };
43
+ }
44
+
45
+ }
46
+
47
+ module.exports = {
48
+ TinifyCommand,
49
+ };
package/index.js CHANGED
@@ -94,6 +94,7 @@ async function MainProcess() {
94
94
  progress(elapsedTime, estimateTime, {
95
95
  desc: "预计耗时",
96
96
  format_time: true,
97
+ color: _estimateLeft > 0 ? "reset" : "red",
97
98
  });
98
99
  }
99
100
  }, timerInterval);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-automator",
3
- "version": "1.4.28",
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,10 +61,12 @@
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",
67
68
  "temp": "^0.9.4",
69
+ "tinify": "^1.8.2",
68
70
  "typescript-plus": "^3.1.5",
69
71
  "websocket": "^1.0.34",
70
72
  "xlsx": "^0.17.0",
@@ -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();