初始化项目

This commit is contained in:
dengtongyu committed 2018-11-17 12:36:31 +08:00
commit d876ae9948
15 files changed
+2922

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
/**
*
* @authors Tony.Deng (dengtongyu123@163.com)
* @date 2018-05-17 11:18:41
* @version $Id$
*/
const run = require('exec-cmd');
const path = require('path');
//const cmdPath = '/Users/dengtongyu/code/webpage/';
const cmdPath = '/var/local/githook/webpage/';
const rootPath = 'src/pages';
exports.use = async (data, log4js) => {
const logger = log4js.getLogger(path.parse(__filename).name);
let buildPath = {};
for (let i = 0; i < data.commits.length; i++) {
let add = data.commits[i].added;
let rm = data.commits[i].removed;
let mdf = data.commits[i].modified;
if (add.length > 0) {
for (let index = 0; index < add.length; index++) {
let name = path.relative(rootPath, add[index]);
logger.info(name);
let re = /^\./
let result = re.test(name);
logger.info(result);
if (!result) {
name = name.split('/')[0];
buildPath[name] = true;
}
}
}
if (mdf.length > 0) {
for (let index = 0; index < mdf.length; index++) {
let name = path.relative(rootPath, mdf[index]);
logger.info(name);
let re = /^\./
let result = re.test(name);
logger.info(result);
if (!result) {
name = name.split('/')[0];
buildPath[name] = true;
}
}
}
}
let str = '';
if (JSON.stringify(buildPath) == '{}') {
str = '<p>本次提交没有pages更新</p>'
} else {
for (let key in buildPath) {
let cmd = await run('node app pack -n ' + key, {
cwd: cmdPath
})
.then((arr) => {
let res = arr[0];
let code = arr[1];
let buffer = arr[2];
logger.info(res, code);
str += '<p>' + key + '构建完成!</p>';
})
.catch((err) => {
logger.error(err);
str += '<p>' + key + '构建失败!</p>';
});
}
}
return {str:str,data:buildPath};
}
+34
View File
@@ -0,0 +1,34 @@
/**
*
* @authors Tony.Deng (dengtongyu123@163.com)
* @date 2018-05-17 10:42:03
* @version $Id$
*/
const path = require('path');
const fs = require('fs-extra');
const srcPath = '/var/local/githook/webpage/build/pages/';
const targetPath = '/data/wwwroot/node/pages/';
exports.use = async (data, log4js) => {
const logger = log4js.getLogger(path.parse(__filename).name);
let str = '';
for (let key in data) {
let target = targetPath + key;
let src = srcPath + key;
let flie = await fs.copy(src, target)
.then(() => {
str += '<p>部署:' + key + '完成!</p>';
})
.catch((err) => {
logger.error(err);
str += '<p>部署:' + key + '出错!</p>';
});
}
return str;
}
+69
View File
@@ -0,0 +1,69 @@
/**
*
* @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));
})
});
}