node-automator 1.3.15 → 1.4.1

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.
@@ -0,0 +1,113 @@
1
+ const {
2
+ BaseCommand
3
+ } = require("./base");
4
+
5
+ const glob = require("glob");
6
+ const fs = require("fs");
7
+ const path = require("path");
8
+ const {
9
+ success,
10
+ error
11
+ } = require('../utils/log_tool');
12
+ const {
13
+ parse,
14
+ stringify,
15
+ processRename
16
+ } = require('../utils/file_tool');
17
+
18
+ class BackupCommand extends BaseCommand {
19
+ async execute() {
20
+ const {
21
+ sourceFolder,
22
+ backupFolder,
23
+ backupCfgs,
24
+ quiet,
25
+ } = this.selfData;
26
+
27
+ async function backup(bakCfg, filename) {
28
+ const {
29
+ sourceEncoding,
30
+ sourceKeys,
31
+ sourceType,
32
+ backupFormat,
33
+ backupFilename,
34
+ backupRename,
35
+ backupEncoding
36
+ } = bakCfg;
37
+ const filepath = path.join(sourceFolder, filename);
38
+ let backupPath = path.join(backupFolder, backupFilename || filename);
39
+ let backupData;
40
+ if (sourceType) {
41
+ backupData = await parse(fs.readFileSync(filepath, sourceEncoding), sourceType);
42
+ } else {
43
+ backupData = fs.readFileSync(filepath, {
44
+ encoding: backupEncoding,
45
+ });
46
+ }
47
+ if (sourceKeys) {
48
+ for (const key of sourceKeys) {
49
+ backupData = backupData[key];
50
+ }
51
+ }
52
+ if (backupFormat) {
53
+ backupData = await stringify(backupData, backupFormat);
54
+ }
55
+ if (backupRename) {
56
+ backupPath = processRename(backupPath, backupRename, backupData);
57
+ }
58
+ const backupDir = path.dirname(backupPath);
59
+ // 创建目录
60
+ if (!fs.existsSync(backupDir)) {
61
+ fs.mkdirSync(backupDir, {
62
+ recursive: true
63
+ });
64
+ }
65
+ fs.writeFileSync(backupPath, backupData, {
66
+ encoding: backupEncoding
67
+ });
68
+ }
69
+
70
+
71
+ backupCfgs.forEach((bakCfg, index) => {
72
+ if (typeof bakCfg === "string") {
73
+ backupCfgs[index] = {
74
+ sourceFilename: bakCfg,
75
+ }
76
+ }
77
+ })
78
+ // 首次备份所有
79
+ backupCfgs.forEach(cfg => {
80
+ const files = glob.sync(path.join(sourceFolder, cfg.sourceFilename)).map(v => path.relative(sourceFolder, v));
81
+ files.forEach(file => {
82
+ backup(cfg, file);
83
+ });
84
+ });
85
+
86
+ // 监听文件夹变化
87
+ fs.watch(sourceFolder, {
88
+ recursive: true
89
+ }, (eventType, filename) => {
90
+ if (eventType === "change") {
91
+ const bakCfg = backupCfgs.find(cfg => {
92
+ const files = glob.sync(path.join(sourceFolder, cfg.sourceFilename)).map(v => path.relative(sourceFolder, v));
93
+ return files.some(file => file === filename);
94
+ });
95
+ if (bakCfg) {
96
+ backup(bakCfg, filename).then(() => {
97
+ !(quiet || bakCfg.quiet) && success(`${filename} 备份成功`);
98
+ })
99
+ .catch(err => {
100
+ error(`${filename} 备份异常 ${err}`);
101
+ })
102
+ }
103
+ }
104
+ })
105
+ .on('error', (err) => {
106
+ error(`备份异常: ${err}`);
107
+ });
108
+ }
109
+ }
110
+
111
+ module.exports = {
112
+ BackupCommand,
113
+ };
package/commands/mgr.js CHANGED
@@ -116,6 +116,7 @@ const { CursorUpCommand } = require('./cursor_up');
116
116
  const { RegisterSearchInstallLocationCommand } = require('./register_search_install_location');
117
117
  const { DeobfuscateCommand } = require('./deobfuscate');
118
118
  const { AliOssCommand } = require('./ali_oss');
119
+ const { BackupCommand } = require('./backup');
119
120
 
120
121
  const globalData = {
121
122
  "executed_cfg": [], // 执行过的配置文件
@@ -475,6 +476,7 @@ function init() {
475
476
  register("register_search_install_location", RegisterSearchInstallLocationCommand, false);
476
477
  register("deobfuscate", DeobfuscateCommand, false);
477
478
  register("ali_oss", AliOssCommand, false);
479
+ register("backup", BackupCommand, false);
478
480
  }
479
481
 
480
482
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-automator",
3
- "version": "1.3.15",
3
+ "version": "1.4.1",
4
4
  "description": "Execute automation with yaml configuration(compatible with json)",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -16,6 +16,8 @@ const iconv = require('iconv-lite');
16
16
  const chardet = require("chardet");
17
17
  const { get_full_path } = require("../commands/share_data");
18
18
  const { parse, stringify } = require("./parse_tool");
19
+ const { hash } = require('./hash_tool');
20
+ const { formatDate } = require('./transform_tool');
19
21
  /**
20
22
  *
21
23
  * @param {Array | string} src
@@ -213,7 +215,7 @@ async function read_cfg(src, type, options) {
213
215
  return parse(content, type, options);
214
216
  }
215
217
 
216
- function processRename(fullpath, params) {
218
+ function processRename(fullpath, params, content) {
217
219
  if (!params) return fullpath;
218
220
  let dirname = path.dirname(fullpath);
219
221
  switch (typeof params) {
@@ -244,6 +246,8 @@ function processRename(fullpath, params) {
244
246
  if (params.dir_suffix) dirname = dirname + params.dir_suffix;
245
247
  if (params.base_prefix) basename = params.base_prefix + basename;
246
248
  if (params.base_suffix) basename = basename + params.base_suffix;
249
+ if (params.hash_suffix) basename = basename + "." + hash(content).substring(0, 8);
250
+ if (params.time_suffix) basename = basename + "." + formatDate(new Date(), "YYYYMMDDhhmmss");
247
251
  return path.join(dirname, basename + extname);
248
252
  }
249
253
  }
@@ -289,7 +293,7 @@ async function copy(src, dst, base, options) {
289
293
  } else {
290
294
  dstFile = path.join(dst, path.relative(base, src));
291
295
  }
292
- dstFile = processRename(dstFile, options.rename);
296
+ dstFile = processRename(dstFile, options.rename, fs.readFileSync(src));
293
297
  let dstDir = path.dirname(dstFile);
294
298
  if (!fs.existsSync(dstDir) || !fs.lstatSync(dstDir).isDirectory()) {
295
299
  try {