Commit de0fb898 authored by mercury233's avatar mercury233

update the update util

parent 775e712a
......@@ -207,6 +207,8 @@
"password": "123456",
"git_html_path": "../ygo233-web/",
"html_path": "../ygo233-web/",
"cdb_path": "./ygopro/cards.cdb",
"script_path": "./ygopro/script",
"changelog_filename": "changelog.json",
"html_gits": [
{
......
......@@ -63,8 +63,9 @@ var loadDb = function(db_file) {
}
});
}
var loadChangelog = function(json_file) {
changelog = JSON.parse(fs.readFileSync(json_file, 'utf8')).changelog;
changelog = loadJSON(json_file).changelog;
sendResponse("已加载更新记录"+json_file+",共"+changelog.length+"条,最后更新于"+changelog[0].date+"");
}
......@@ -114,21 +115,28 @@ var makeChangelogs = function(dir, since) {
}
}
sendResponse("新增卡片:");
var fullLog = [];
fullLog.push("新增卡片:");
for (var i in addedCards) {
sendResponse("-   " + addedCards[i]);
fullLog.push("- " + addedCards[i]);
}
if (addedCards.length == 0) {
sendResponse("-   ");
fullLog.push("- ");
}
sendResponse("\n");
sendResponse("卡片更改:");
fullLog.push("");
fullLog.push("卡片更改:");
for (var i in changedCards) {
sendResponse("-   " + changedCards[i]);
fullLog.push("- " + changedCards[i]);
}
if (changedCards.length == 0) {
sendResponse("-   ");
fullLog.push("- ");
}
fullLog.push("\n");
var resJSON = {};
resJSON.type = "changelog";
resJSON.changelog = fullLog;
sendResponse(JSON.stringify(resJSON));
} else {
sendResponse("获取更新记录失败:" + prc_git_log.stderr.toString());
}
......@@ -136,7 +144,7 @@ var makeChangelogs = function(dir, since) {
//从远程更新数据库,异步
var fetchDatas = function() {
var proc = spawn("git", ["pull", "origin", "master"], { cwd: config.git_db_path, env: process.env });
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);
......@@ -146,22 +154,52 @@ var fetchDatas = function() {
sendResponse("git pull: "+data);
});
proc.on('close', function (code) {
sendResponse("数据更新完成。");
});
var proc2 = spawn("git", ["pull", "origin", "master"], { cwd: config.git_html_path, env: process.env });
proc2.stdout.setEncoding('utf8');
proc2.stdout.on('data', function(data) {
sendResponse("git pull: "+data);
});
proc2.stderr.setEncoding('utf8');
proc2.stderr.on('data', function(data) {
sendResponse("git pull: "+data);
});
proc2.on('close', function (code) {
sendResponse("网页同步完成。");
});
}
var updateChangelogs = function(message) {
message = message.split("!换行符!").join("\n");
change_log = {};
change_log.title = "服务器更新";
change_log.date = moment().format("YYYY-MM-DD");
change_log.text = message;
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操作
http.createServer(function (req, res) {
......@@ -189,32 +227,37 @@ http.createServer(function (req, 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("./ygopro/cards.cdb");
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 +'以来的更新记录:"});');
makeChangelogs("./ygopro/script", "--since="+date);
makeChangelogs(config.script_path, "--since="+date);
}
else if (u.pathname === '/api/make_more_changelog') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始生成最近20次的更新记录:"});');
makeChangelogs("./ygopro/script", "-20");
makeChangelogs(config.script_path, "-20");
}
else if (u.pathname === '/api/push_datas') {
else if (u.pathname === '/api/update_changelog') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始上传到网页。"});');
pushDatas();
res.end(u.query.callback+'({"message":"开始写入更新记录。"});');
updateChangelogs(u.query.message);
}
else if (u.pathname === '/api/write_to_file') {
else if (u.pathname === '/api/push_datas') {
res.writeHead(200);
res.end(u.query.callback+'({"message":"开始写列表页。"});');
writeToFile(u.query.message);
res.end(u.query.callback+'({"message":"开始上传到网页。"});');
pushHTMLs();
}
else {
res.writeHead(400);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment