123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- var FS = require('fire-fs');
- let Path = require('path');
- let self = module.exports = {
- // 递归创建目录
- mkdirsSync: function (dirname) {
- if (FS.existsSync(dirname)) {
- return true;
- } else {
- if (self.mkdirsSync(Path.dirname(dirname))) {
- FS.mkdirSync(dirname);
- return true;
- }
- }
- },
- /**
- *
- * @param {*} srcPath 资源文件夹
- * @param {*} destPath 目标文件夹
- */
- //拷贝文件到指定目录,并重建目录结构
- copyFile: function (srcPath, destPath) {
- self.mkdirsSync(Path.dirname(destPath));
- FS.writeFileSync(destPath, FS.readFileSync(srcPath));
- },
- delFile: function (path, reservePath) {
- if (FS.existsSync(path)) {
- if (FS.statSync(path).isDirectory()) {
- let files = FS.readdirSync(path);
- files.forEach((file, index) => {
- let currentPath = path + "/" + file;
- if (FS.statSync(currentPath).isDirectory()) {
- self.delFile(currentPath, reservePath);
- } else {
- FS.unlinkSync(currentPath);
- }
- });
- if (path != reservePath) {
- FS.rmdirSync(path);
- }
- } else {
- FS.unlinkSync(path);
- }
- }
- },
- IokitData: {
- OldVersionJson: "",
- NewVersionJson: "",
- FinalPath: "",
- },
- build(oldFile, newFile, resPath, FinalPath, cb) {
- // cb("old:"+oldFile)
- // cb("new:"+newFile)
- // cb("final:"+FinalPath)
- let OldfilePathUrl = oldFile//Editor.url(oldFile);
- let hasOld = true;
- if (FS.existsSync(OldfilePathUrl)) {
- FS.readFile(OldfilePathUrl, 'utf-8', function (err, data) {
- if (!err) {
- //let OldVersionJsonData = JSON.parse(data.toString());
- self.OldVersionJson = data.toString();
- if (cb) {
- //cb("旧版本文件读取成功:" + self.OldVersionJson);
- }
- }
- }.bind(self));
- } else {
- if (cb) {
- cb("旧版本文件不存在" + OldfilePathUrl);
- }
- hasOld = false;
- // return;
- }
- if (FS.existsSync(newFile)) {
- FS.readFile(newFile, 'utf-8', function (err, data) {
- if (!err) {
- //let NewVersionJsonData = JSON.parse(data.toString());
- self.NewVersionJson = data.toString();
- if (cb) {
- //cb("新版本文件读取成功:" + self.NewVersionJson);
- }
- }
- }.bind(self));
- } else {
- if (cb) {
- cb("新版本文件不存在:" + newFile);
- }
- return;
- }
- if (FS.existsSync(FinalPath)) {
- } else {
- if (cb) {
- cb("生成文件目录不存在:" + FinalPath);
- }
- return;
- }
-
- cb("开始执行...");
- setTimeout(function () {
- if (self.OldVersionJson != "" && self.NewVersionJson != "") {
- cb("准备解析版本文件...");
- // let newAssetsVersion = JSON.parse(self.NewVersionJson).version;
- // cb("pack_assets:" + newAssetsVersion);
-
- let oldAssets = [];
- if(hasOld) {
- oldAssets = JSON.parse(self.OldVersionJson).assets;
- }
- let newAssets = JSON.parse(self.NewVersionJson).assets;
- cb("解析成功,正在清空目标文件夹...");
-
-
- // let destVersionFolder = "\\" + newAssetsVersion;
- // FinalPath += destVersionFolder;
- self.delFile(FinalPath,FinalPath);
- cb("生成文件夹已清空!开始筛选...");
-
- let dataArray = [];
- let idx = 0;
- for (var newKey in newAssets) {
- let newValue = newAssets[newKey];
- let newElementSize = newValue["size"];
- let newElementMd5 = newValue["md5"];
- /*cb("找到:newAssets:" + idx);
- cb("找到:newValueKey:" + newKey);
- cb("找到:newElementSize:" + newElementSize);
- cb("找到:newElementMd5:" + newElementMd5);*/
- let isFind = false;
- // if(hasOld) {
- for (var oldKey in oldAssets) {
- let oldValue = oldAssets[oldKey];
- let oldElementSize = oldValue["size"];
- let oldElementMd5 = oldValue["md5"];
- if (oldKey == newKey && oldElementSize == newElementSize && oldElementMd5 == newElementMd5) {
- isFind = true;
- break;
- }
- }
- // }
-
- if (!isFind) {//没找到
- //cb("找到差异文件:" + newKey + " size:" + newElementSize + " md5:" + newElementMd5);
- dataArray.push(newKey);
- }
- idx++;
- }
- cb("筛选成功,共 " + dataArray.length + "个文件。");
- for (let i = 0; i < dataArray.length; i++) {
- let srcPath = resPath + "\\" + dataArray[i];
- let destPath = FinalPath + "\\" + dataArray[i];
- // cb("拷贝 " + srcPath + " 到 " + destPath);
- self.copyFile(srcPath, destPath);
- }
- cb("" + dataArray.length +" 个文件已成功拷贝至:" + FinalPath);
- let srcProjectFile = resPath + "\\project.manifest";
- let destProjectFile = FinalPath+"\\project.manifest";
- self.copyFile(srcProjectFile,destProjectFile);
- cb("" + srcProjectFile +" 文件已成功拷贝至:" + destProjectFile);
- let srcVersionFile = resPath + "\\version.manifest";
- let destVersionFile = FinalPath+"\\version.manifest";
- self.copyFile(srcVersionFile, destVersionFile);
- cb("" + srcVersionFile +" 文件已成功拷贝至:" + destVersionFile, true);
- } else {
- cb("文件打开失败,请重新操作!");
- }
- }, 100);
- },
- }
|