先看下要实现的效果:
按照
express
官网文档运行出Hello World完成后目录如下:
.
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.pug
├── index.pug
└── layout.pug
7 directories, 9 files拷贝下面内容至
routes/users.js
var express = require('express');
var router = express.Router();
var exec = require('child_process').exec;
const defaultDir='your work dir'
/* GET users listing. */
router.get('/', function(req, res, next) {
updateProject(defaultDir,res)
});
function updateProject(dir,res){
res.write('<html> <meta charset="utf-8"> <body><pre style="word-wrap: break-word; white-space: pre-wrap;"></body></html>');
exec("git pull",{cwd:dir},function(err,stdout,stderr){
var gitResult="git pull: " + stdout;
res.write(gitResult,'utf8');
buildProject(dir,res)
})
}
function buildProject(dir,res){
var exec = require('child_process').exec;
var build = exec("gradle assembleDebug",{cwd:dir});
build.stdout.on('data', function(data) {
console.log(data);
res.write(data,'utf8');
});
build.stdout.on('close', function(data) {
console.log(data);
res.write('</pre>')
res.end();
});
}
module.exports = router;修改
defaultDir
为工作目录,修改exec
逻辑,上面的处理为更新项目之后build浏览器打开
http://localhost:3000/users
即可看到build实时的log
注意要在最后一个命令执行的
close
里面发送res.end()
来结束这一次网络请求