-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (82 loc) · 2.53 KB
/
Copy pathindex.js
File metadata and controls
89 lines (82 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const {
exec
} = require('child_process');
const path = require('path');
const fs = require('hexo-fs');
const utils = require("./libs/utils");
var config = Object.assign({
enable: false,
expire: 30,
rclonepath: "",
type: [],
}, hexo.config.autobackup);
const flag = "Auto Backup Plugin: "
const w_rclone_filename = "rclone.exe"
const hexo_dir = path.dirname(path.dirname(__dirname));
const dir = utils.genDateStr();
hexo.on('after_deploy', function () {
if (!config.enable) {
return;
}
start();
});
function start() {
hexo.log.info(flag + "Backup " + dir);
config.type.forEach(ele => {
parseType(ele);
});
}
function parseType(type) {
const arr = type.split("<");
if(!arr[0] || !arr[1]) {
hexo.log.error(flag + "Invaild type: " + type);
return;
}
type = arr[0].trim();
dest_path = arr[1].trim();
// Cache is good, but it does not help anything
const real_dest_path = path.join(dest_path, dir);
const source = path.join(real_dest_path, "source");
const themes = path.join(real_dest_path, "themes")
// const packagejson = path.join(real_dest_path, "package.json")
// const configyml = path.join(real_dest_path, "_config.yml")
if (!config.rclonepath) {hex
hexo.log.error(flag + "rclonepath not defined!");
return;
}
if (!fs.existsSync(config.rclonepath)) {
hexo.log.error(flag + "rclone.exe not exist!");
return;
}
const o_source = path.join(hexo_dir, "source");
const o_themes = path.join(hexo_dir, "themes");
const o_packagejson = path.join(hexo_dir, "package.json");
const o_configyml = path.join(hexo_dir, "_config.yml");
const rcp = path.join(config.rclonepath, w_rclone_filename );
if (type == "local") {
type = "";
} else {
type += ":";
}
cmdWithOutput(`${rcp} copy ${o_source} ${type}${source}`)
cmd(`${rcp} copy ${o_themes} ${type}${themes}`)
cmd(`${rcp} copy ${o_packagejson} ${type}${real_dest_path}`)
cmd(`${rcp} copy ${o_configyml} ${type}${real_dest_path}`)
if (type == "") type = "local";
hexo.log.info(flag + type + " backup running!");
}
function cmdWithOutput(cmd) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
hexo.log.error(flag + " backup miscondigured!");
hexo.log.error(`CMD Err: ${error.message}`);
}
});
}
function cmd(cmd) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
hexo.log.error(flag + " cmd error!");
}
});
}