hook.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var Fs = require("fs");
  3. var Path = require("path");
  4. var inject_script = `
  5. (function () {
  6. if (typeof window.jsb === 'object') {
  7. var hotUpdateSearchPaths = localStorage.getItem('HotUpdateSearchPaths');
  8. if (hotUpdateSearchPaths) {
  9. var paths = JSON.parse(hotUpdateSearchPaths);
  10. jsb.fileUtils.setSearchPaths(paths);
  11. var fileList = [];
  12. var storagePath = paths[0] || '';
  13. var tempPath = storagePath + '_temp/';
  14. var baseOffset = tempPath.length;
  15. if (jsb.fileUtils.isDirectoryExist(tempPath) && !jsb.fileUtils.isFileExist(tempPath + 'project.manifest.temp')) {
  16. jsb.fileUtils.listFilesRecursively(tempPath, fileList);
  17. fileList.forEach(srcPath => {
  18. var relativePath = srcPath.substr(baseOffset);
  19. var dstPath = storagePath + relativePath;
  20. if (srcPath[srcPath.length] == '/') {
  21. jsb.fileUtils.createDirectory(dstPath)
  22. }
  23. else {
  24. if (jsb.fileUtils.isFileExist(dstPath)) {
  25. jsb.fileUtils.removeFile(dstPath)
  26. }
  27. jsb.fileUtils.renameFile(srcPath, dstPath);
  28. }
  29. })
  30. jsb.fileUtils.removeDirectory(tempPath);
  31. }
  32. }
  33. }
  34. })();
  35. `;
  36. exports.onAfterBuild = function (options, result) {
  37. var root = Path.join(Editor.Project.path, 'build/', options.outputName, 'assets');
  38. var url = Path.join(root, "main.js");
  39. Fs.readFile(url, "utf8", function (err, data) {
  40. if (err) {
  41. throw err;
  42. }
  43. var newStr = inject_script + data;
  44. Fs.writeFile(url, newStr, function (error) {
  45. if (err) {
  46. throw err;
  47. }
  48. Editor.log("SearchPath updated in built main.js for hot update");
  49. });
  50. });
  51. }