node-automator 1.4.29 → 1.4.31

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
 
package/commands/mgr.js CHANGED
@@ -138,6 +138,7 @@ const { ZipFolderCommand } = require("./zip_folder");
138
138
  const { EncryptCommand } = require('./encrypt');
139
139
  const { DecryptCommand } = require('./decrypt');
140
140
  const { TinifyCommand } = require('./tinify');
141
+ const { WatchCommand } = require('./watch');
141
142
 
142
143
  const globalData = {
143
144
  executed_cfg: [], // 执行过的配置文件
@@ -527,6 +528,7 @@ function init() {
527
528
  register("encrypt", EncryptCommand, false);
528
529
  register("decrypt", DecryptCommand, false);
529
530
  register("tinify", TinifyCommand, false);
531
+ register("watch", WatchCommand, false);
530
532
  }
531
533
 
532
534
  module.exports = {
@@ -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
  }
@@ -0,0 +1,49 @@
1
+ const { watch } = require('node:fs');
2
+ const { BaseCommand } = require("./base");
3
+ const { get_full_path } = require('./share_data');
4
+ const { throttle } = require('../utils/func_tool');
5
+
6
+ class WatchCommand extends BaseCommand {
7
+ async execute() {
8
+ const data = this.selfData;
9
+ const src = get_full_path(data.src);
10
+ const throttleDelay = data.throttleDelay;
11
+ const options = {
12
+ recursive: true,
13
+ ...data.options,
14
+ };
15
+ let Handler = async () => {
16
+ let commandCfgs = data.commands;
17
+ // 特殊处理字符的情况
18
+ if (typeof commandCfgs === "string") {
19
+ commandCfgs = this.formatData(commandCfgs);
20
+ }
21
+ // 默认从循环开始执行,增加深度
22
+ await this.exec(commandCfgs, this.depth + 1);
23
+ }
24
+ if (throttleDelay != undefined) {
25
+ Handler = throttle(Handler, throttleDelay);
26
+ }
27
+ watch(
28
+ src,
29
+ options,
30
+ (eventType, filename) => {
31
+ this.shareData.WatchPayload = {
32
+ eventType,
33
+ filename,
34
+ };
35
+ Handler();
36
+ },
37
+ ).on("error", (err) => {
38
+ error(`Watch error: ${err}`);
39
+ });
40
+ }
41
+
42
+ getRequiredParams() {
43
+ return ["src", "commands"];
44
+ }
45
+ }
46
+
47
+ module.exports = {
48
+ WatchCommand,
49
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-automator",
3
- "version": "1.4.29",
3
+ "version": "1.4.31",
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();