iokit.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var FS = require('fire-fs');
  2. let Path = require('path');
  3. let self = module.exports = {
  4. // 递归创建目录
  5. mkdirsSync: function (dirname) {
  6. if (FS.existsSync(dirname)) {
  7. return true;
  8. } else {
  9. if (self.mkdirsSync(Path.dirname(dirname))) {
  10. FS.mkdirSync(dirname);
  11. return true;
  12. }
  13. }
  14. },
  15. /**
  16. *
  17. * @param {*} srcPath 资源文件夹
  18. * @param {*} destPath 目标文件夹
  19. */
  20. //拷贝文件到指定目录,并重建目录结构
  21. copyFile: function (srcPath, destPath) {
  22. self.mkdirsSync(Path.dirname(destPath));
  23. FS.writeFileSync(destPath, FS.readFileSync(srcPath));
  24. },
  25. delFile: function (path, reservePath) {
  26. if (FS.existsSync(path)) {
  27. if (FS.statSync(path).isDirectory()) {
  28. let files = FS.readdirSync(path);
  29. files.forEach((file, index) => {
  30. let currentPath = path + "/" + file;
  31. if (FS.statSync(currentPath).isDirectory()) {
  32. self.delFile(currentPath, reservePath);
  33. } else {
  34. FS.unlinkSync(currentPath);
  35. }
  36. });
  37. if (path != reservePath) {
  38. FS.rmdirSync(path);
  39. }
  40. } else {
  41. FS.unlinkSync(path);
  42. }
  43. }
  44. },
  45. IokitData: {
  46. OldVersionJson: "",
  47. NewVersionJson: "",
  48. FinalPath: "",
  49. },
  50. build(oldFile, newFile, resPath, FinalPath, cb) {
  51. // cb("old:"+oldFile)
  52. // cb("new:"+newFile)
  53. // cb("final:"+FinalPath)
  54. let OldfilePathUrl = oldFile//Editor.url(oldFile);
  55. let hasOld = true;
  56. if (FS.existsSync(OldfilePathUrl)) {
  57. FS.readFile(OldfilePathUrl, 'utf-8', function (err, data) {
  58. if (!err) {
  59. //let OldVersionJsonData = JSON.parse(data.toString());
  60. self.OldVersionJson = data.toString();
  61. if (cb) {
  62. //cb("旧版本文件读取成功:" + self.OldVersionJson);
  63. }
  64. }
  65. }.bind(self));
  66. } else {
  67. if (cb) {
  68. cb("旧版本文件不存在" + OldfilePathUrl);
  69. }
  70. hasOld = false;
  71. // return;
  72. }
  73. if (FS.existsSync(newFile)) {
  74. FS.readFile(newFile, 'utf-8', function (err, data) {
  75. if (!err) {
  76. //let NewVersionJsonData = JSON.parse(data.toString());
  77. self.NewVersionJson = data.toString();
  78. if (cb) {
  79. //cb("新版本文件读取成功:" + self.NewVersionJson);
  80. }
  81. }
  82. }.bind(self));
  83. } else {
  84. if (cb) {
  85. cb("新版本文件不存在:" + newFile);
  86. }
  87. return;
  88. }
  89. if (FS.existsSync(FinalPath)) {
  90. } else {
  91. if (cb) {
  92. cb("生成文件目录不存在:" + FinalPath);
  93. }
  94. return;
  95. }
  96. cb("开始执行...");
  97. setTimeout(function () {
  98. if (self.OldVersionJson != "" && self.NewVersionJson != "") {
  99. cb("准备解析版本文件...");
  100. // let newAssetsVersion = JSON.parse(self.NewVersionJson).version;
  101. // cb("pack_assets:" + newAssetsVersion);
  102. let oldAssets = [];
  103. if(hasOld) {
  104. oldAssets = JSON.parse(self.OldVersionJson).assets;
  105. }
  106. let newAssets = JSON.parse(self.NewVersionJson).assets;
  107. cb("解析成功,正在清空目标文件夹...");
  108. // let destVersionFolder = "\\" + newAssetsVersion;
  109. // FinalPath += destVersionFolder;
  110. self.delFile(FinalPath,FinalPath);
  111. cb("生成文件夹已清空!开始筛选...");
  112. let dataArray = [];
  113. let idx = 0;
  114. for (var newKey in newAssets) {
  115. let newValue = newAssets[newKey];
  116. let newElementSize = newValue["size"];
  117. let newElementMd5 = newValue["md5"];
  118. /*cb("找到:newAssets:" + idx);
  119. cb("找到:newValueKey:" + newKey);
  120. cb("找到:newElementSize:" + newElementSize);
  121. cb("找到:newElementMd5:" + newElementMd5);*/
  122. let isFind = false;
  123. // if(hasOld) {
  124. for (var oldKey in oldAssets) {
  125. let oldValue = oldAssets[oldKey];
  126. let oldElementSize = oldValue["size"];
  127. let oldElementMd5 = oldValue["md5"];
  128. if (oldKey == newKey && oldElementSize == newElementSize && oldElementMd5 == newElementMd5) {
  129. isFind = true;
  130. break;
  131. }
  132. }
  133. // }
  134. if (!isFind) {//没找到
  135. //cb("找到差异文件:" + newKey + " size:" + newElementSize + " md5:" + newElementMd5);
  136. dataArray.push(newKey);
  137. }
  138. idx++;
  139. }
  140. cb("筛选成功,共 " + dataArray.length + "个文件。");
  141. for (let i = 0; i < dataArray.length; i++) {
  142. let srcPath = resPath + "\\" + dataArray[i];
  143. let destPath = FinalPath + "\\" + dataArray[i];
  144. // cb("拷贝 " + srcPath + " 到 " + destPath);
  145. self.copyFile(srcPath, destPath);
  146. }
  147. cb("" + dataArray.length +" 个文件已成功拷贝至:" + FinalPath);
  148. let srcProjectFile = resPath + "\\project.manifest";
  149. let destProjectFile = FinalPath+"\\project.manifest";
  150. self.copyFile(srcProjectFile,destProjectFile);
  151. cb("" + srcProjectFile +" 文件已成功拷贝至:" + destProjectFile);
  152. let srcVersionFile = resPath + "\\version.manifest";
  153. let destVersionFile = FinalPath+"\\version.manifest";
  154. self.copyFile(srcVersionFile, destVersionFile);
  155. cb("" + srcVersionFile +" 文件已成功拷贝至:" + destVersionFile, true);
  156. } else {
  157. cb("文件打开失败,请重新操作!");
  158. }
  159. }, 100);
  160. },
  161. }