-
Notifications
You must be signed in to change notification settings - Fork 239
/
rsync.js
178 lines (129 loc) · 4.17 KB
/
rsync.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const Rsync = require("rsync");
const path = require("path");
const chmodr = require('chmodr');
const fs = require("fs");
const fse = require("fs-extra");
// 项目文件夹名
const main_dir_name = "OnlineToolsBook";
// markdown文件夹名
const md_dir_name = "OnlineToolsBookMD";
// 需要排除的md文件
const exclude_md_files = [];
// 自定义
const target_url =
"https://www.v2fy.com/asset/0i/" + main_dir_name + "/" + md_dir_name + "/";
console.log("target_url::", target_url);
const target_path = "/usr/share/nginx/v2fy.com/asset/0i";
// 将本目录同步服务器
async function update_data() {
await new Promise((resolve, reject) => {
// Build the command
var rsync = new Rsync()
.shell("ssh")
.flags({
a: true,
z: true,
v: true
})
.delete()
.progress()
.compress()
.exclude([".*/", "up.js"])
.output(
function(data) {
// do things like parse progress
console.log("=传输数据=>>", data.toString());
},
function(data) {
// do things like parse error output
console.log("=数据传输报错=>>", data.toString());
}
)
.source(path.join(__dirname))
.destination("[email protected]:" + target_path);
// Execute the command
rsync.execute(function(error, code, cmd) {
console.log("error", error);
console.log("code", code);
console.log("cmd", cmd);
resolve();
});
}).then(() => {});
}
// 获取md文件
function get_md_file_list() {
// 读取当前当前目录下的.md文件
let root = "./";
root = path.join(__dirname, md_dir_name);
console.log(root);
let all_files = fs.readdirSync(root);
let all_md_files = [];
// 获取md文件列表
all_files.map((file_name, file_index) => {
if (file_name.endsWith(".md")) {
// md文件不包含在排除列表中
if (exclude_md_files.indexOf(file_name) === -1) {
all_md_files.push(file_name);
}
}
});
return all_md_files;
}
// 将md文件内容替换为https内容
function local_file_href_2_https_href(md_file_name) {
const whole_md_file_path = path.join(__dirname, md_dir_name, md_file_name);
let file_content = fs.readFileSync(whole_md_file_path).toString();
let img_addr_re = /\!\[.*\]\(.*\)/g;
let md_img_addr_list = file_content.match(img_addr_re);
if(md_img_addr_list === null){
md_img_addr_list = []
}
for (let img_index = 0; img_index < md_img_addr_list.length; img_index++) {
let tmp_md_img_addr = md_img_addr_list[img_index];
let img_addr = tmp_md_img_addr.match(/\((.*)\)/)[1];
let img_desc = tmp_md_img_addr.match(/\[(.*)\]/)[1];
if (img_addr.indexOf("http") !== 0) {
let new_img_addr = target_url + img_addr;
new_tmp_md_img_addr = "![" + img_desc + "](" + new_img_addr + ")";
file_content = file_content.replace(tmp_md_img_addr, new_tmp_md_img_addr);
console.log("准备替换==>", tmp_md_img_addr, "为==>>", new_tmp_md_img_addr)
}else{
console.log("略过==>>", img_addr);
}
}
console.log(file_content);
fs.writeFileSync(whole_md_file_path, file_content, {encoding: "utf8"})
// file_content = file_content.replace(/\!\[.*\]\(.*\)/g, target_url+'$1');
// console.log(md_img_addr_list, "--->>", img_addr);
}
// 将Markdown文件中本地图片链接替换为网络图片链接
async function local_href_2_https_href() {
let md_file_list = get_md_file_list();
console.log("md_file_list==>>", md_file_list);
// md_file_list = ["kr-000046.md"];
for (let i = 0; i < md_file_list.length; i++) {
console.log("=读取=>>", md_file_list[i]);
local_file_href_2_https_href(md_file_list[i]);
}
}
async function md_and_img_chmodr(path){
await new Promise((resolve, reject)=>{
chmodr(path, 0o777, (err) => {
if (err) {
console.log('Failed to execute chmod', err);
} else {
console.log('Success');
resolve();
}
});
})
}
async function main() {
// 改权限
await md_and_img_chmodr(path.join(__dirname, md_dir_name))
// 先同步数据到服务端
await update_data();
// console.log("执行完成")
local_href_2_https_href();
}
main();