fix-wechat-landscape.mjs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { access, readFile, writeFile } from 'node:fs/promises';
  2. import { constants } from 'node:fs';
  3. import path from 'node:path';
  4. import { fileURLToPath } from 'node:url';
  5. const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
  6. const level1Scene = {
  7. url: 'db://assets/scenes/Level1.scene',
  8. uuid: '07c80a1c-b246-4b95-9712-3e19f8dc2084'
  9. };
  10. const changed = [];
  11. const skipped = [];
  12. async function exists(file) {
  13. try {
  14. await access(file, constants.F_OK);
  15. return true;
  16. } catch {
  17. return false;
  18. }
  19. }
  20. function isObject(value) {
  21. return value !== null && typeof value === 'object' && !Array.isArray(value);
  22. }
  23. function patchWechatBuildJson(json) {
  24. json.deviceOrientation = 'landscape';
  25. json.resizable = true;
  26. delete json.subpackages;
  27. }
  28. function patchBuildOptions(value) {
  29. if (Array.isArray(value)) {
  30. for (const item of value) patchBuildOptions(item);
  31. return;
  32. }
  33. if (!isObject(value)) return;
  34. for (const [key, child] of Object.entries(value)) {
  35. if (key === 'orientation') value[key] = 'landscape';
  36. if (key === 'separateEngine') value[key] = true;
  37. if (key === 'experimentalEraseModules') value[key] = true;
  38. if (key === 'useSplashScreen') value[key] = false;
  39. if (key === 'sourceMaps') value[key] = false;
  40. if (key === 'startScene') value[key] = level1Scene.uuid;
  41. if (key === 'includeModules' && isObject(child)) {
  42. if ('physics' in child) child.physics = 'off';
  43. if ('physics-2d' in child) child['physics-2d'] = 'off';
  44. if ('gfx-webgl2' in child) child['gfx-webgl2'] = 'off';
  45. }
  46. if (key === 'scenes' && Array.isArray(child)) {
  47. value.scenes = [level1Scene];
  48. continue;
  49. }
  50. patchBuildOptions(child);
  51. }
  52. }
  53. async function updateJson(relativePath, patcher) {
  54. const file = path.join(root, relativePath);
  55. if (!(await exists(file))) {
  56. skipped.push(relativePath);
  57. return;
  58. }
  59. const before = await readFile(file, 'utf8');
  60. const json = JSON.parse(before);
  61. patcher(json);
  62. const after = `${JSON.stringify(json, null, 2)}\n`;
  63. if (after !== before) {
  64. await writeFile(file, after, 'utf8');
  65. changed.push(relativePath);
  66. }
  67. }
  68. await updateJson('build-templates/wechatgame/game.json', patchWechatBuildJson);
  69. await updateJson('build/wechatgame/game.json', patchWechatBuildJson);
  70. await updateJson('profiles/v2/packages/wechatgame.json', patchBuildOptions);
  71. await updateJson('profiles/v2/packages/builder.json', patchBuildOptions);
  72. if (changed.length) {
  73. console.log(`Fixed WeChat landscape/minigame settings:\n- ${changed.join('\n- ')}`);
  74. } else {
  75. console.log('WeChat landscape/minigame settings already fixed.');
  76. }
  77. if (skipped.length) {
  78. console.log(`Skipped missing files:\n- ${skipped.join('\n- ')}`);
  79. }