104 lines
3.5 KiB
JavaScript
104 lines
3.5 KiB
JavaScript
'use strict';
|
|
const fs = require('fs');
|
|
const {join} = require('path');
|
|
const FileUtil = require('./js/FileUtils');
|
|
const FileUpload = require('./js/FileUploader');
|
|
|
|
// 生成更新文件.
|
|
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');
|
|
}
|
|
if (!this.ftp) {
|
|
this.ftp = FileUpload();
|
|
}
|
|
this.ftp.setStateListener(this.onUploadStateChange.bind(this));
|
|
},
|
|
|
|
onUploadStateChange(state) {
|
|
if (!state) {
|
|
Editor.info('开始上传更新包');
|
|
} else {
|
|
Editor.success('所有文件上传完成!');
|
|
}
|
|
},
|
|
|
|
unload() {
|
|
Editor.Builder.removeListener('build-finished', doMakeUpdatePackage);
|
|
if (this.ftp) {
|
|
this.ftp.destroy();
|
|
}
|
|
},
|
|
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);
|
|
},
|
|
'uploadStop'() {
|
|
if (this.ftp) {
|
|
this.ftp.destroy();
|
|
}
|
|
}
|
|
},
|
|
}; |