publish_bdgame.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // v1.2.0
  2. // publish 2.x 也是用这个文件,需要做兼容
  3. let isPublish2 = process.argv[2].includes("publish_bdgame.js") && process.argv[3].includes("--evn=publish2");
  4. // 获取Node插件和工作路径
  5. let ideModuleDir, workSpaceDir;
  6. if (isPublish2) {
  7. //是否使用IDE自带的node环境和插件,设置false后,则使用自己环境(使用命令行方式执行)
  8. const useIDENode = process.argv[0].indexOf("LayaAir") > -1 ? true : false;
  9. ideModuleDir = useIDENode ? process.argv[1].replace("gulp\\bin\\gulp.js", "").replace("gulp/bin/gulp.js", "") : "";
  10. workSpaceDir = useIDENode ? process.argv[2].replace("--gulpfile=", "").replace("\\.laya\\publish_bdgame.js", "").replace("/.laya/publish_bdgame.js", "") + "/" : "./../";
  11. } else {
  12. ideModuleDir = global.ideModuleDir;
  13. workSpaceDir = global.workSpaceDir;
  14. }
  15. //引用插件模块
  16. const gulp = require(ideModuleDir + "gulp");
  17. const fs = require("fs");
  18. const path = require("path");
  19. const crypto = require("crypto");
  20. const childProcess = require("child_process");
  21. const del = require(ideModuleDir + "del");
  22. const revCollector = require(ideModuleDir + 'gulp-rev-collector');
  23. let commandSuffix = ".cmd";
  24. let prevTasks = ["packfile"];
  25. if (isPublish2) {
  26. prevTasks = "";
  27. }
  28. let
  29. config,
  30. platform,
  31. releaseDir;
  32. let isGlobalCli = true;
  33. let versionCon; // 版本管理version.json
  34. // 应该在publish中的,但是为了方便发布2.0及IDE 1.x,放在这里修改
  35. gulp.task("preCreate_BD", prevTasks, function() {
  36. if (isPublish2) {
  37. let pubsetPath = path.join(workSpaceDir, ".laya", "pubset.json");
  38. let content = fs.readFileSync(pubsetPath, "utf8");
  39. let pubsetJson = JSON.parse(content);
  40. platform = "bdgame";
  41. releaseDir = path.join(workSpaceDir, "release", platform).replace(/\\/g, "/");
  42. config = pubsetJson[3];
  43. } else {
  44. platform = global.platform;
  45. releaseDir = global.releaseDir;
  46. config = global.config;
  47. }
  48. // 如果不是百度小游戏
  49. if (platform !== "bdgame") {
  50. return;
  51. }
  52. if (process.platform === "darwin") {
  53. commandSuffix = "";
  54. }
  55. let copyLibsList = [`${workSpaceDir}/bin/libs/laya.bdmini.js`];
  56. var stream = gulp.src(copyLibsList, { base: `${workSpaceDir}/bin` });
  57. return stream.pipe(gulp.dest(releaseDir));
  58. });
  59. gulp.task("copyPlatformFile_BD", ["preCreate_BD"], function() {
  60. // 如果不是百度小游戏
  61. if (platform !== "bdgame") {
  62. return;
  63. }
  64. let adapterPath = path.join(ideModuleDir, "../", "out", "layarepublic", "LayaAirProjectPack", "lib", "data", "bdfiles");
  65. // 如果新建项目时已经点击了"微信/百度小游戏bin目录快速调试",不再拷贝
  66. let isHadBdFiles =
  67. fs.existsSync(path.join(workSpaceDir, "bin", "game.js")) &&
  68. fs.existsSync(path.join(workSpaceDir, "bin", "game.json")) &&
  69. fs.existsSync(path.join(workSpaceDir, "bin", "project.swan.json")) &&
  70. fs.existsSync(path.join(workSpaceDir, "bin", "swan-game-adapter.js"));
  71. if (isHadBdFiles) {
  72. return;
  73. }
  74. let stream = gulp.src(adapterPath + "/*.*");
  75. return stream.pipe(gulp.dest(releaseDir));
  76. });
  77. gulp.task("modifyFile_BD", ["copyPlatformFile_BD"], function() {
  78. // 如果不是百度小游戏
  79. if (platform !== "bdgame") {
  80. return;
  81. }
  82. if (config.version) {
  83. let versionPath = releaseDir + "/version.json";
  84. versionCon = fs.readFileSync(versionPath, "utf8");
  85. versionCon = JSON.parse(versionCon);
  86. }
  87. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  88. // 百度小游戏项目,修改index.js
  89. let filePath = path.join(releaseDir, indexJsStr);
  90. if (!fs.existsSync(filePath)) {
  91. return;
  92. }
  93. let fileContent = fs.readFileSync(filePath, "utf8");
  94. fileContent = fileContent.replace(/loadLib(\(['"])/gm, "require$1./");
  95. fs.writeFileSync(filePath, fileContent, "utf8");
  96. });
  97. // 开放域的情况下,合并game.js和index.js,并删除game.js
  98. gulp.task("openData_BD", ["modifyFile_BD"], function (cb) {
  99. // 如果不是百度小游戏
  100. if (platform !== "bdgame") {
  101. return cb();
  102. }
  103. if (config.openDataZone) {
  104. let versionCon;
  105. if (config.version) {
  106. let versionPath = releaseDir + "/version.json";
  107. versionCon = fs.readFileSync(versionPath, "utf8");
  108. versionCon = JSON.parse(versionCon);
  109. }
  110. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  111. let indexPath = path.join(releaseDir, indexJsStr);
  112. let indexjs = readFile(indexPath);
  113. let gamejs = readFile(releaseDir + "/game.js");
  114. if (gamejs && indexjs) {
  115. gamejs = gamejs.replace(`require("index.js")`, indexjs);
  116. fs.writeFileSync(indexPath, gamejs, 'utf-8');
  117. }
  118. return cb();
  119. }
  120. cb();
  121. });
  122. function readFile(path) {
  123. if (fs.existsSync(path)) {
  124. return fs.readFileSync(path, "utf-8");
  125. }
  126. return null;
  127. }
  128. gulp.task("version_BD", ["openData_BD"], function() {
  129. // 如果不是百度小游戏
  130. if (platform !== "bdgame") {
  131. return;
  132. }
  133. if (config.version) {
  134. let versionPath = releaseDir + "/version.json";
  135. let gameJSPath = releaseDir + "/game.js";
  136. let srcList = [versionPath, gameJSPath];
  137. return gulp.src(srcList)
  138. .pipe(revCollector())
  139. .pipe(gulp.dest(releaseDir));
  140. }
  141. });
  142. gulp.task("optimizeOpen_BD", ["version_BD"], function(cb) {
  143. // 如果不是百度小游戏
  144. if (platform !== "bdgame") {
  145. return cb();
  146. }
  147. let bdOptimize = config.bdOptimize;
  148. if (!bdOptimize || !bdOptimize.useOptimizeOpen) {
  149. return cb();
  150. }
  151. // 首屏加载优化(秒开),修改game.json
  152. let filePath = path.join(releaseDir, "game.json");
  153. if (!fs.existsSync(filePath)) {
  154. return cb();
  155. }
  156. let fileContent = fs.readFileSync(filePath, "utf8");
  157. let fileConObj = JSON.parse(fileContent);
  158. if (bdOptimize.preloadRes) {
  159. fileConObj.preloadResources = bdOptimize.preloadResList;
  160. } else {
  161. delete fileConObj.preloadResources;
  162. }
  163. fs.writeFileSync(filePath, JSON.stringify(fileConObj, null, 4), "utf8");
  164. return cb();
  165. });
  166. gulp.task("buildBDProj", ["optimizeOpen_BD"], function() {
  167. console.log("all tasks completed");
  168. });