美文网首页
node.js + ejs + python + shell >

node.js + ejs + python + shell >

作者: __Gavin__ | 来源:发表于2018-06-04 01:02 被阅读0次

初衷

   需求驱动开发,由于混合开发项目,前端同事需要不停的打包测试,为了避免成为专职打包员,所以考虑做一个自动打包服务,前端同事可以访问我的网站提交代码,然后下载打好的安装包。

目标

  • 可以上传代码压缩包
  • 可以自动打包
  • 可以下载安装包

用到的东西

步骤

安装 node.js

   直接 官网 安装,或者我另一篇博客:从零开始搭建一个Node服务

安装 express,ejs

$ npm i express --save
$ npm i ejs --save

初始化项目

$ npm init
$ touch index.js
$ mkdir views   // ejs 模版

/* 填充 index.js */
const express = require('express')
const path = require('path')
const app = express()

app.set('views', path.join(__dirname, 'views'))  // 设置存放模板文件的目录
app.set('view engine', 'ejs')  // 设置模板引擎为 ejs

app.get('/', function (req, res) {
    res.send('hello, express')
})
app.listen(3000)

创建网站首页

$ cd views
$ touch home.ejs

/* 填充 home.ejs */
<!DOCTYPE html>
<html>
<head>
    <title>打个包</title>
</head>
<body>
    <form id="upload-form" action="http://192.168.xx.xx:3000/upload" method="post" enctype="multipart/form-data" >
    <input type="file" id="upload" name="uploadFile" /> <br />
    <input type="submit" value="Upload" />
    </form>
</body>
</html>

/* 更改 index.js */
app.get('/', function (req, res) {
    res.render('home')
})

node 服务接受代码压缩包

$ 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 服务解压代码

$ 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 脚本替换旧代码

$ 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")  // 添加新代码
  • 报错权限不足解决:
$ chmod a+x copyFile.py

node 调用 python 脚本

/* 修改 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 脚本

/* 修改 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 服务给用户下载安装包

  • 提供下载页面
$ 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>
  • 响应下载
/* 修改 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 未使用,还有很多地方可以改善。

相关文章

网友评论

      本文标题:node.js + ejs + python + shell >

      本文链接:https://www.haomeiwen.com/subject/dthdsftx.html