hot-update/packages/update-manager/main.js

117 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-02-01 16:11:00 +00:00
'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');
2021-02-01 16:11:00 +00:00
// 生成更新文件.
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,
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');
}
2021-02-02 03:26:55 +00:00
if (!this.ftp) {
this.ftp = FileUpload();
}
this.ftp.setStateListener(this.onUploadStateChange.bind(this));
},
onUploadStateChange(state) {
logger.log('当前任务执行器状态:', state);
2021-02-02 03:26:55 +00:00
if (!state) {
this.startUploadTime = new Date().getTime();
logger.info('开始上传更新包', this.startUploadTime);
2021-02-02 03:26:55 +00:00
} else {
logger.success(`所有文件上传完成,耗时:${Math.floor((new Date().getTime() - this.startUploadTime) / 1000)}`);
2021-02-02 03:26:55 +00:00
}
2021-02-01 16:11:00 +00:00
},
unload() {
Editor.Builder.removeListener('build-finished', doMakeUpdatePackage);
2021-02-02 03:26:55 +00:00
if (this.ftp) {
this.ftp.destroy();
2021-02-03 01:40:01 +00:00
this.ftp = null;
2021-02-02 03:26:55 +00:00
}
2021-02-01 16:11:00 +00:00
},
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);
},
2021-02-02 03:26:55 +00:00
'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);
},
2021-02-01 16:11:00 +00:00
'uploadStop'() {
if (this.ftp) {
this.ftp.destroy();
}
}
},
};