UNPKG

fsk-branches

Version:

切换分支工具

374 lines (366 loc) 15.5 kB
#!/usr/bin/env node var fs = require('fs'), os = require("os"), colors = require("colors"), path = require("path"), child_process = require('child_process'); var branchesMac = function(){ this.svnpath = "https://maben@10.10.10.248/svn"; this.configName = "config.json"; this.binpath = __dirname; this.password = this.jsonLoad("passWord"); this.nginxPath = this.jsonLoad("nginxPath"); } branchesMac.prototype.init = function(){} branchesMac.prototype.svnCopyBranches = function(options){ //$ svn cp mianshikaui m="ffff" --h --h5 var that = this, trunk = this.svnpath+"/fe/dev/"+options.port+"/"+options.system+"/"+"trunk", branches = this.svnpath+"/fe/dev/"+options.port+"/"+options.system+"/branches/", name = new Date().toLocaleDateString().replace(/-/g,"")+"_maben_"+options.name; child_process.exec("svn cp -m "+(options.log||"")+" "+trunk+" "+branches+name, function(err, stdout, stderr) { err && console.log(err); that.log(stdout); }); } branchesMac.prototype.svnCheckout = function(num){ var that = this, arr = this.jsonLoad("svnCatalog")[num], branches = this.jsonLoad("svnBranchesPath").replace(/pc/g,arr[0])+"/"+arr[1], localpath = this.jsonLoad("path")+"/"+"dev/"+this.jsonLoad("svnPort")+"/"+arr[0]+"/branches/"+arr[1]; child_process.exec("svn co "+branches+" "+localpath, function(err, stdout, stderr){ err && console.log(err); that.log(stdout) child_process.exec("open "+localpath); }); } branchesMac.prototype.svnList = function(port){ port = port || this.jsonLoad('port'); this.jsonSave('svnPort', port); this.jsonSave('svnBranchesPath', this.svnpath + path.join("/fe/dev/", this.jsonLoad('svnPort'), "/pc/branches")); var that = this, branches = this.jsonLoad('svnBranchesPath'), arr = ['svn'], i = 0; var files = child_process.execSync("svn list "+branches, {encoding: 'utf8'}).match(/[^\/\s\n\r]+/gi) || []; files.forEach(function(v){ arr.push(["pc", v]); that.log("[pc]"+(++i)+"."+v); }); files = child_process.execSync("svn list "+branches.replace(/pc/gi,"h5"), {encoding: 'utf8'}).match(/[^\/\s\n\r]+/gi) || []; files.forEach(function(v){ arr.push(["h5", v]); that.log("[h5]"+(++i)+"."+v); }); this.jsonSave('svnCatalog', arr); } branchesMac.prototype.trunkChange = function(param){ if(param=='trunk'){ this.jsonSave("pc", 'trunk'); this.log("trunk start..."); this.writeNginxConf(); }else if(param=='h5trunk'){ this.jsonSave("h5", 'trunk'); this.log("h5trunk start..."); this.writeNginxConf(); } } branchesMac.prototype.branchesChange = function(param){ var arr = this.jsonLoad("catalog"); if(arr[param]){ this.jsonSave(arr[param][0], arr[param][1]); this.log(arr[param][1]+" start..."); this.writeNginxConf(); }else{ this.error('你选择的序号不存在!'); } } branchesMac.prototype.branchesList = function(port){ try{ var that = this; var dev = this.jsonLoad('path')+"/"+"dev/"; var arr = fs.readdirSync(dev).filter(function(v){ return !/^\./.test(v) }); if(!port || arr.some( function(v){ return v == port })){ if(port){ this.jsonSave("branchesPath", dev+port+"/pc/branches"); this.jsonSave("trunkPath", dev+port+"/pc/trunk"); }else{ this.jsonSave("branchesPath", dev+this.jsonLoad("port")+"/pc/branches"); this.jsonSave("trunkPath", dev+this.jsonLoad("port")+"/pc/trunk"); } var branchesPath = this.jsonLoad("branchesPath"), files = fs.readdirSync(branchesPath), i = 0, arr = ["trunk"]; //pc files.forEach(function(value, index){ if(!/^\./.test(value)){ that.log("[PC]"+(++i)+". "+value); arr.push(["pc",value]); } }); //h5 files = fs.readdirSync(branchesPath.replace(/pc/gi,"h5")); files.forEach(function(value, index){ if(!/^\./.test(value)){ that.log("[H5]"+(++i)+". "+value); arr.push(["h5",value]); } }); this.jsonSave("catalog",arr); }else{ this.error('没找到这个端!请参考'+arr.join(",")); } }catch(e){ if(/directory/i.test(e)){ this.error("获取目录失败!", " 1)确定fe目录是否正确,请通过fsk-branches setup设置!", " 2)请确定(pc和h5)目录下是否有branches" ); }else if(/config\.json/gi.test(e)){ this.error("写入config.json失败,请通过fsk-branches setpwd password设置 把password替换成你的mac密码!"); }else{ this.error(e); } } } branchesMac.prototype.eventCommand = function(port, param){ var that = this; var path = port=="trunk" ? this.jsonLoad("trunkPath") : this.jsonLoad("branchesPath")+"/"+this.jsonLoad("catalog")[port][1]; child_process.exec('sh '+ this.binpath + '/sh/event.sh '+path+' '+(param[1] ? param[1] : ''), function(err, stdout, stderr){ if(err){ if(/exists/gmi.test(err)){ that.error("文件已经存在了!") }else{ console.log(err); } }else{ that.log(stdout); } }) } branchesMac.prototype.command = function(port, param){ var that = this; try{ //使fsk-sync支持q1-q6这样儿的写法 if(/^fsk-sync[\s\-a-z]*([a-z]+\d*[-][a-z]+\d+).*/ig.test(param)){ param = param.replace(/(.*?)([a-z]+\d*[-][a-z]+\d+)(.*)/gi, function(all, $1, $2, $3){ var arr = $2.match(/([a-z]+)(\d*)-[a-z]+(\d+)/i) || [], prefix = arr[1], i = arr[2] ? parseInt(arr[2], 10) : 0, max = parseInt(arr[3], 10), str = ""; for(; i<=max; i++) str += "-h "+prefix+(i?i:'')+" "; return ($1+str+$3).replace(/\s{2}/g, " ").replace(/(-h\s){2}/, "$1"); }); }else if(param=="open"){ param+=" ./" }; param = param.replace(/\s/g,"\\ "); var path; if(port=="trunk"){ path = this.jsonLoad("trunkPath"); }else{ path = this.jsonLoad("branchesPath")+"/"+this.jsonLoad("catalog")[port][1]; } child_process.exec("sh "+this.binpath+"/sh/command.sh "+path+" "+param, function(err, stdout, stderr) { if(err){ that.error( '命令有误!', '举两个命令使用方式的例子:', 'fsk-branches '+port+' open', 'fsk-branches '+port+' fsk-sync -d -h -q1 --pub' ); }else{ console.log(stdout); } }); }catch(e){ console.log(e); this.error('命令有误!'); } } branchesMac.prototype.testConcat = function(data){ if(!/#+\s*fsk-branches\s*#+/gi.test(data)){ var str = [ '####### fsk-branches #######', ' server {', ' listen 443;', ' server_name concat.lietou-static.com iconcat.lietou-static.com i.concat.lietou-static.com;', ' location ~ ^/(WEB-INF|\\.svn|RPC)/ {', ' deny all;', ' }', ' ssl on;', ' ssl_certificate '+this.binpath+'/certificate/lietou-static.com.crt;', ' ssl_certificate_key '+this.binpath+'/certificate/lietou-static.com.key;', ' ssl_ciphers AES128+EECDH:AES128+EDH:!aNULL;', ' ssl_prefer_server_ciphers on;', ' ssl_stapling on;', ' ssl_stapling_verify on;', ' ssl_session_cache shared:SSL:10m;', ' ssl_session_timeout 10m;', ' add_header Strict-Transport-Security "max-age=31536000";', ' add_header Timing-Allow-Origin *;', ' root '+this.jsonLoad("path")+';', ' index index.html index.htm;', ' location / {', ' if ($uri ~ /revs/(.*)$){', ' rewrite "^(.+?)/revs(/.+?)(_[^_]{8})(\\.[\\w]+)$" /$1$2$4 last;', ' } ', ' }', ' rewrite "^/(core/(pc|h5))/(.*)$" /$1/trunk/$3 last;', ' rewrite "^/(dev/passport/(pc))/(.*)$" /$1/trunk/$3 last;', ' rewrite "^/(dev/[^/]+/(pc))/(.*)$" /$1/trunk/$3 last;', ' rewrite "^/(dev/[^/]+/(h5))/(.*)$" /$1/branches/20150807_jinjj_industry/$3 last;', ' rewrite "^/(pics/(pc|h5))/(.*)$" /$1/trunk/$3 last;', ' }', ' server {', ' listen 80;', ' server_name concat.lietou-static.com iconcat.lietou-static.com i.concat.lietou-static.com;', ' root '+this.jsonLoad("path")+';', ' index index.html index.htm;', ' if ($uri ~ /revs/(.*)$){', ' rewrite "^/(core|dev/[^/]+|pics)/(pc|h5)/revs(/.+?)(_[^_]{8})(.*)$" /$1/$2/trunk/$3$5 last;', ' }', ' rewrite "^/(core/(pc|h5))/(.*)$" /$1/trunk/$3 last;', ' rewrite "^/(dev/passport/(pc))/(.*)$" /$1/trunk/$3 last;', ' rewrite "^/(dev/[^/]+/(pc))/(.*)$" /$1/trunk/$3 last;', ' rewrite "^/(dev/[^/]+/(h5))/(.*)$" /$1/trunk/$3 last;', ' rewrite "^/(pics/(pc|h5))/(.*)$" /$1/trunk/$3 last;', ' }', '####### fsk-branches-end #######' ]; fs.writeFileSync(this.nginxPath, data.replace(/([\s\S]*)(\})/gm, function(all, $1, $2){ return $1 + str.join("\n") + "\n" +$2; })); return false; }else{ return true; } }; branchesMac.prototype.writeNginxConf = function(){ var that = this, json = this.jsonLoad(), r1 = function(prot){ return new RegExp("(\\*\\."+prot+"\\.lietou-static\\.com;?[^#]*?rewrite\\s+.*[\\/\\\\](?:pc|h5)[\\/\\\\])(.*)([\\/\\\\]\\$.*)", "gmi"); }; fs.readFile(that.nginxPath, "utf-8", function(err,data){ if(that.testConcat(data)){ var str = data.replace(r1('pc'), "$1"+(json.pc=="trunk" ? "trunk" : "branches\/"+json.pc)+"$3") .replace(r1('h5'), "$1"+(json.h5=="trunk" ? "trunk" : "branches\/"+json.h5)+"$3") .replace(/(^[\s]*rewrite.*\(dev\/\[.*(pc|h5).*\/\$\1[^\/]+?.*$)/gmi, function(all, $1, $2){ return all.replace(/trunk|branches\/[^\/]+/i, json[$2] == "trunk" ? "trunk" : "branches\/"+json[$2]); }); fs.writeFile(that.nginxPath, str, function(err){ child_process.exec("expect "+that.binpath+"/sh/export.sh "+that.password+" nginx", function(err, stdout, stderr) { !err && that.log("nginx finished"); }); }); }else{ that.writeNginxConf(); } }); }; branchesMac.prototype.dir = function(){ fs.readFile(this.nginxPath, "utf-8", function(err,data){ !err && console.log(data); }); }; branchesMac.prototype.edit = function(){ child_process.execSync("open " + this.binpath); }; branchesMac.prototype.nginx = function(){ var that = this; child_process.exec("expect "+this.binpath+"/sh/nginx.sh "+this.password, function(err, stdout, stderr) { !err && that.log("nginx is start"); }); }; branchesMac.prototype.setPassword = function(param){ var that = this; child_process.exec("expect "+that.binpath+"/sh/chomd.sh "+that.binpath+" "+param, function(err, stdout, stderr) { that.jsonSave("passWord",param); }); }; branchesMac.prototype.setup = function(){ var that = this; console.log("*********************** 提示:直接回车代表跳过不修改*********************** "); var arr = [ ["输入你所在的端(h,lpt,c等)", "port"], ["输入fe路径(/Users/maben/work/codeing/fe/)", "path"], ["输入nginx路径(/usr/local/etc/nginx/nginx.conf)", "nginxPath"] ], i = 0; //if(!that.jsonLoad("passWord")) arr.unshift(["输入你的mac登录用户密码", "passWord"]); console.log(arr[i][0]); process.stdin.setEncoding("utf8"); process.stdin.on("data", function(data){ data = data.replace(/\n/g,"").replace(/^[\s]+|[\s]+$/g,""); if(data){ if(arr[i][1]=="path") data = data.replace(/[\/\\]+$/,""); try{ that.jsonSave(arr[i][1],data); }catch(e){ that.error("写入config.json失败,请通过fsk-branches setpwd password设置 把password替换成你的mac密码!"); that.jsonSave("password",""); process.exit(); } } i++; if(i==arr.length){ try{ that.jsonSave("branchesPath", that.jsonLoad("path")+"/"+"dev/"+that.jsonLoad("port")+"/pc/branches"); that.jsonSave("trunkPath", that.jsonLoad("path")+"/"+"dev/"+that.jsonLoad("port")+"/pc/trunk"); that.setPassword(that.jsonLoad("password")); }catch(e){ that.error("写入config.json失败,请通过fsk-branches setpwd password设置 把password替换成你的mac密码!"); that.jsonSave("password",""); } process.exit(); } console.log(arr[i][0]); }); }; branchesMac.prototype.delNginxConcat = function(){ var data = fs.readFileSync(this.nginxPath,'utf8'); data = data.replace('#{6} fsk-branches #{6}[\s\S]+#{6} fsk-branches-end #{6}', ''); console.log(data); //data = data.replace(/(server.*\{.*[\r\n].*[\r\n].*?server_name[\s]+concat\.lietou-static\.com[\s\S]+?\}[\s\r\n]*?)(server|[^\}]*\}[\s\r\n]*$)/gi, function(all, $1, $2){ // console.log($1); // //console.log(/^.*[#]server/m.test($1)); // return /^.*[#]/.test($1) ? $1+$2 : ""+$2; //}); //console.log(data); //mm.forEach(function(v){ // console.log( v.replace('\\n','\n') ); //}); //fs.writeFileSync(this.nginxPath, data); }; branchesMac.prototype.help = function(){ var arr = [ ["$ setpwd pwd ", "获取sudo权限,pwd替换成你的mac密码"], ["$ setup ", "进行相关配置"], ["$ ", "查询本端分支"], ["$ lpt ", "查询企业端,其它端以此类推"], ["$ 1~9 ", "输入数字,切换对应分支"], ["$ nginx ", "启动nginx,重启机器时执行"], ["$ trunk ", "切换到主干"], ["$ h5trunk ", "切换到h5主干"], ["$ dir ", "查询配置文件内容"], ["$ 1~9|trunk|h5trunk event ", "创建活动页目录"], ["$ svn cp 项目名 m=log --h ", "创建分支"] ]; arr.forEach(function(value, index){ console.log( value ); }); }; branchesMac.prototype.jsonLoad = function(name){ return name ? require(this.binpath+"/"+this.configName)[name] : require(this.binpath+"/"+this.configName); }; branchesMac.prototype.jsonSave = function(name, value){ var data = this.jsonLoad(); if(value) data[name]=value; fs.writeFileSync(this.binpath+"/"+this.configName, JSON.stringify(data), 'utf-8'); }; branchesMac.prototype.log = function(){ for(var i in arguments) console.log(arguments[i].cyan); }; branchesMac.prototype.error = function(message){ console.log("=========================================================================".red); for(var i in arguments) console.log((">>>> "+arguments[i]).red); console.log("=========================================================================".red); }; module.exports = new branchesMac();