env-test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const Path=require("path"),Fs=require("fs"),FsExtra=require("fs-extra"),Express=require("express"),Iokit=Editor.require("packages://hot-update-tools/core/iokit.js"),CfgUtil=Editor.require("packages://hot-update-tools/core/CfgUtil.js"),Util=Editor.require("packages://hot-update-tools/core/Util.js"),OutPut=Editor.require("packages://hot-update-tools/core/OutPut.js"),GoogleAnalytics=Editor.require("packages://hot-update-tools/core/GoogleAnalytics.js");
  2. Vue.component("env-test",{
  3. template:Fs.readFileSync(Editor.url("packages://hot-update-tools/panel/env-test.html"),"utf-8"),
  4. mixins:[Editor.require("packages://hot-update-tools/panel/mixin.js")],
  5. data:()=>(
  6. {serverApp:null,serverUrl:null,localServerPath:"",serverVersion:null,serverPackageUrl:null,testEnvALi:!1,testEnvEmail:!1,testEnvSelect:0,emailContent:"邮件内容!",addMailPeople:"",emailPeopleArray:["xu_yanfeng@126.com"]}
  7. ),
  8. created(){
  9. this.$nextTick(()=>{
  10. let e=CfgUtil.cfgData;
  11. e&&(this.localServerPath=e.localServerPath),this._initLocalServerDir(),this._updateServerVersion()
  12. })
  13. },
  14. methods:{
  15. _getAvailableNetPort(e,t){
  16. let r=require("net").createServer().listen(e);
  17. r.on("listening",()=>{r.once("close",()=>{t&&t(e)}),r.close()}),r.on("error",r=>{this._getAvailableNetPort(e+1,t)})
  18. },
  19. onBtnClickOpenStaticFileServer(){
  20. let e=this.localServerPath;
  21. e&&Fs.existsSync(e)&&this._getAvailableNetPort(5520,t=>{
  22. let r=Express();
  23. r.use(Express.static(e)),r.listen(t),this.serverApp=r;let i=Util.getLocalIP();
  24. this.serverUrl=`http://${i}:${t}/`,this.log(`Http文件服务开启: ${e}`)
  25. })
  26. },
  27. onCopyFileToLocalServer(){
  28. GoogleAnalytics.eventCustom("copyFileToLocalServer");
  29. let{localServerPath:e}=this,{manifestResDir:t}=Util,{selectDestType:c,resourceRootDir:r}=CfgUtil.cfgData,{manifestDir:i,reducedDir:u,versionsDir:v}=OutPut;
  30. if(!Fs.existsSync(e))return void
  31. this.log("本地测试服务器目录不存在:"+e);
  32. let s=Path.join(r,"src"),l=Path.join(r,t);
  33. if(!Fs.existsSync(r))return void
  34. this.log("资源目录不存在: "+r+", 请先构建项目");
  35. if(!Fs.existsSync(s))return void
  36. this.log(r+"不存在src目录, 无法拷贝文件");
  37. if(!Fs.existsSync(l))return void
  38. this.log(r+"不存在res目录, 无法拷贝文件");
  39. let o=Path.join(i,"project.manifest"),a=Path.join(i,"version.manifest");
  40. !i||i.length<=0?this.log("manifest文件生成地址未填写"):Fs.existsSync(o)?Fs.existsSync(a)?(FsExtra.emptyDirSync(e),FsExtra.copySync(s,Path.join(e,"src")),FsExtra.copySync(l,Path.join(e,t)),FsExtra.copyFileSync(o,Path.join(e,"project.manifest")),FsExtra.copyFileSync(a,Path.join(e,"version.manifest")),this.log(`已经将热更包copy到: ${e}`),this._updateServerVersion()):this.log(a+"不存在, 请点击生成热更包"):this.log(o+"不存在, 请点击生成热更包")
  41. // this.log(o);
  42. // this.log(a);
  43. // this.log(e);
  44. let b=Path.join(e,"project.manifest"),n=Path.join(v,c+"/UnZip"),k=Path.join(n,"project.manifest");
  45. this.copyReduceRes(k,b,e,u, ()=>{
  46. this._copyFiles(n);
  47. });
  48. },
  49. copyReduceRes(e,d,c,f,callback){
  50. Iokit.build(e,d,c,f,
  51. function (data, bSucc) {
  52. if (data) {
  53. this.log("Iokit:" + data);
  54. }
  55. if(bSucc) {
  56. if(callback) {
  57. callback();
  58. }
  59. }
  60. }.bind(this)
  61. );
  62. },
  63. _copyFiles(f) {
  64. let{manifestResDir:t}=Util,{resourceRootDir:r}=CfgUtil.cfgData,{manifestDir:i}=OutPut;
  65. let s=Path.join(r,"src"),l=Path.join(r,t);
  66. if(!Fs.existsSync(r))return void
  67. this.log("资源目录不存在: "+r+", 请先构建项目");
  68. if(!Fs.existsSync(s))return void
  69. this.log(r+"不存在src目录, 无法拷贝文件");
  70. if(!Fs.existsSync(l))return void
  71. this.log(r+"不存在res目录, 无法拷贝文件");
  72. let o=Path.join(i,"project.manifest"),a=Path.join(i,"version.manifest");
  73. if(Fs.existsSync(o)){
  74. if(Fs.existsSync(a)){
  75. FsExtra.emptyDirSync(f);
  76. FsExtra.copySync(s,Path.join(f,"src"))
  77. FsExtra.copySync(l,Path.join(f,t))
  78. FsExtra.copyFileSync(o,Path.join(f,"project.manifest"))
  79. FsExtra.copyFileSync(a,Path.join(f,"version.manifest"))
  80. this.log(`已经将版本资源copy到: ${f}`)
  81. }else{
  82. this.log(a+"不存在, 请点击生成热更包")
  83. }
  84. }else{
  85. this.log(o+"不存在, 请点击生成热更包")
  86. }
  87. },
  88. onTestEnvChange(e){
  89. let t=e.target.value;
  90. this.testEnvALi=!1,this.testEnvEmail=!1,"0"===t||("1"===t?this.testEnvALi=!0:"2"===t&&(this.testEnvEmail=!0))
  91. },
  92. onCleanSimRemoteRes(){
  93. let e=process.platform,t=null;
  94. if("win32"===e){
  95. let e=Path.join(Editor.appPath,"../cocos2d-x/simulator/");
  96. t=Path.join(e,"win32/remote-asset")
  97. }else if("darwin"===e){
  98. let e=Path.join(Editor.appPath,"../cocos2d-x/simulator/");
  99. t=Path.join(e,"mac/Simulator.app/Contents/Resources/remote-asset")
  100. }
  101. t&&(Fs.existsSync(t)?(FsExtra.emptyDirSync(t),this.log("[清理热更缓存] 清空目录 "+t+" 成功.")):this.log("[清理热更缓存] 目录不存在: "+t))
  102. },
  103. onOpenLocalServer(){
  104. this.openDir(this.localServerPath)
  105. },
  106. onSelectLocalServerPath(e){
  107. let t=Editor.projectInfo.path;
  108. this.localServerPath&&this.localServerPath.length>0&&Fs.existsSync(this.localServerPath)&&(t=this.localServerPath);
  109. let r=Editor.Dialog.openFile({title:"选择本地测试服务器目录",defaultPath:t,properties:["openDirectory"]});
  110. -1!==r&&(this.localServerPath=r[0],this._saveConfig(),this._updateServerVersion())
  111. },
  112. refreshLocalServerVersion(){
  113. this._updateServerVersion()
  114. },
  115. _initLocalServerDir(){
  116. this.localServerPath&&this.localServerPath.length>0||(this.localServerPath=OutPut.testServerDir)
  117. },
  118. _updateServerVersion(){
  119. if(this.localServerPath.length>0){
  120. let e={"version.manifest":null,"project.manifest":null};
  121. for(let t in e){
  122. let r=Path.join(this.localServerPath,t);
  123. Fs.existsSync(r)?e[t]=JSON.parse(Fs.readFileSync(r,"utf-8")):this.log(`测试环境:未发现文件${t}`)
  124. }
  125. let t=e["version.manifest"],r=e["project.manifest"];
  126. t&&r?t.version===r.version&&t.packageUrl===r.packageUrl&&(this.serverVersion=r.version,this.serverPackageUrl=r.packageUrl):t&&!r?(this.serverVersion=t.version,this.serverPackageUrl=t.packageUrl):!t&&r?(this.serverVersion=r.version,this.serverPackageUrl=r.packageUrl):t||r||this.log("测试环境: 无法获取到本地测试服务器版本号")
  127. }else
  128. this.log("请选择本机server物理路径")
  129. },
  130. onBtnClickSendMail(){
  131. Mail.sendMail(this.remoteServerVersion,this.emailContent,null,()=>{this.log("发送邮件完毕!")})
  132. },
  133. onInputMailPeopleOver(){
  134. !1===this.isPeopleExist()&&this.emailPeopleArray.push(this.addMailPeople)
  135. },
  136. isPeopleExist(){
  137. if(null===this.addMailPeople||""===this.addMailPeople)return!1;
  138. for(let e=0;e<this.emailPeopleArray.length;e++){
  139. if(this.emailPeopleArray[e]===this.addMailPeople)return!0
  140. }
  141. return!1
  142. }
  143. }
  144. });