forked from zhaoolee/ChromeAppHeroes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.js
164 lines (113 loc) · 3.95 KB
/
push.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
const fse = require("fs-extra");
const path = require("path");
const download = require("download");
const fs = require("fs");
const { spawn } = require("child_process");
let RepositoriesName = "ChromeAppHeroes";
let readme_img_dir =
"https://raw.githubusercontent.com/zhaoolee/" +
RepositoriesName +
"/master/README/";
// md所在的文件夹
let zhaoolee_md_dir = "page";
// readme文件名
let readme_file_name = "readme-chromeappheroes.md";
// 可以跳过的图片地址关键词
let ignore_img_list = ["img.shields.io", "www.bilibili.com"];
// let md_dir = path.join(__dirname, RepositoriesName+"MD");
// 将README.md从 项目名+md文件夹内拷贝到外面,并将图片替换下载到README文件夹
async function cp_readme_md() {
// 在当前目录生成README文件夹
// 创建backup文件夹
if (!fs.existsSync(path.join(__dirname, "README"))) {
fs.mkdirSync(path.join(__dirname, "README"));
}
let origin_readme_file_pathname = path.join(
__dirname,
zhaoolee_md_dir,
readme_file_name
);
let target_readme_file_pathname = path.join(__dirname, "README" + ".md");
fse.copySync(origin_readme_file_pathname, target_readme_file_pathname, {
overwrite: true
});
await download_imgs_by_md("README.md");
await change_img_url("README.md");
}
// 将README.md内的图片下载到README文件夹
async function download_imgs_by_md(file_name) {
// 获取文件夹名称
let dir_name = file_name.match(/^(.*).md/)[1];
let file_content = fs.readFileSync(file_name).toString();
let img_addr_re = /\!\[.*\]\(.*\)/g;
let md_img_addr_s = file_content.match(img_addr_re);
let md_img_addr_s_length = md_img_addr_s.length;
let img_re = /^!\[(.*)\]\((.*)\)/;
for (let i = 0; i < md_img_addr_s_length; i++) {
let download_img = true;
for(let t =0 ; t<ignore_img_list.length; t++){
if (md_img_addr_s[i].indexOf(ignore_img_list[t]) !==-1) {
download_img = false;
}
}
if (download_img) {
let img_addr = md_img_addr_s[i].match(img_re)[2];
// 定义文件名
let img_addr_list = img_addr.split("/");
img_name = img_addr_list[img_addr_list.length - 1];
if (!fs.existsSync(path.join(__dirname, dir_name, img_name))) {
console.log("正在下载>>>", img_addr);
fs.writeFileSync(
path.join(__dirname, dir_name, img_name),
await download(img_addr)
);
}
}
}
}
// 更换url
async function change_img_url(file_name) {
let dir_name = file_name.match(/^(.*).md/)[1];
let file_content = fs.readFileSync(file_name).toString();
let img_addr_re = /\!\[.*\]\(.*\)/g;
let md_img_addr_s = file_content.match(img_addr_re);
let md_img_addr_s_length = md_img_addr_s.length;
let img_re = /^!\[(.*)\]\((.*)\)/;
for (let i = 0; i < md_img_addr_s_length; i++) {
let download_img = true;
for(let t =0 ; t<ignore_img_list.length; t++){
if (md_img_addr_s[i].indexOf(ignore_img_list[t]) !==-1) {
download_img = false;
}
}
if (download_img) {
let img_addr = md_img_addr_s[i].match(img_re)[2];
// 定义文件名
let img_addr_list = img_addr.split("/");
img_name = img_addr_list[img_addr_list.length - 1];
let new_img_addr = readme_img_dir + img_name;
file_content = file_content.replace(img_addr, new_img_addr);
fs.writeFileSync(path.join(__dirname, file_name), file_content);
}
}
}
async function md_to_wordpress(){
const run_build = spawn("node", ["md_to_wordpress.js"], {
cwd: path.join(__dirname, zhaoolee_md_dir)
});
run_build.stdout.on("data", async data => {
console.log(`stdout: ${data}`);
});
run_build.stderr.on("data", async data => {
console.log("data::", String(data));
});
run_build.on("close", async code => {
console.log("程序执行完成");
});
}
// 进入项目名加MD文件夹,执行md_to_wordpress
async function main() {
await cp_readme_md();
await md_to_wordpress();
}
main();