-
Notifications
You must be signed in to change notification settings - Fork 76
/
ygopro-update.js
288 lines (264 loc) · 9.81 KB
/
ygopro-update.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
ygopro-update.js
ygopro update util (not fully implymented)
Author: mercury233
License: MIT
不带参数运行时,会建立一个服务器,调用API执行对应操作
TODO:带参数运行时执行对应操作后退出
*/
var http = require('http');
var https = require('https');
var sqlite3 = require('sqlite3').verbose();
var fs = require('fs');
var execSync = require('child_process').execSync;
var spawn = require('child_process').spawn;
var spawnSync = require('child_process').spawnSync;
var url = require('url');
var moment = require('moment');
moment.locale('zh-cn');
var loadJSON = require('load-json-file').sync;
var auth = require('./ygopro-auth.js');
var constants = loadJSON('./data/constants.json');
var settings = loadJSON('./config/config.json');
config = settings.modules.update_util;
ssl_config = settings.modules.http.ssl;
//全卡名称列表
var cardNames={};
//
var changelog=[];
//http长连接
var responder;
//输出反馈信息,如有http长连接则输出到http,否则输出到控制台
var sendResponse = function(text) {
text=""+text;
if (responder) {
text=text.replace(/\n/g,"<br>");
responder.write("data: " + text + "\n\n");
}
else {
console.log(text);
}
}
//读取数据库内内容到cardNames,异步
var loadDb = function(db_file) {
var db = new sqlite3.Database(db_file);
db.each("select id,name from texts", function (err,result) {
if (err) {
sendResponse(db_file + ":" + err);
return;
}
else {
cardNames[result.id] = result.name;
}
}, function(err, num) {
if(err) {
sendResponse(db_file + ":" + err);
}
else {
sendResponse("已加载数据库"+db_file+",共"+num+"张卡。");
}
});
}
var loadChangelog = function(json_file) {
changelog = loadJSON(json_file).changelog;
sendResponse("已加载更新记录"+json_file+",共"+changelog.length+"条,最后更新于"+changelog[0].date+"。");
}
var makeChangelogs = function(dir, since) {
var lastcommit;
var addedCards=[];
var changedCards=[];
var prc_git_log = spawnSync("git", [ "log", "--pretty=%H,%ai", since ], { "cwd" : dir });
if (prc_git_log.stdout) {
var logs = prc_git_log.stdout.toString().split(/\n/g);
for (var i in logs) {
var log = logs[i].split(",");
var date = log[1];
if (date) {
var prc_git_diff = spawnSync("git", [ "diff-tree", "--no-commit-id", "--name-only" ,"--diff-filter=A" , "-r", log[0] ], { "cwd" : dir });
if (prc_git_diff.stdout) {
var lines = prc_git_diff.stdout.toString().split(/\n/g);
for (var j in lines) {
var line = lines[j].match(/c(\d+)\.lua/);
if (line) {
var name = cardNames[line[1]] || line[1];
addedCards.push(name);
sendResponse("<span class='add'>" + date + " + " + name + "</span>");
}
}
}
}
}
for (var i in logs) {
var log = logs[i].split(",");
var date = log[1];
if (date) {
var prc_git_diff = spawnSync("git", [ "diff-tree", "--no-commit-id", "--name-only" ,"--diff-filter=CMR" , "-r", log[0] ], { "cwd" : dir });
if (prc_git_diff.stdout) {
var lines = prc_git_diff.stdout.toString().split(/\n/g);
for (var j in lines) {
var line = lines[j].match(/c(\d+)\.lua/);
if (line) {
var name = cardNames[line[1]] || line[1];
sendResponse("<span class='change'>" + date + " * " + name + "</span>");
if (!addedCards.includes(name) && !changedCards.includes(name)) {
changedCards.push(name);
}
}
}
}
}
}
var fullLog = [];
fullLog.push("新增卡片:");
for (var i in addedCards) {
fullLog.push("- " + addedCards[i]);
}
if (addedCards.length == 0) {
fullLog.push("- 无");
}
fullLog.push("");
fullLog.push("卡片更改:");
for (var i in changedCards) {
fullLog.push("- " + changedCards[i]);
}
if (changedCards.length == 0) {
fullLog.push("- 无");
}
fullLog.push("\n");
var resJSON = {};
resJSON.type = "changelog";
resJSON.changelog = fullLog;
sendResponse(JSON.stringify(resJSON));
} else {
sendResponse("获取更新记录失败:" + prc_git_log.stderr.toString());
}
}
//从远程更新数据库,异步
var fetchDatas = function() {
var proc = spawn("git", ["pull", "origin", "master"], { cwd: config.git_html_path, env: process.env });
proc.stdout.setEncoding('utf8');
proc.stdout.on('data', function(data) {
sendResponse("git pull: "+data);
});
proc.stderr.setEncoding('utf8');
proc.stderr.on('data', function(data) {
sendResponse("git pull: "+data);
});
proc.on('close', function (code) {
sendResponse("网页同步完成。");
});
}
var updateChangelogs = function(dir, message) {
message = message.split("!换行符!").join("\n");
change_log = {};
change_log.title = "服务器更新";
change_log.date = moment().format("YYYY-MM-DD");
change_log.text = message;
var prc_git_rev = spawnSync("git", [ "rev-parse", "HEAD" ], { "cwd" : dir });
if (prc_git_rev.stdout) {
change_log.commit = prc_git_rev.stdout.toString().split(/\n/)[0];
}
changelog.unshift(change_log);
fileContent = JSON.stringify({ changelog: changelog }, null, 2);
fs.writeFileSync(config.html_path + config.changelog_filename, fileContent);
sendResponse("更新完成,共有" + changelog.length + "条记录。");
}
var pushHTMLs = function() {
try {
execSync('git add ' + config.changelog_filename, { cwd: config.git_html_path, env: process.env });
execSync('git commit -m update-auto', { cwd: config.git_html_path, env: process.env });
} catch (error) {
sendResponse("git error: "+error.stdout);
}
for (var i in config.html_gits) {
var git = config.html_gits[i];
var proc = spawn("git", git.push, { cwd: config.git_html_path, env: process.env });
proc.stdout.setEncoding('utf8');
proc.stdout.on('data', (function(git) {
return function(data) {
sendResponse(git.name + " git push: " + data);
}
})(git));
proc.stderr.setEncoding('utf8');
proc.stderr.on('data', (function(git) {
return function(data) {
sendResponse(git.name + " git push: " + data);
}
})(git));
proc.on('close', (function(git) {
return function(code) {
sendResponse(git.name + "上传完成。");
}
})(git));
}
}
//建立一个http服务器,接收API操作
async function requestListener(req, res) {
var u = url.parse(req.url, true);
if (!await auth.auth(u.query.username, u.query.password, "update_dashboard", "update_dashboard")) {
res.writeHead(403);
res.end("Auth Failed.");
return;
}
if (u.pathname === '/api/msg') {
res.writeHead(200, {
"Access-Control-Allow-origin": "*",
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive"
});
res.on("close", function(){
responder = null;
});
responder = res;
sendResponse("已连接。");
}
else if (u.pathname === '/api/fetch_datas') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始更新网页。"});');
fetchDatas();
}
else if (u.pathname === '/api/load_db') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始加载数据库。"});');
loadDb(config.cdb_path);
loadChangelog(config.html_path + config.changelog_filename);
}
else if (u.pathname === '/api/make_changelog') {
res.writeHead(200);
var date = moment(changelog[0].date).add(1,'days').format("YYYY-MM-DD");
res.end(u.query.callback+'({"message":"开始生成'+ date +'以来的更新记录:"});');
var since = changelog[0].commit ? (changelog[0].commit + "..") : ("--since=" + date);
makeChangelogs(config.script_path, since);
}
else if (u.pathname === '/api/make_more_changelog') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始生成最近20次的更新记录:"});');
makeChangelogs(config.script_path, "-20");
}
else if (u.pathname === '/api/update_changelog') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始写入更新记录。"});');
updateChangelogs(config.script_path, u.query.message);
}
else if (u.pathname === '/api/push_datas') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始上传到网页。"});');
pushHTMLs();
}
else {
res.writeHead(400);
res.end("400");
}
}
if (ssl_config.enabled) {
const ssl_cert = fs.readFileSync(ssl_config.cert);
const ssl_key = fs.readFileSync(ssl_config.key);
const options = {
cert: ssl_cert,
key: ssl_key
}
https.createServer(options, requestListener).listen(config.port);
} else {
http.createServer(requestListener).listen(config.port);
}