UNPKG

fsk-branches

Version:

切换分支工具

201 lines (195 loc) 8.11 kB
#!/usr/bin/env node //netstat -ano | findstr "80" var fs = require('fs'), os = require("os"), path = require("path"), child_process = require('child_process'); var branchesWin = function(){ this.configName = "config.json"; this.binpath = path.dirname(__filename); this.password = this.jsonLoad("passWord"); this.nginxPath = this.jsonLoad("nginxPath"); this.nginxConfPath = this.nginxPath+"\\conf\\nginx.conf"; } branchesWin.prototype.init = function(){} branchesWin.prototype.trunkChange = function(param){ if(param=='trunk'){ this.jsonSave("pc", 'trunk'); console.log("trunk start..."); this.writeNginxConf(); }else if(param=='h5trunk'){ this.jsonSave("h5", 'trunk'); console.log("h5trunk start..."); this.writeNginxConf(); } } branchesWin.prototype.error = function(message){ console.log("========================================================================="); for(i in arguments){ console.log(">>>> "+arguments[i]); } console.log("========================================================================="); } branchesWin.prototype.branchesChange = function(param){ var arr = this.jsonLoad("catalog"); if(arr[param]){ this.jsonSave(arr[param][0], arr[param][1]); console.log(arr[param][1]+" start..."); this.writeNginxConf(); }else{ this.error('你选择的序号不存在!'); } } branchesWin.prototype.branchesList = function(port){ try{ 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)){ console.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)){ console.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{ console.log(e); } } } branchesWin.prototype.testConcat = function(data){ if(!/server_name[\s]+concat\.lietou-static\.com/gmi.test(data)){ var str = [ '####### fsk-branches #######', ' 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/[^/]+/(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.nginxConfPath, data.replace(/([\s\S]*)(\})/gm, function(all, $1, $2){ return $1 + str.join("\n") + "\n" +$2; })); return false; }else{ return true; } } branchesWin.prototype.writeNginxConf = function(){ var that = this, json = this.jsonLoad(), r1 = function(prot){ //return new RegExp("(\\*\\."+prot+"\\.lietou-static\\.com;?[^#]*?rewrite\\s+.*\\/(?:pc|h5)\\/)(.*)(\\/\\$.*)", "gmi"); return new RegExp("(\\*\\."+prot+"\\.lietou-static\\.com;?[^#]*?rewrite\\s+.*[\\/\\\\](?:pc|h5)[\\/\\\\])(.*)([\\/\\\\]\\$.*)", "gmi"); }; fs.readFile(that.nginxConfPath, "utf-8", function(err,data){ if(that.testConcat(data)){ 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.nginxConfPath, str, function(err){ var arr = that.nginxPath.match(/(^[a-zA-Z]:)([\s\S]*$)/i); console.log(arr[1], arr[2]); child_process.exec(that.binpath+"\\bat\\nginx.bat "+arr[1]+" \""+arr[2]+"\"", {timeout : 500}, function(err, stdout, stderr) { !err && console.log("nginx finished"); }); }); }else{ that.writeNginxConf(); } }); } branchesWin.prototype.dir = function(){ fs.readFile(this.nginxConfPath, "utf-8", function(err,data){ !err && console.log(data) }); } branchesWin.prototype.edit = function(){ child_process.execSync("open " + this.binpath); } branchesWin.prototype.setup = function(){ var that = this; console.log("*********************** 提示:直接回车代表跳过不修改*********************** "); var arr = [ ["输入你所在的端(h,lpt,c等)", "port"], ["输入fe路径(D:\\work\\codeing\\fe)", "path"], ["输入nginx路径(C:\\Program Files\\nginx-1.7.11)", "nginxPath"] ], i = 0; console.log(arr[i][0]); process.stdin.setEncoding('utf8'); process.stdin.on("data", function(data){ data = data.replace(/[\r\n]|\r\n/g,""); if(data){ if(arr[i][1]=="path") data = data.replace(/[\/\\]+$/,""); that.jsonSave(arr[i][1],data); }; i++; if(i==arr.length){ that.jsonSave("branchesPath", that.jsonLoad("path")+"dev\\"+that.jsonLoad("port")+"\\pc\\branches"); that.jsonSave("trunkPath", that.jsonLoad("path")+"dev\\"+that.jsonLoad("port")+"\\pc\\trunk"); process.exit(); }; console.log(arr[i][0]); }); } branchesWin.prototype.help = function(){ var arr = [ ["fsk-branches setup ", "进行相关配置"], ["fsk-branches ", "查询本端分支"], ["fsk-branches lpt ", "查询企业端,其它端以此类推"], ["fsk-branches 1~9 ", "输入数字,切换对应分支"], ["fsk-branches trunk ", "切换到主干"], ["fsk-branches h5trunk ", "切换到h5主干"], ["fsk-branches dir ", "查询配置文件内容"] ]; arr.forEach(function(value, index){ console.log( value ) }); } branchesWin.prototype.jsonLoad = function(name){ return name ? require(this.binpath+"/"+this.configName)[name] : require(this.binpath+"/"+this.configName); } branchesWin.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'); } module.exports = new branchesWin();