index.js 1.22 KB
var express = require('express');
var router = express.Router();
var exec = require('child_process').exec;

var projects = {};

router.post('/:name', function(req, res, next) {
   proc(req,res);
});


/**
 * 处理刷新和重启服务
 * @param  {String} name 服务目录
 * @param  {Object} req  请求
 * @param  {Object} res  响应
 */
function proc(req,res) {
   var cliArr = [
    "cd /Data/code/test/"+req.params.name+"/client/",
    "git pull",
    "gulp",
    "cd /Data/code/test/"+req.params.name+"/",
    "make start"
   ];

  console.log(req.body);

  if(req.body.ref.indexOf('release')<0) {
     res.json({"code":200,"message":"not release,do nothing!"});
     return;
  }
  if(projects[req.params.name].hash === req.body.after) {
     res.json({"code":200,"message":"same commits , do nothing!"});
     return;
  }
  
  //设置当前项目的commits的hash
  projects[req.params.name].hash = req.body.after;

  //执行命令
  exec(cliArr.join(' && '),function(err,out,outerr) {
    if(err) {
        console.log(err);
        console.log(outerr);
        res.json({"code":500,"message": err.message});
    } else {
        console.log(out);
        res.json({"code":200,"message":"done!"});
    }
  });  
}

module.exports = router;