publish_alipaygame.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // v1.4.0
  2. // publish 2.x 也是用这个文件,需要做兼容
  3. let isPublish2 = process.argv[2].includes("publish_alipaygame.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_alipaygame.js", "").replace("/.laya/publish_alipaygame.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 revCollector = require(ideModuleDir + 'gulp-rev-collector');
  20. let commandSuffix = ".cmd";
  21. let copyLibsTask = ["copyLibsJsFile"];
  22. let packfiletask = ["packfile"];
  23. if (isPublish2) {
  24. copyLibsTask = "";
  25. packfiletask = ["copyPlatformFile_Alipay"];
  26. }
  27. let
  28. config,
  29. platform,
  30. releaseDir;
  31. let versionCon; // 版本管理version.json
  32. let layarepublicPath = path.join(ideModuleDir, "../", "code", "layarepublic");
  33. if (!fs.existsSync(layarepublicPath)) {
  34. layarepublicPath = path.join(ideModuleDir, "../", "out", "layarepublic");
  35. }
  36. // 应该在publish中的,但是为了方便发布2.0及IDE 1.x,放在这里修改
  37. gulp.task("preCreate_Alipay", copyLibsTask, function() {
  38. if (isPublish2) {
  39. let pubsetPath = path.join(workSpaceDir, ".laya", "pubset.json");
  40. let content = fs.readFileSync(pubsetPath, "utf8");
  41. let pubsetJson = JSON.parse(content);
  42. platform = "Alipaygame";
  43. releaseDir = path.join(workSpaceDir, "release", platform).replace(/\\/g, "/");
  44. config = pubsetJson[8];
  45. } else {
  46. platform = global.platform;
  47. releaseDir = global.releaseDir;
  48. config = global.config;
  49. }
  50. // 如果不是Alipay快游戏
  51. if (platform !== "Alipaygame") {
  52. return;
  53. }
  54. if (process.platform === "darwin") {
  55. commandSuffix = "";
  56. }
  57. let copyLibsList = [`${workSpaceDir}/bin/libs/laya.Alipaymini.js`];
  58. var stream = gulp.src(copyLibsList, { base: `${workSpaceDir}/bin` });
  59. return stream.pipe(gulp.dest(releaseDir));
  60. });
  61. gulp.task("copyPlatformFile_Alipay", ["preCreate_Alipay"], function() {
  62. // 如果不是Alipay快游戏
  63. if (platform !== "Alipaygame") {
  64. return;
  65. }
  66. let isHasPublish =
  67. fs.existsSync(path.join(releaseDir, "game.js")) &&
  68. fs.existsSync(path.join(releaseDir, "game.json")) &&
  69. fs.existsSync(path.join(releaseDir, "my-adapter.js"));
  70. if (isHasPublish) {
  71. return;
  72. }
  73. let AlipayAdapterPath = path.join(layarepublicPath, "LayaAirProjectPack", "lib", "data", "Alipayfiles");
  74. let copyLibsList = [`${AlipayAdapterPath}/**/*.*`];
  75. var stream = gulp.src(copyLibsList);
  76. return stream.pipe(gulp.dest(releaseDir));
  77. });
  78. gulp.task("modifyFile_Alipay", packfiletask, function() {
  79. // 如果不是Alipay快游戏
  80. if (platform !== "Alipaygame") {
  81. return;
  82. }
  83. // 修改game.json文件
  84. let gameJsonPath = path.join(releaseDir, "game.json");
  85. let content = fs.readFileSync(gameJsonPath, "utf8");
  86. let conJson = JSON.parse(content);
  87. conJson.screenOrientation = config.AlipayInfo.screenOrientation;
  88. content = JSON.stringify(conJson, null, 4);
  89. fs.writeFileSync(gameJsonPath, content, "utf8");
  90. // 修改game.js
  91. let filePath = path.join(releaseDir, "game.js");
  92. // 这个地方,1.x IDE和2.x IDE 不一致
  93. let fileContent = `require("./my-adapter.js");
  94. require("./libs/laya.Alipaymini.js");\nrequire("./index.js");`;
  95. fs.writeFileSync(filePath, fileContent, "utf8");
  96. if (config.version || config.enableVersion) {
  97. let versionPath = releaseDir + "/version.json";
  98. versionCon = fs.readFileSync(versionPath, "utf8");
  99. versionCon = JSON.parse(versionCon);
  100. }
  101. // 修改index.js
  102. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  103. let indexFilePath = path.join(releaseDir, indexJsStr);
  104. if (!fs.existsSync(indexFilePath)) {
  105. return;
  106. }
  107. let indexFileContent = fs.readFileSync(indexFilePath, "utf8");
  108. indexFileContent = indexFileContent.replace(/loadLib(\(['"])/gm, "require$1./");
  109. fs.writeFileSync(indexFilePath, indexFileContent, "utf8");
  110. })
  111. gulp.task("version_Alipay", ["modifyFile_Alipay"], function () {
  112. // 如果不是Alipay快游戏
  113. if (platform !== "Alipaygame") {
  114. return;
  115. }
  116. if (config.version) {
  117. let versionPath = releaseDir + "/version.json";
  118. let gameJSPath = releaseDir + "/game.js";
  119. let srcList = [versionPath, gameJSPath];
  120. return gulp.src(srcList)
  121. .pipe(revCollector())
  122. .pipe(gulp.dest(releaseDir));
  123. }
  124. });
  125. gulp.task("buildAlipayProj", ["version_Alipay"], function() {
  126. console.log("all tasks completed");
  127. });