Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
260 lines (141 loc) · 6.14 KB
/
Copy pathmain.js
File metadata and controls
260 lines (141 loc) · 6.14 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const request = require("request");
const fs = require("fs-extra");
const path = require("path");
// 读取用户输入
async function readline_sync() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise((resolve, reject) => {
readline.question(``, data => {
readline.close();
resolve(data)
})
});
}
async function get_github_username_repositories_name() {
// 查看.git文件夹是否存在
let exist = fs.existsSync(path.join(__dirname, ".git", "config"));
// 如果.git存在则读取username和repositories_name
let username = "";
let repositories_name = "";
let url = "";
if (exist) {
let config_content = String(fs.readFileSync(path.join(__dirname, ".git", "config")))
let re_n_t = /\n|\t/;
let config_content_list = config_content.split(re_n_t)
for (let i = 0, config_content_list_length = config_content_list.length; i < config_content_list_length; i++) {
if (config_content_list[i].indexOf("url") === 0) {
url = config_content_list[i].split("url = ")[1];
url = url.split(".git")[0]
let url_info = url.split("/");
// console.log(url_info);
url_info.reverse();
username = url_info[1];
repositories_name = url_info[0];
}
}
}
// 如果.git不存在则要求输入username和repositories_name
if (exist === false) {
console.log("请输入github用户名:");
username = await readline_sync();
console.log("请输入仓库名:");
repositories_name = await readline_sync();
}
console.log(username, repositories_name);
return new Promise((resolve, reject) => {
resolve({ username: username, repositories_name: repositories_name })
})
}
function get_img_url_list_in_md(md_path) {
const md_content = String(fs.readFileSync(md_path));
re_md_img = /\!\[(.*)\]\((.*)\)/g;
let md_img_list = md_content.match(re_md_img);
console.log(md_img_list);
let img_url_list = [];
for (let i = 0, md_img_list_length = md_img_list.length; i < md_img_list_length; i++) {
img_url = md_img_list[i].match(/(.*)\((.*)\)/)[2];
img_url_list.push(img_url);
}
return img_url_list;
}
// 创建随机数函数
// 随机产生字符串
function randomString(e) {
e = e || 32;
var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz012345678",
a = t.length,
n = "";
for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
return n
}
function download_img_to_readme_dir(img_url, pre_image_url){
let new_img_url = "";
return new Promise((resolve, reject)=>{
// 如果以http开头,则进行下载
if(img_url.indexOf("http") === 0){
try {
let img_url_info = img_url.split(".")
img_url_info.reverse();
let ext = img_url_info[0];
let new_img_name = Date.now() + randomString(8) + "." + ext;
request.get(img_url).pipe(fs.createWriteStream(path.join(__dirname, "README", new_img_name))).on("close", function(err){
new_img_url = pre_image_url + new_img_name;
resolve(new_img_url);
});
}catch(e){
console.log(e);
new_img_url = img_url;
resolve(new_img_url);
}
}
// 如果不以http开头,则是本地图片,进行拷贝即可
else {
let img_url_info = img_url.split(".")
img_url_info.reverse();
let ext = img_url_info[0];
let new_img_name = Date.now() + randomString(8) + "." + ext;
fs.createReadStream(img_url).pipe(fs.createWriteStream(path.join(__dirname, "README", new_img_name))).on("close", function(err){
new_img_url = pre_image_url + new_img_name;
resolve(new_img_url);
});
}
})
}
function replace_readme_info ( src_text, dest_text) {
// 获取README.md原始信息
let readme_content = String(fs.readFileSync(path.join(__dirname, "README.md")));
let new_readme_content = readme_content.replace(src_text, dest_text);
fs.writeFileSync(path.join(__dirname, "README.md"), new_readme_content);
}
async function main() {
// 获取仓库的用户名和仓库名
let { username, repositories_name } = await get_github_username_repositories_name();
// console.log(username_repositories_name);
// username = username_repositories_name.username;
// repositories_name = username_repositories_name.repositories_name;
console.log(username, repositories_name);
// 备份README.md为README_BEFORE.md
fs.copyFileSync(path.join(__dirname, "README.md"), path.join(__dirname, "README_BEFORE.md"))
// 拼接README图片前缀
let pre_image_url = "https://raw.githubusercontent.com/" + username + "/" + repositories_name + "/master/README/"
// 获取README.md里面的所有图片地址列表
let img_url_list = get_img_url_list_in_md(path.join(__dirname, "README.md"));
// 如果README文件夹不存在,则创建README文件夹
if ((fs.existsSync(path.join(__dirname, "README"))) === false) {
fs.mkdirSync(path.join(__dirname, "README"))
}
for (let i = 0, img_url_list_length = img_url_list.length; i < img_url_list_length; i++) {
// 如果图片以pre_image_url开头,则跳过
if (img_url_list[i].indexOf(pre_image_url) === -1) {
// 将图片下载到README文件夹,并生成图片github地址
let new_img_url = await download_img_to_readme_dir(img_url_list[i], pre_image_url);
// 替换README.md内图片地址为github地址
replace_readme_info(img_url_list[i], new_img_url);
console.log("将==》",img_url_list[i] ,"替换为==》",new_img_url);
}
}
}
main();