[add] 熱更新

This commit is contained in:
2022-08-31 09:48:48 +08:00
parent fd5f31fd66
commit 2bde4ac72e
57 changed files with 10746 additions and 31 deletions

View File

@@ -0,0 +1,128 @@
let fs = require('fire-fs');
let path = require('fire-path');
let electron = require('electron');
let FileUtil = Editor.require("packages://hot-update-tools/core/FileUtil");
let self = module.exports = {
cfgData: {
version: "",
serverRootDir: "",
resourceRootDir: "",
genManifestDir: "",
genProjectManifestFile: "",
localServerPath: "",
hotAddressArray: [],
buildTime: null,// 构建时间,全部保存int值
genTime: null,// manifest生成时间
genVersion: null,// manifest版本
},
updateBuildTimeByMain(time) {
// 在main.js中调用electron中没有remote属性
// Editor.log(electron.app.getPath('userData'));
let cfgPath = this._getAppCfgPath();
if (fs.existsSync(cfgPath)) {
let data = fs.readFileSync(cfgPath, 'utf-8');
let json = JSON.parse(data);
json.buildTime = time;
json.genTime = time;
fs.writeFileSync(cfgPath, JSON.stringify(json));
} else {
Editor.log("热更新配置文件不存在: " + cfgPath);
}
},
updateBuildTime(time) {
this.cfgData.buildTime = time;
this.cfgData.genTime = time;
this._save();
},
updateGenTime(time, version) {
this.cfgData.genTime = time;
this.cfgData.genVersion = version;
this._save();
},
// 获取构建时间生成时间
getBuildTimeGenTime() {
let ret = { buildTime: null, genTime: null };
let cfgPath = this._getAppCfgPath();
if (fs.existsSync(cfgPath)) {
let data = fs.readFileSync(cfgPath, 'utf-8');
let json = JSON.parse(data);
ret.buildTime = json.buildTime;
ret.genTime = json.genTime;
this.cfgData.buildTime = json.buildTime;
this.cfgData.genTime = json.genTime;
}
return ret;
},
saveConfig(data) {
this.cfgData.version = data.version;
this.cfgData.genProjectManifestFile = data.genProjectManifestFile;
this.cfgData.serverRootDir = data.serverRootDir;
this.cfgData.resourceRootDir = data.resourceRootDir;
this.cfgData.localServerPath = data.localServerPath;
this.cfgData.hotAddressArray = data.hotAddressArray;
this._save();
},
_save() {
let configFilePath = self._getAppCfgPath();
let ret = fs.writeFileSync(configFilePath, JSON.stringify(this.cfgData));
console.log("保存配置成功!");
},
cleanConfig() {
fs.unlink(this._getAppCfgPath());
},
// manifest文件包地址
getMainFestDir() {
let userDataPath = electron.remote.app.getPath('userData');
return path.join(userDataPath, "hot-update-tools-manifestOutPut");
//输出文件不能存在在插件目录下,否则会造成插件刷新
// return Editor.url('packages://hot-update-tools/outPut');
},
// 获取打包目录地址,一般放在项目目录下
getPackZipDir() {
let userDataPath = electron.remote.app.getPath('userData');
return path.join(this._getAppRootPath(), "packVersion");
},
_getAppRootPath() {
let lib = Editor.libraryPath;
return lib.substring(0, lib.length - 7);
},
_getAppCfgPath() {
let userDataPath = null;
if (electron.remote) {
userDataPath = electron.remote.app.getPath('userData');
} else {
userDataPath = electron.app.getPath('userData');
}
let tar = Editor.libraryPath;
tar = tar.replace(/\\/g, '-');
tar = tar.replace(/:/g, '-');
tar = tar.replace(/\//g, '-');
return path.join(userDataPath, "hot-update-tools-cfg-" + tar + ".json");
// return Editor.url('packages://hot-update-tools/save/cfg.json');
},
initCfg(cb) {
let configFilePath = this._getAppCfgPath();
let b = FileUtil.isFileExit(configFilePath);
if (b) {
console.log("cfg path: " + configFilePath);
fs.readFile(configFilePath, 'utf-8', function (err, data) {
if (!err) {
let saveData = JSON.parse(data.toString());
self.cfgData = saveData;
if (cb) {
cb(saveData);
}
}
}.bind(self));
} else {
if (cb) {
cb(null);
}
}
}
}

View File

@@ -0,0 +1,136 @@
let fs = require("fire-fs");
let path = require("fire-path");
let self = module.exports = {
getDirAllFiles(dirPath, result) {
let files = fs.readdirSync(dirPath);
files.forEach((val, index) => {
let fPath = path.join(dirPath, val);
let stats = fs.statSync(fPath);
if (stats.isDirectory()) {
this.getDirAllFiles(fPath, result);
} else if (stats.isFile()) {
result.push(fPath);
}
});
},
getFileString(fileList, options) {
let curIndex = 0;
let totalIndex = fileList.length;
let str = {};
for (let key in fileList) {
let filePath = fileList[key];
let b = this._isFileExit(filePath);
if (b) {
fs.readFile(filePath, 'utf-8', function (err, data) {
if (!err) {
self._collectString(data, str);
} else {
console.log("error: " + filePath);
}
self._onCollectStep(filePath, ++curIndex, totalIndex, str, options);
})
} else {
self._onCollectStep(filePath, ++curIndex, totalIndex, str, options);
}
}
},
_onCollectStep(filePath, cur, total, str, data) {
if (data && data.stepCb) {
data.stepCb(filePath, cur, total);
}
if (cur >= total) {
self._onCollectOver(str, data);
}
},
_onCollectOver(collectObjArr, data) {
let strArr = [];
let str = "";
for (let k in collectObjArr) {
str += k;
strArr.push(k);
}
// console.log("一共有" + strArr.length + "个字符, " + strArr);
console.log("一共有" + strArr.length + "个字符");
if (data && data.compCb) {
data.compCb(str);
}
},
mkDir(path) {
try {
fs.mkdirSync(path);
} catch (e) {
if (e.code !== 'EEXIST') throw e;
}
},
isFileExit(file) {
try {
fs.accessSync(file, fs.F_OK);
} catch (e) {
return false;
}
return true;
},
_collectString(data, collectObject) {
for (let i in data) {
let char = data.charAt(i);
if (collectObject[char]) {
collectObject[char]++;
} else {
collectObject[char] = 1;
}
}
},
emptyDir(rootFile) {
//删除所有的文件(将所有文件夹置空)
let emptyDir = function (fileUrl) {
let files = fs.readdirSync(fileUrl);//读取该文件夹
for (let k in files) {
let filePath = path.join(fileUrl, files[k]);
let stats = fs.statSync(filePath);
if (stats.isDirectory()) {
emptyDir(filePath);
} else {
fs.unlinkSync(filePath);
console.log("删除文件:" + filePath);
}
}
};
//删除所有的空文件夹
let rmEmptyDir = function (fileUrl) {
let files = fs.readdirSync(fileUrl);
if (files.length > 0) {
for (let k in files) {
let rmDir = path.join(fileUrl, files[k]);
rmEmptyDir(rmDir);
}
if (fileUrl !== rootFile) {// 不删除根目录
fs.rmdirSync(fileUrl);
console.log('删除空文件夹' + fileUrl);
}
} else {
if (fileUrl !== rootFile) {// 不删除根目录
fs.rmdirSync(fileUrl);
console.log('删除空文件夹' + fileUrl);
}
}
};
emptyDir(rootFile);
rmEmptyDir(rootFile);
},
/*
is_fileType($('#uploadfile').val(), 'doc,pdf,txt,wps,odf,md,png,gif,jpg')
* */
is_fileType(filename, types) {
types = types.split(',');
let pattern = '\.(';
for (let i = 0; i < types.length; i++) {
if (0 !== i) {
pattern += '|';
}
pattern += types[i].trim();
}
pattern += ')$';
return new RegExp(pattern, 'i').test(filename);
}
}

View File

@@ -0,0 +1,141 @@
"use strict";
/**
* 处理内部逻辑发出HTTP请求
*/
var http = require("http");
var https = require('https');
var qs = require('querystring');
var HttpService = function(){
//todo
};
var pro = HttpService.prototype;
//发送HTTP GET请求
pro.sendHttpGetReq = function(hostName,port,path,param,cb){
console.log("sendHttpGetReq");
var content = qs.stringify(param);
console.log("content:",content);
var options = {
hostname: hostName,
port: port,
path: path+"?"+content,
method: 'GET'
};
console.log(options);
//todo 请求超时timer
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
cb(null,JSON.parse(chunk));
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
cb(new Error("err"),null)
});
req.end();
};
//发送HTTPS GET请求
pro.sendHttpsGetReq = function(hostName,port,path,param,cb){
console.log("sendHttpGetReq");
var content = qs.stringify(param);
https.get(hostName + ":" + port + path + "?"+content, function(res){
console.log('statusCode: ', res.statusCode);
res.on('data', function(d){
cb(null,JSON.parse(d.toString()))
});
}).on('error',function(e) {
console.error(e);
cb(e)
});
};
//发送HTTP POST请求
pro.sendHttpPostReq = function(hostName,port,path,param,cb){
console.log("sendHttpPostReq");
var content = qs.stringify(param);
console.log("content:",content);
var options = {
hostname: hostName,
port: port,
path: path,
method: 'POST',
headers: {
"Content-Type": 'application/x-www-form-urlencoded',
"Content-Length": content.length
}
};
//todo 请求超时timer
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
if (res.statusCode == 200) {
res.setEncoding('utf8');
var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
console.log(data);
cb(null,JSON.parse(data));
});
}else{
res.send(500, "error");
cb(new Error("err"),null)
}
});
req.on('error', function (e) {
cb(new Error("err"),null)
});
req.write(content);
req.end();
};
//发送HTTPS POST请求
pro.sendHttpsPostReq = function(hostName,port,path,param,cb){
console.log("sendHttpsPostReq");
var content = qs.stringify(param);
path = path + "?" + content;
console.log("path=>",path);
var options = {
hostname: hostName,
port: port || 443,
path: path || '/',
method: 'POST'
};
https.request(options,function(res){
console.log('statusCode: ', res.statusCode);
res.on('data', function(d){
cb(null,JSON.parse(d.toString()))
});
}).on('error',function(e) {
console.error(e);
cb(e)
});
};
module.exports = new HttpService();