iokit.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, FinalPath, cb) {
  51. let OldfilePathUrl = oldFile//Editor.url(oldFile);
  52. if (FS.existsSync(OldfilePathUrl)) {
  53. FS.readFile(OldfilePathUrl, 'utf-8', function (err, data) {
  54. if (!err) {
  55. //let OldVersionJsonData = JSON.parse(data.toString());
  56. self.OldVersionJson = data.toString();
  57. if (cb) {
  58. //cb("旧版本文件读取成功:" + self.OldVersionJson);
  59. }
  60. }
  61. }.bind(self));
  62. } else {
  63. if (cb) {
  64. cb("旧版本文件不存在" + OldfilePathUrl);
  65. }
  66. return;
  67. }
  68. if (FS.existsSync(newFile)) {
  69. FS.readFile(newFile, 'utf-8', function (err, data) {
  70. if (!err) {
  71. //let NewVersionJsonData = JSON.parse(data.toString());
  72. self.NewVersionJson = data.toString();
  73. if (cb) {
  74. //cb("新版本文件读取成功:" + self.NewVersionJson);
  75. }
  76. }
  77. }.bind(self));
  78. } else {
  79. if (cb) {
  80. cb("新版本文件不存在:" + newFile);
  81. }
  82. return;
  83. }
  84. if (FS.existsSync(FinalPath)) {
  85. } else {
  86. if (cb) {
  87. cb("生成文件目录不存在:" + FinalPath);
  88. }
  89. return;
  90. }
  91. cb("开始执行...");
  92. setTimeout(function () {
  93. if (self.OldVersionJson != "" && self.NewVersionJson != "") {
  94. cb("准备解析版本文件...");
  95. let newAssetsVersion = JSON.parse(self.NewVersionJson).version;
  96. cb("pack_assets:" + newAssetsVersion);
  97. let oldAssets = JSON.parse(self.OldVersionJson).assets;
  98. let newAssets = JSON.parse(self.NewVersionJson).assets;
  99. cb("解析成功,正在清空目标文件夹...");
  100. let destVersionFolder = "\\" + newAssetsVersion;
  101. FinalPath += destVersionFolder;
  102. self.delFile(FinalPath,FinalPath);
  103. cb("生成文件夹已清空!开始筛选...");
  104. let dataArray = [];
  105. let idx = 0;
  106. for (var newKey in newAssets) {
  107. let newValue = newAssets[newKey];
  108. let newElementSize = newValue["size"];
  109. let newElementMd5 = newValue["md5"];
  110. /*cb("找到:newAssets:" + idx);
  111. cb("找到:newValueKey:" + newKey);
  112. cb("找到:newElementSize:" + newElementSize);
  113. cb("找到:newElementMd5:" + newElementMd5);*/
  114. let isFind = false;
  115. for (var oldKey in oldAssets) {
  116. let oldValue = oldAssets[oldKey];
  117. let oldElementSize = oldValue["size"];
  118. let oldElementMd5 = oldValue["md5"];
  119. if (oldKey == newKey && oldElementSize == newElementSize && oldElementMd5 == newElementMd5) {
  120. isFind = true;
  121. break;
  122. }
  123. }
  124. if (!isFind) {//没找到
  125. //cb("找到差异文件:" + newKey + " size:" + newElementSize + " md5:" + newElementMd5);
  126. dataArray.push(newKey);
  127. }
  128. idx++;
  129. }
  130. cb("筛选成功,共 " + dataArray.length + "个文件。");
  131. for (let i = 0; i < dataArray.length; i++) {
  132. let srcPath = Editor.Project.path + "\\build\\jsb-link\\" + dataArray[i];
  133. let destPath = FinalPath + "\\" + dataArray[i];
  134. // cb("拷贝 " + srcPath + " 到 " + destPath);
  135. self.copyFile(srcPath, destPath);
  136. }
  137. cb("" + dataArray.length +" 个文件已成功拷贝至:" + FinalPath);
  138. let srcProjectFile = Editor.Project.path + "\\build\\jsb-link\\pack_assets_"+ newAssetsVersion + "\\project.manifest";
  139. let destProjectFile = FinalPath+"\\project.manifest";
  140. self.copyFile(srcProjectFile,destProjectFile);
  141. cb("" + srcProjectFile +" 文件已成功拷贝至:" + destProjectFile);
  142. let srcVersionFile = Editor.Project.path + "\\build\\jsb-link\\pack_assets_"+ newAssetsVersion + "\\version.manifest";
  143. let destVersionFile = FinalPath+"\\version.manifest";
  144. self.copyFile(srcVersionFile, destVersionFile);
  145. cb("" + srcVersionFile +" 文件已成功拷贝至:" + destVersionFile);
  146. } else {
  147. cb("文件打开失败,请重新操作!");
  148. }
  149. }, 100);
  150. },
  151. }