/** * * @authors Tony.Deng (dengtongyu123@163.com) * @date 2018-05-03 17:11:02 * @version 0.0.1 */ const run = require('exec-cmd'); const path = require('path'); const blogUpdate = require('./updatefile'); const gitPath = '/var/local/githook/blog/'; exports.use = (app, log4js) => { const logger = log4js.getLogger(path.parse(__filename).name); app.post('/blog', (req, res) => { let data = req.body; if(data.ref != 'refs/heads/master') return; let updateCode = async () => { let str = ''; if (data && data.commits && data.commits.length > 0) { let cmd = await run('git pull origin master', { cwd: gitPath }) .then((arr) => { let res = arr[0]; let code = arr[1]; let buffer = arr[2]; logger.info(res, code); return arr; //return run(targetPath + ' git remote show', {stdio: 'inherit'}) }) .catch((err) => { logger.error(err); }); if(cmd && cmd[1] == 0) { let updateString = await blogUpdate.use(data, log4js); str += updateString; } } else { str += '
post数据缺失!
'; } return str; } updateCode().then((html) => { res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" }); res.end(html); logger.info(JSON.stringify(data)); }) }); }