env-test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. const Path = require('path');
  2. const Fs = require('fs');
  3. const FsExtra = require('fs-extra');
  4. const Express = require('express');
  5. const CfgUtil = Editor.require('packages://hot-update-tools/core/CfgUtil.js');
  6. const Util = Editor.require('packages://hot-update-tools/core/Util.js');
  7. const OutPut = Editor.require('packages://hot-update-tools/core/OutPut.js');
  8. const GoogleAnalytics = Editor.require('packages://hot-update-tools/core/GoogleAnalytics.js');
  9. const { shell } = require('electron');
  10. Vue.component('env-test', {
  11. template: Fs.readFileSync(Editor.url('packages://hot-update-tools/panel/env-test.html'), 'utf-8'),
  12. mixins: [Editor.require('packages://hot-update-tools/panel/mixin.js')],
  13. data () {
  14. return {
  15. serverApp: null,
  16. serverUrl: null,
  17. localServerPath: '',
  18. serverVersion: null,
  19. serverPackageUrl: null,
  20. // 测试环境逻辑变量
  21. testEnvALi: false,
  22. testEnvEmail: false, // 发送邮件界面
  23. testEnvSelect: 0,
  24. // 邮件逻辑变量
  25. emailContent: '邮件内容!',
  26. addMailPeople: '',
  27. emailPeopleArray: [
  28. 'xu_yanfeng@126.com',
  29. ],
  30. };
  31. },
  32. created () {
  33. this.$nextTick(() => {
  34. let data = CfgUtil.cfgData;
  35. if (data) {
  36. this.localServerPath = data.localServerPath;
  37. }
  38. this._initLocalServerDir();
  39. this._updateServerVersion();
  40. });
  41. },
  42. methods: {
  43. // 检测port是否被占用
  44. _getAvailableNetPort (port, callback) {
  45. console.log('检测端口是否被占用: ' + port);
  46. let net = require('net');
  47. // tcp使用端口0表示系统分配端口
  48. let server = net.createServer().listen(port);
  49. server.on('listening', () => {
  50. server.once('close', () => {
  51. callback && callback(port);
  52. });
  53. server.close();
  54. });
  55. server.on('error', (err) => {
  56. this._getAvailableNetPort(port + 1, callback);
  57. });
  58. },
  59. onBtnClickOpenStaticFileServer () {
  60. let staticDir = this.localServerPath;
  61. if (staticDir && Fs.existsSync(staticDir)) {
  62. this._getAvailableNetPort(5520, (serverPort) => {
  63. let app = Express();
  64. app.use(Express.static(staticDir));
  65. app.listen(serverPort);
  66. this.serverApp = app;
  67. let ip = Util.getLocalIP();
  68. this.serverUrl = `http://${ip}:${serverPort}/`;
  69. this.log(`Http文件服务开启: ${staticDir}`);
  70. });
  71. }
  72. },
  73. onCopyFileToLocalServer () {
  74. GoogleAnalytics.eventCustom('copyFileToLocalServer');
  75. let { localServerPath } = this;
  76. let { manifestResDir } = Util;
  77. let { resourceRootDir } = CfgUtil.cfgData;
  78. let { manifestDir } = OutPut;
  79. if (!Fs.existsSync(localServerPath)) {
  80. this.log('本地测试服务器目录不存在:' + localServerPath);
  81. return;
  82. }
  83. // 检查资源目录
  84. let srcPath = Path.join(resourceRootDir, 'src');
  85. let resPath = Path.join(resourceRootDir, manifestResDir);
  86. if (!Fs.existsSync(resourceRootDir)) {
  87. this.log('资源目录不存在: ' + resourceRootDir + ', 请先构建项目');
  88. return;
  89. }
  90. if (!Fs.existsSync(srcPath)) {
  91. this.log(resourceRootDir + '不存在src目录, 无法拷贝文件');
  92. return;
  93. }
  94. if (!Fs.existsSync(resPath)) {
  95. this.log(resourceRootDir + '不存在res目录, 无法拷贝文件');
  96. return;
  97. }
  98. // 检查manifest文件
  99. let projectTag = 'project.manifest';
  100. let versionTag = 'version.manifest';
  101. let project = Path.join(manifestDir, projectTag);
  102. let version = Path.join(manifestDir, versionTag);
  103. if (!manifestDir || manifestDir.length <= 0) {
  104. this.log('manifest文件生成地址未填写');
  105. return;
  106. }
  107. if (!Fs.existsSync(project)) {
  108. this.log(project + '不存在, 请点击生成热更包');
  109. return;
  110. }
  111. if (!Fs.existsSync(version)) {
  112. this.log(version + '不存在, 请点击生成热更包');
  113. return;
  114. }
  115. FsExtra.emptyDirSync(localServerPath);
  116. FsExtra.copySync(srcPath, Path.join(localServerPath, 'src'));
  117. FsExtra.copySync(resPath, Path.join(localServerPath, manifestResDir));
  118. FsExtra.copyFileSync(project, Path.join(localServerPath, projectTag));
  119. FsExtra.copyFileSync(version, Path.join(localServerPath, versionTag));
  120. this.log(`已经将热更包copy到: ${localServerPath}`);
  121. this._updateServerVersion();
  122. },
  123. onCopyFileToReduceDir() {
  124. GoogleAnalytics.eventCustom('onCopyFileToReduceDir');
  125. let { manifestResDir } = Util;
  126. let { manifestDir,reduceDir,versionsDir } = OutPut;
  127. let { resourceRootDir,serverRootDir } = CfgUtil.cfgData;
  128. // 检查资源目录
  129. let srcPath = Path.join(resourceRootDir, 'src');
  130. let resPath = Path.join(resourceRootDir, manifestResDir);
  131. if(!Fs.existsSync(resourceRootDir)) {
  132. this.log('资源目录不存在:'+resourceRootDir+',请构建项目');
  133. return;
  134. }
  135. if (!Fs.existsSync(srcPath)) {
  136. this.log(resourceRootDir + '不存在src目录, 无法拷贝文件');
  137. return;
  138. }
  139. if (!Fs.existsSync(resPath)) {
  140. this.log(resourceRootDir + '不存在res目录, 无法拷贝文件');
  141. return;
  142. }
  143. },
  144. onTestEnvChange (event) {
  145. // let children = event.target.$select.children;
  146. // for (let i = 0; i < children.length; i++) {
  147. // let item = children[i];
  148. // if (item.value === "0") {
  149. // item.selected = true;
  150. // } else {
  151. // item.selected = false;
  152. // }
  153. // }
  154. let selectValue = event.target.value;
  155. this.testEnvALi = false;
  156. this.testEnvEmail = false;
  157. if (selectValue === '0') {// 本地测试环境
  158. } else if (selectValue === '1') {// 阿里云测试环境
  159. this.testEnvALi = true;
  160. } else if (selectValue === '2') {
  161. this.testEnvEmail = true;
  162. }
  163. },
  164. onCleanSimRemoteRes () {
  165. let runPlatform = process.platform;
  166. let remoteAsset = null;
  167. if (runPlatform === 'win32') {
  168. let simPath = Path.join(Editor.appPath, '../cocos2d-x/simulator/');
  169. remoteAsset = Path.join(simPath, 'win32/remote-asset');
  170. } else if (runPlatform === 'darwin') {
  171. let simPath = Path.join(Editor.appPath, '../cocos2d-x/simulator/');
  172. remoteAsset = Path.join(simPath, 'mac/Simulator.app/Contents/Resources/remote-asset');
  173. }
  174. if (remoteAsset) {
  175. if (!Fs.existsSync(remoteAsset)) {
  176. console.log(remoteAsset);
  177. this.log('[清理热更缓存] 目录不存在: ' + remoteAsset);
  178. } else {
  179. FsExtra.emptyDirSync(remoteAsset);
  180. this.log('[清理热更缓存] 清空目录 ' + remoteAsset + ' 成功.');
  181. }
  182. }
  183. },
  184. onOpenLocalServer () {
  185. this.openDir(this.localServerPath);
  186. },
  187. // 选择物理server路径
  188. onSelectLocalServerPath (event) {
  189. let path = Editor.projectInfo.path;
  190. if (this.localServerPath && this.localServerPath.length > 0) {
  191. if (Fs.existsSync(this.localServerPath)) {
  192. path = this.localServerPath;
  193. }
  194. }
  195. let res = Editor.Dialog.openFile({
  196. title: '选择本地测试服务器目录',
  197. defaultPath: path,
  198. properties: ['openDirectory'],
  199. });
  200. if (res !== -1) {
  201. this.localServerPath = res[0];
  202. this._saveConfig();
  203. this._updateServerVersion();
  204. }
  205. },
  206. // 刷新服务器版本号
  207. refreshLocalServerVersion () {
  208. this._updateServerVersion();
  209. },
  210. // 初始化本地server目录
  211. _initLocalServerDir () {
  212. if (this.localServerPath && this.localServerPath.length > 0) {
  213. console.log('已经有本地server目录');
  214. } else {
  215. this.localServerPath = OutPut.testServerDir;
  216. }
  217. },
  218. _updateServerVersion () {
  219. if (this.localServerPath.length > 0) {
  220. let manifest = {
  221. 'version.manifest': null,
  222. 'project.manifest': null,
  223. };
  224. for (let key in manifest) {
  225. let fullPath = Path.join(this.localServerPath, key);
  226. if (Fs.existsSync(fullPath)) {
  227. manifest[key] = JSON.parse(Fs.readFileSync(fullPath, 'utf-8'));
  228. } else {
  229. this.log(`测试环境:未发现文件${key}`);
  230. }
  231. }
  232. let versionData = manifest['version.manifest'];
  233. let projectData = manifest['project.manifest'];
  234. if (versionData && projectData) {
  235. if (versionData.version === projectData.version &&
  236. versionData.packageUrl === projectData.packageUrl) {
  237. this.serverVersion = projectData.version;
  238. this.serverPackageUrl = projectData.packageUrl;
  239. }
  240. } else if (versionData && !projectData) {
  241. this.serverVersion = versionData.version;
  242. this.serverPackageUrl = versionData.packageUrl;
  243. } else if (!versionData && projectData) {
  244. this.serverVersion = projectData.version;
  245. this.serverPackageUrl = projectData.packageUrl;
  246. } else if (!versionData && !projectData) {
  247. this.log('测试环境: 无法获取到本地测试服务器版本号');
  248. }
  249. } else {
  250. this.log('请选择本机server物理路径');
  251. }
  252. },
  253. onBtnClickSendMail () {
  254. // Editor.Ipc.sendToMain('hot-update-tools:test', 'Hello, this is simple panel');
  255. Mail.sendMail(this.remoteServerVersion, this.emailContent, null, () => {
  256. this.log('发送邮件完毕!');
  257. });
  258. },
  259. onInputMailPeopleOver () {
  260. if (this.isPeopleExist() === false) {
  261. this.emailPeopleArray.push(this.addMailPeople);
  262. }
  263. },
  264. isPeopleExist () {
  265. console.log('isPeopleExist');
  266. if (this.addMailPeople === null || this.addMailPeople === '') {
  267. return false;
  268. }
  269. for (let i = 0; i < this.emailPeopleArray.length; i++) {
  270. let itemPeople = this.emailPeopleArray[i];
  271. if (itemPeople === this.addMailPeople) {
  272. return true;
  273. }
  274. }
  275. return false;
  276. },
  277. onClickPackageUrl () {
  278. let url = `${this.serverPackageUrl}/version.manifest`;
  279. shell && shell.openExternal(url);
  280. },
  281. onClickServerUrl () {
  282. shell && shell.openExternal(this.serverUrl);
  283. },
  284. },
  285. });