'use strict'; const fs = require('fs'); const {join} = require('path'); const FileUtil = require('./js/FileUtils'); const FileUpload = require('./js/FileUploader'); const logger = require('./js/logger'); // 生成更新文件. const doMakeUpdatePackage = function (opt, cb) { if (opt.platform == 'android' || opt.platform == 'ios') { try { const configStr = fs.readFileSync( Editor.url('packages://update-manager/.settings.conf'), 'utf-8'); const configJson = JSON.parse(configStr || "{}"); if (typeof configJson.versionCfg.versionCode === 'undefined') { logger.warn("未配置更新版本,不生成热更Patch"); cb && cb(); return; } if (!configJson.versionCfg.versionType || configJson.versionCfg.versionType.length <= 0) { configJson.versionCfg.versionType = 'dev'; } const bundleVers = {}; for (let b of opt.bundles) { bundleVers[b.name] = b.version; } saveNewVersionInfo(opt, bundleVers, configJson); const remoteDir = join(opt.dest, 'remote'); const assetDir = join(opt.dest, 'assets'); FileUtil.copy(assetDir, remoteDir); cb && cb(); } catch (e) { logger.log("未生成更新包", e); cb && cb(); } } else { cb && cb(); } }; const saveNewVersionInfo = function (opt, bundleVers, configJson) { const data = { versionCode: configJson.versionCfg.versionCode, versionName: configJson.versionCfg.versionName, versionType: configJson.versionCfg.versionType, versionLog: configJson.versionCfg.versionLog, forceUpdate: configJson.versionCfg.forceUpdate, server: `${configJson.versionCfg.baseUrl}${configJson.ftpCfg.rootPath}${configJson.versionCfg.versionType}/${configJson.versionCfg.versionCode}` }; const updateInfo = Object.assign(data, {bundles: bundleVers}); // 将版本更新信息写入文件 let targetUpdatePath = join(opt.dest, `remote/update-${configJson.versionCfg.versionType}.json`) FileUtil.write(targetUpdatePath, JSON.stringify(updateInfo, null, 2)); logger.log("更新信息存储位置:", targetUpdatePath); }; module.exports = { load() { Editor.Builder.on('build-finished', doMakeUpdatePackage); try { fs.readFileSync( Editor.url('packages://update-manager/.settings.conf'), 'utf-8'); } catch (e) { fs.writeFileSync( Editor.url('packages://update-manager/.settings.conf'), JSON.stringify({}, null, 2), 'utf-8'); } if (!this.ftp) { this.ftp = FileUpload(); } this.ftp.setStateListener(this.onUploadStateChange.bind(this)); }, onUploadStateChange(state) { logger.log('当前任务执行器状态:', state); if (!state) { this.startUploadTime = new Date().getTime(); logger.info('开始上传更新包', this.startUploadTime); } else { logger.success(`所有文件上传完成,耗时:${Math.floor((new Date().getTime() - this.startUploadTime) / 1000)}秒`); } }, unload() { Editor.Builder.removeListener('build-finished', doMakeUpdatePackage); if (this.ftp) { this.ftp.destroy(); this.ftp = null; } }, messages: { 'open'() { Editor.Panel.open('update-manager'); }, 'upload'(event, dir, dst, options) { logger.info('upload....'); this.ftp.setOption(options, options.maxThread); this.ftp.upload(dir, dst); }, 'getProjectPath'(event) { event.reply(Editor.Project.path); }, "checkThreads"(event) { event.reply(this.ftp.checkThreads()); }, 'queryThreadStates'(event) { event.reply(this.ftp.checkState()); }, 'restartThread'(event, name) { this.ftp.restart(name); }, 'uploadStop'() { if (this.ftp) { this.ftp.destroy(); } } }, };