node-automator 1.3.14 → 1.4.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.
- package/commands/backup.js +108 -0
- package/commands/mgr.js +2 -0
- package/package.json +1 -1
- package/utils/func_tool.js +2 -6
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
} = require('../utils/file_tool');
|
|
16
|
+
|
|
17
|
+
class BackupCommand extends BaseCommand {
|
|
18
|
+
async execute() {
|
|
19
|
+
const {
|
|
20
|
+
sourceFolder,
|
|
21
|
+
backupFolder,
|
|
22
|
+
backupCfgs,
|
|
23
|
+
quiet,
|
|
24
|
+
} = this.selfData;
|
|
25
|
+
|
|
26
|
+
async function backup(bakCfg, filename) {
|
|
27
|
+
const {
|
|
28
|
+
sourceEncoding,
|
|
29
|
+
sourceKeys,
|
|
30
|
+
sourceType,
|
|
31
|
+
backupFormat,
|
|
32
|
+
backupFilename,
|
|
33
|
+
backupEncoding
|
|
34
|
+
} = bakCfg;
|
|
35
|
+
const filepath = path.join(sourceFolder, filename);
|
|
36
|
+
let backupPath = path.join(backupFolder, backupFilename || filename);
|
|
37
|
+
let backupData;
|
|
38
|
+
if (sourceType) {
|
|
39
|
+
backupData = await parse(fs.readFileSync(filepath, sourceEncoding), sourceType);
|
|
40
|
+
} else {
|
|
41
|
+
backupData = fs.readFileSync(filepath, {
|
|
42
|
+
encoding: backupEncoding,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (sourceKeys) {
|
|
46
|
+
for (const key of sourceKeys) {
|
|
47
|
+
backupData = backupData[key];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (backupFormat) {
|
|
51
|
+
backupData = await stringify(backupData, backupFormat);
|
|
52
|
+
}
|
|
53
|
+
const backupDir = path.dirname(backupPath);
|
|
54
|
+
// 创建目录
|
|
55
|
+
if (!fs.existsSync(backupDir)) {
|
|
56
|
+
fs.mkdirSync(backupDir, {
|
|
57
|
+
recursive: true
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
fs.writeFileSync(backupPath, backupData, {
|
|
61
|
+
encoding: backupEncoding
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
backupCfgs.forEach((bakCfg, index) => {
|
|
67
|
+
if (typeof bakCfg === "string") {
|
|
68
|
+
backupCfgs[index] = {
|
|
69
|
+
sourceFilename: bakCfg,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
// 首次备份所有
|
|
74
|
+
backupCfgs.forEach(cfg => {
|
|
75
|
+
const files = glob.sync(path.join(sourceFolder, cfg.sourceFilename)).map(v => path.relative(sourceFolder, v));
|
|
76
|
+
files.forEach(file => {
|
|
77
|
+
backup(cfg, file);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// 监听文件夹变化
|
|
82
|
+
fs.watch(sourceFolder, {
|
|
83
|
+
recursive: true
|
|
84
|
+
}, (eventType, filename) => {
|
|
85
|
+
if (eventType === "change") {
|
|
86
|
+
const bakCfg = backupCfgs.find(cfg => {
|
|
87
|
+
const files = glob.sync(path.join(sourceFolder, cfg.sourceFilename)).map(v => path.relative(sourceFolder, v));
|
|
88
|
+
return files.some(file => file === filename);
|
|
89
|
+
});
|
|
90
|
+
if (bakCfg) {
|
|
91
|
+
backup(bakCfg, filename).then(() => {
|
|
92
|
+
!quiet && success(`${filename} 备份成功`);
|
|
93
|
+
})
|
|
94
|
+
.catch(err => {
|
|
95
|
+
error(`${filename} 备份异常 ${err}`);
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
.on('error', (err) => {
|
|
101
|
+
error(`备份异常: ${err}`);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = {
|
|
107
|
+
BackupCommand,
|
|
108
|
+
};
|
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
package/utils/func_tool.js
CHANGED
|
@@ -83,15 +83,11 @@ function queueAsync(tasks, limit, onProgress) {
|
|
|
83
83
|
onProgress && onProgress(numDone, tasks.length, currIndex);
|
|
84
84
|
task().then(function (val) {
|
|
85
85
|
numDone++;
|
|
86
|
-
result
|
|
87
|
-
val,
|
|
88
|
-
});
|
|
86
|
+
result[currIndex] = { val };
|
|
89
87
|
checkDone(currIndex, { val });
|
|
90
88
|
}).catch(function (err) {
|
|
91
89
|
numDone++;
|
|
92
|
-
result
|
|
93
|
-
err,
|
|
94
|
-
});
|
|
90
|
+
result[currIndex] = { err };
|
|
95
91
|
checkDone(currIndex, { err });
|
|
96
92
|
});
|
|
97
93
|
}
|