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
| "use strict" const http = require('http'); const url = require('url'); const fs = require('fs'); const readline = require('readline'); const querystring = require('querystring'); const spawnSync = require('child_process').spawnSync;
http.createServer((req, res) => { let urlData = url.parse(req.url); //console.dir(urlData); let queryObj = querystring.parse(urlData.query); console.dir(queryObj); queryObj.title && queryObj.url && writeToMarKdownDoc(queryObj); res.writeHead(200, {"Content-Type": "application/json"}); res.end(JSON.stringify({ code: 0, msg : 'success' })); }).listen(3175);
function changeFilename () { let fileName = 'README.md'; let name = ''; let date = new Date(); if (date.getDate() === 1) { name = data.getFullYear() + '_' + data.getMonth() + '.md' try { let state = fs.state(name); !state.isFile() && fs.renameSync(fileName, name); } catch (e){ fs.renameSync(fileName, name); } } return fileName; }
function isNewFile(filename) { try { let fileState = fs.statSync(filename); let result = fileState.isFile(); return result; } catch(e) { console.error(e); return false; } }
function writeToFile(filename, data, title) { fs.writeFile(filename, data, (err) => { if (err) console.err(err); console.log('saved ' + filename); //pushToGit(title); }); }
function writeToMarKdownDoc(obj) { let fileName = changeFilename(); let data = []; let inserted = false; const date = new Date(); if (!isNewFile(fileName)) { data.push(`### ${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}<br />`) data.push(`+ [${obj.title}](${obj.url})<br />\n`); writeToFile(fileName, data.join('\n'), obj.title); return; } const rl = readline.createInterface({ input: fs.createReadStream(fileName) });
rl.on('line', (line) => { if (!inserted && line.startsWith('###')) {
let data_str = line.match(/\d{4}-\d{1,2}-\d{1,2}/)[0]; if (date_str && (new Date(data_str).getDate() === date.getDate())) { data.push(line); data.push(`+ [${obj.title}](${obj.url})<br />`); } else { data.push(`### ${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}<br />`) data.push(`+ [${obj.title}](${obj.url})<br />\n`); data.push(line); } inserted = true; } else { data.push(line); } }); rl.on('close', () => { if (!inserted) { //新建的文件 data.push(`### ${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}<br />`) data.push(`+ [${obj.title}](${obj.url})<br />\n`); } writeToFile(fileName, data.join('\n'), obj.title); }); }
function pushToGit(title) { var title = title || ''; var cmds = [ ['git', ['add', '.']], ['git', ['commit', '-am', `"add ${title}"`]], ['git', ['pull', '--rebase']], ['git', ['push', 'origin', 'master']] ]; cmds.forEach((cmd) => { let result = spawnSync(cmd[0], cmd[1]); console.log(result.output.join('\n')); }); }
setInterval(function(){ console.log('push to github'); pushToGit(new Date().toString()); }, 1000 * 60 * 60 * 2);
console.log('server start at 3175');
|