node.js + ejs + python + shell > Autopack service
初衷
* * 需求驱动开发,由于混合开发项目,前端同事需要不停的打包测试,为了避免成为专职打包员,所以考虑做一个自动打包服务,前端同事可以访问我的网站提交代码,然后下载打好的安装包。
目标
- 可以上传代码压缩包
- 可以自动打包
- 可以下载安装包
用到的东西
步骤
安装 node.js
* * 直接 官网 安装,或者我另一篇博客:从零开始搭建一个Node服务。
安装 express,ejs
1 | $ npm i express --save |
初始化项目
1 | $ npm init |
创建网站首页
1 | $ cd views |
node 服务接受代码压缩包
- 用到了中间件 formidable
1
2
3
4
5
6
7
8
9
10
11
12
13
14$ npm i formidable --save
/* 修改index.js 添加一下代码 */
/* upload */
const formidable = require('formidable')
app.post('/upload', function (req, res) {
var form = formidable.IncomingForm()
form.uploadDir = './source/upload'
form.keepExtensions = true
form.parse(req, function(err, fields, files) {
})
})
node 服务解压代码
- 用到了中间件 progress-extract
1
2
3
4
5
6
7
8
9
10
11
12$ npm i progress-extract --save
/* 修改index.js */
const extract = require('progress-extract')
let target = process.cwd()
extract(files.uploadFile.path, target + '/source/upload')
.then(() => {
console.log('extract succeed')
}, err => {
console.log('extract failed')
})
python 脚本替换旧代码
- 用到了 shutil
1
2
3
4
5
6
7$ touch copyFile.py
$ open -sublime copyFile.py
/* copyFile.py */
import shutil
shutil.rmtree('./source/basesource/sgcis/platforms/ios/www') // 删除旧代码
shutil.copytree('./source/upload/www', "./source/basesource/sgcis/platforms/ios/www") // 添加新代码 - 报错权限不足解决:
1
$ chmod a+x copyFile.py
node 调用 python 脚本
- 用到了 child_process
1
2
3
4
5
6
7
8
9
10/* 修改 index.js */
const exec = require('child_process').exec;
exec('python3 ./source/basesource/copyFile.py', function (err, stdout, srderr) {
if(err) {
console.log(srderr);
} else {
console.log(stdout);
}
});
node 调用自动打包 shell 脚本
- 自动打包脚本可以参考我另一篇博客:shell脚本实现自动化打包发布
1
2
3
4
5
6
7
8
9
10
11/* 修改 index.js */
const execFile = require('child_process').execFile;
execFile('./source/basesource/autoPackage.sh',{encoding:'utf8'},function (err,stdout,stderr){
if (err){
console.log(err);
} else {
console.log(stdout)
console.log('autoPackage.sh success')
}
});
node 服务给用户下载安装包
提供下载页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17$ touch download.ejs
/* 填充 download.ejs */
<!DOCTYPE html>
<html>
<head>
<title>下载包</title>
</head>
<body>
<div>
<h1>一分钟后下载</h1>
<form id="download-form" action="http://192.168.2.207:3000/download" method="get">
<input type="submit" value="download" />
</form>
</div>
</body>
</html>响应下载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/* 修改 index.js */
const fs = require("fs")
/* download */
app.get('/download', function (req, res) {
var filePath = './source/package/ipa/sgcis.ipa'
var stats = fs.statSync(filePath);
if (stats.isFile()) {
res.set({
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment; filename=sgcis.ipa',
'Content-Length': stats.size
});
fs.createReadStream(filePath).pipe(res);
} else {
res.end(404);
}
})
总结
- demo地址:APNodeService 。
- python 脚本的功能可以用 shell 脚本实现。
- router 未使用,还有很多地方有待改善。