const Path = require('path'); const Fs = require('fs'); const FsExtra = require('fs-extra'); const Express = require('express'); const CfgUtil = Editor.require('packages://hot-update-tools/core/CfgUtil.js'); const Util = Editor.require('packages://hot-update-tools/core/Util.js'); const OutPut = Editor.require('packages://hot-update-tools/core/OutPut.js'); const GoogleAnalytics = Editor.require('packages://hot-update-tools/core/GoogleAnalytics.js'); const { shell } = require('electron'); Vue.component('env-test', { template: Fs.readFileSync(Editor.url('packages://hot-update-tools/panel/env-test.html'), 'utf-8'), mixins: [Editor.require('packages://hot-update-tools/panel/mixin.js')], data () { return { serverApp: null, serverUrl: null, localServerPath: '', serverVersion: null, serverPackageUrl: null, // 测试环境逻辑变量 testEnvALi: false, testEnvEmail: false, // 发送邮件界面 testEnvSelect: 0, // 邮件逻辑变量 emailContent: '邮件内容!', addMailPeople: '', emailPeopleArray: [ 'xu_yanfeng@126.com', ], }; }, created () { this.$nextTick(() => { let data = CfgUtil.cfgData; if (data) { this.localServerPath = data.localServerPath; } this._initLocalServerDir(); this._updateServerVersion(); }); }, methods: { // 检测port是否被占用 _getAvailableNetPort (port, callback) { console.log('检测端口是否被占用: ' + port); let net = require('net'); // tcp使用端口0表示系统分配端口 let server = net.createServer().listen(port); server.on('listening', () => { server.once('close', () => { callback && callback(port); }); server.close(); }); server.on('error', (err) => { this._getAvailableNetPort(port + 1, callback); }); }, onBtnClickOpenStaticFileServer () { let staticDir = this.localServerPath; if (staticDir && Fs.existsSync(staticDir)) { this._getAvailableNetPort(5520, (serverPort) => { let app = Express(); app.use(Express.static(staticDir)); app.listen(serverPort); this.serverApp = app; let ip = Util.getLocalIP(); this.serverUrl = `http://${ip}:${serverPort}/`; this.log(`Http文件服务开启: ${staticDir}`); }); } }, onCopyFileToLocalServer () { GoogleAnalytics.eventCustom('copyFileToLocalServer'); let { localServerPath } = this; let { manifestResDir } = Util; let { resourceRootDir } = CfgUtil.cfgData; let { manifestDir } = OutPut; if (!Fs.existsSync(localServerPath)) { this.log('本地测试服务器目录不存在:' + localServerPath); return; } // 检查资源目录 let srcPath = Path.join(resourceRootDir, 'src'); let resPath = Path.join(resourceRootDir, manifestResDir); if (!Fs.existsSync(resourceRootDir)) { this.log('资源目录不存在: ' + resourceRootDir + ', 请先构建项目'); return; } if (!Fs.existsSync(srcPath)) { this.log(resourceRootDir + '不存在src目录, 无法拷贝文件'); return; } if (!Fs.existsSync(resPath)) { this.log(resourceRootDir + '不存在res目录, 无法拷贝文件'); return; } // 检查manifest文件 let projectTag = 'project.manifest'; let versionTag = 'version.manifest'; let project = Path.join(manifestDir, projectTag); let version = Path.join(manifestDir, versionTag); if (!manifestDir || manifestDir.length <= 0) { this.log('manifest文件生成地址未填写'); return; } if (!Fs.existsSync(project)) { this.log(project + '不存在, 请点击生成热更包'); return; } if (!Fs.existsSync(version)) { this.log(version + '不存在, 请点击生成热更包'); return; } FsExtra.emptyDirSync(localServerPath); FsExtra.copySync(srcPath, Path.join(localServerPath, 'src')); FsExtra.copySync(resPath, Path.join(localServerPath, manifestResDir)); FsExtra.copyFileSync(project, Path.join(localServerPath, projectTag)); FsExtra.copyFileSync(version, Path.join(localServerPath, versionTag)); this.log(`已经将热更包copy到: ${localServerPath}`); this._updateServerVersion(); }, onCopyFileToReduceDir() { GoogleAnalytics.eventCustom('onCopyFileToReduceDir'); let { manifestResDir } = Util; let { manifestDir,reduceDir,versionsDir } = OutPut; let { resourceRootDir,serverRootDir } = CfgUtil.cfgData; // 检查资源目录 let srcPath = Path.join(resourceRootDir, 'src'); let resPath = Path.join(resourceRootDir, manifestResDir); if(!Fs.existsSync(resourceRootDir)) { this.log('资源目录不存在:'+resourceRootDir+',请构建项目'); return; } if (!Fs.existsSync(srcPath)) { this.log(resourceRootDir + '不存在src目录, 无法拷贝文件'); return; } if (!Fs.existsSync(resPath)) { this.log(resourceRootDir + '不存在res目录, 无法拷贝文件'); return; } }, onTestEnvChange (event) { // let children = event.target.$select.children; // for (let i = 0; i < children.length; i++) { // let item = children[i]; // if (item.value === "0") { // item.selected = true; // } else { // item.selected = false; // } // } let selectValue = event.target.value; this.testEnvALi = false; this.testEnvEmail = false; if (selectValue === '0') {// 本地测试环境 } else if (selectValue === '1') {// 阿里云测试环境 this.testEnvALi = true; } else if (selectValue === '2') { this.testEnvEmail = true; } }, onCleanSimRemoteRes () { let runPlatform = process.platform; let remoteAsset = null; if (runPlatform === 'win32') { let simPath = Path.join(Editor.appPath, '../cocos2d-x/simulator/'); remoteAsset = Path.join(simPath, 'win32/remote-asset'); } else if (runPlatform === 'darwin') { let simPath = Path.join(Editor.appPath, '../cocos2d-x/simulator/'); remoteAsset = Path.join(simPath, 'mac/Simulator.app/Contents/Resources/remote-asset'); } if (remoteAsset) { if (!Fs.existsSync(remoteAsset)) { console.log(remoteAsset); this.log('[清理热更缓存] 目录不存在: ' + remoteAsset); } else { FsExtra.emptyDirSync(remoteAsset); this.log('[清理热更缓存] 清空目录 ' + remoteAsset + ' 成功.'); } } }, onOpenLocalServer () { this.openDir(this.localServerPath); }, // 选择物理server路径 onSelectLocalServerPath (event) { let path = Editor.projectInfo.path; if (this.localServerPath && this.localServerPath.length > 0) { if (Fs.existsSync(this.localServerPath)) { path = this.localServerPath; } } let res = Editor.Dialog.openFile({ title: '选择本地测试服务器目录', defaultPath: path, properties: ['openDirectory'], }); if (res !== -1) { this.localServerPath = res[0]; this._saveConfig(); this._updateServerVersion(); } }, // 刷新服务器版本号 refreshLocalServerVersion () { this._updateServerVersion(); }, // 初始化本地server目录 _initLocalServerDir () { if (this.localServerPath && this.localServerPath.length > 0) { console.log('已经有本地server目录'); } else { this.localServerPath = OutPut.testServerDir; } }, _updateServerVersion () { if (this.localServerPath.length > 0) { let manifest = { 'version.manifest': null, 'project.manifest': null, }; for (let key in manifest) { let fullPath = Path.join(this.localServerPath, key); if (Fs.existsSync(fullPath)) { manifest[key] = JSON.parse(Fs.readFileSync(fullPath, 'utf-8')); } else { this.log(`测试环境:未发现文件${key}`); } } let versionData = manifest['version.manifest']; let projectData = manifest['project.manifest']; if (versionData && projectData) { if (versionData.version === projectData.version && versionData.packageUrl === projectData.packageUrl) { this.serverVersion = projectData.version; this.serverPackageUrl = projectData.packageUrl; } } else if (versionData && !projectData) { this.serverVersion = versionData.version; this.serverPackageUrl = versionData.packageUrl; } else if (!versionData && projectData) { this.serverVersion = projectData.version; this.serverPackageUrl = projectData.packageUrl; } else if (!versionData && !projectData) { this.log('测试环境: 无法获取到本地测试服务器版本号'); } } else { this.log('请选择本机server物理路径'); } }, onBtnClickSendMail () { // Editor.Ipc.sendToMain('hot-update-tools:test', 'Hello, this is simple panel'); Mail.sendMail(this.remoteServerVersion, this.emailContent, null, () => { this.log('发送邮件完毕!'); }); }, onInputMailPeopleOver () { if (this.isPeopleExist() === false) { this.emailPeopleArray.push(this.addMailPeople); } }, isPeopleExist () { console.log('isPeopleExist'); if (this.addMailPeople === null || this.addMailPeople === '') { return false; } for (let i = 0; i < this.emailPeopleArray.length; i++) { let itemPeople = this.emailPeopleArray[i]; if (itemPeople === this.addMailPeople) { return true; } } return false; }, onClickPackageUrl () { let url = `${this.serverPackageUrl}/version.manifest`; shell && shell.openExternal(url); }, onClickServerUrl () { shell && shell.openExternal(this.serverUrl); }, }, });