Files
2018-11-17 12:36:31 +08:00

69 lines
1.5 KiB
JavaScript

/**
*
* @authors Tony.Deng (dengtongyu123@163.com)
* @date 2018-05-17 10:07:16
* @version $Id$
*/
const run = require('exec-cmd');
const path = require('path');
const build = require('./build');
const filesUpdate = require('./updatefile');
//const gitPath = '/Users/dengtongyu/code/webpage/';
const gitPath = '/var/local/githook/webpage/';
exports.use = (app, log4js) => {
const logger = log4js.getLogger(path.parse(__filename).name);
app.post('/webpage', (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;
})
.catch((err) => {
logger.error(err);
});
let buildObj = null;
if(cmd && cmd[1] == 0) {
buildObj = await build.use(data, log4js);
logger.info(buildObj);
str += buildObj.str;
}
if(JSON.stringify(buildObj.data) != '{}') {
updateString = await filesUpdate.use(buildObj.data, log4js);
str += updateString;
}
} else {
str += '<p>post数据缺失!</p>';
}
return str;
}
updateCode().then((html) => {
res.writeHead(200, {
"Content-Type": "text/html;charset=utf-8"
});
res.end(html);
logger.info(JSON.stringify(data));
})
});
}