Authored by 沈志敏

代码检查

... ... @@ -6,21 +6,33 @@
* @date 2016/10/12
*/
'use strict';
const fs = require('fs');
const sh = require('shelljs');
const moment = require('moment');
const path = require('path');
const config = require('../../config/config');
const mail = require('../models/mail');
class Collect {
constructor(projectname, gitlab, branch) {
this.projectname = projectname;
this.gitlab = gitlab;
this.branch = branch;
this.buildTime = moment().format('YYYYMMDDHHmmss');
this.mailOptions = {
from: 'automan@yoho.cn',
to: 'kai.bi@yoho.cn,zhimin.shen@yoho.cn',
subject: `项目:${projectname} 代码检查一览`
};
}
get buildPath() {
return path.join(config.buildDir, this.projectname, this.buildTime, this.projectname);
}
get rootPath() {
let buildTime = moment().format('YYYYMMDDHHmmss')
return path.join(config.buildDir, this.projectname, buildTime);
return path.join(config.buildDir, this.projectname, this.buildTime);
}
/**
* do some folder check
... ... @@ -41,7 +53,7 @@ class Collect {
_cloneCode(branch) {
var self = this;
this._log('cloning_code');
let clone_script = `git clone --depth 1 -b ${branch} ${this.gitlab}`;
let clone_script = `git clone --depth 1 -b ${branch} ${this.gitlab} `;
this._log(`>>>>>>>>> ${clone_script} >>>>>>>>>>>`);
return new Promise((reslove, reject) => {
... ... @@ -75,7 +87,7 @@ class Collect {
var self = this;
this._log('>>>>>>>> install dependencies >>>>>>>');
return new Promise((resolve, reject) => {
sh.cd(self.rootPath);
sh.cd(self.buildPath);
var child = sh.exec('npm i --production=false', {
silent: self.silent,
... ... @@ -104,16 +116,32 @@ class Collect {
var self = this;
this._log(`>>>>>>>>> check code >>>>>>>>>>>`);
return new Promise((reslove, reject) => {
sh.cd(self.rootPath);
sh.cd(self.buildPath);
var child = sh.exec('npm run build -s', {
var child = sh.exec('npm run lint-all', {
silent: self.silent,
async: true
});
child.stdout.pipe(fs.createWriteStream(self.logFile, {
flags: 'a'
}));
var dataText = '';
child.stdout.on('data', (chunk) => {
dataText += chunk.toString();
});
child.stdout.on("end", function () {
self.mailOptions.text = dataText;
mail.sendMail(self.mailOptions, function(err, info){
if(err){
self._log(error);
} else {
self._log('mail sent: ' + info.response);
}
})
});
// child.stdout.pipe(fs.createWriteStream(self.logFile, {
// flags: 'a'
// }));
child.stderr.pipe(fs.createWriteStream(self.logFile, {
flags: 'a'
}));
... ... @@ -122,7 +150,7 @@ class Collect {
if (code == 0) {
reslove();
} else {
reject(new Error(`${self.project.name} build code fail`));
reject(new Error(`${self.projectname} build code fail`));
}
});
});
... ...
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'QQex',
auth: {
user: 'automan@yoho.cn',
pass: 'Yoho@9646'
}
});
exports.sendMail = function(data, cb){
transporter.sendMail(data, cb);
}
... ...
... ... @@ -11,11 +11,27 @@ module.exports = {
async check() {
let rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(1, 6)];  
rule.hour = 23;
rule.minute = 59;  
schedule.scheduleJob(rule, function() {
console.log("执行任务");
// rule.dayOfWeek = [0, new schedule.Range(1, 6)];  
// rule.hour = 23;
// rule.minute = 59;  
// schedule.scheduleJob(rule, function() {
// console.log("执行任务");
// let projects = await Project.findAll();
// if (!projects || !projects.length) {
// return;
// }
// projects.forEach(async(p) => {
// if( p.name !== 'yohobuywap-node') {
// return;
// }
// //new Check(p.gitlab, 'master').check();
// new Check(p.gitlab, 'feature/checkcode').check();
// });
// });
console.log("执行任务");
let projects = await Project.findAll();
if (!projects || !projects.length) {
... ... @@ -23,12 +39,12 @@ module.exports = {
}
projects.forEach(async(p) => {
if( p.name !== 'yohobuywap-node') {
if(p.name !== 'yohobuywap-node') {
return;
}
new Check(p.gitlab, 'master').check();
//new Check(p.name, p.gitlab, 'master').check();
new Check(p.name, p.gitlab, 'feature/checkcode').check();
});
});
}
};
\ No newline at end of file
... ...
... ... @@ -52,6 +52,7 @@
"node-schedule": "^1.2.1",
"node-xlsx": "^0.7.4",
"node-zookeeper-client": "^0.2.2",
"nodemailer": "^3.1.8",
"npm": "^3.10.9",
"qn": "^1.3.0",
"qs": "^6.2.0",
... ...