import { access, readFile, writeFile } from 'node:fs/promises'; import { constants } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const level1Scene = { url: 'db://assets/scenes/Level1.scene', uuid: '07c80a1c-b246-4b95-9712-3e19f8dc2084' }; const changed = []; const skipped = []; async function exists(file) { try { await access(file, constants.F_OK); return true; } catch { return false; } } function isObject(value) { return value !== null && typeof value === 'object' && !Array.isArray(value); } function patchWechatBuildJson(json) { json.deviceOrientation = 'landscape'; json.resizable = true; delete json.subpackages; } function patchBuildOptions(value) { if (Array.isArray(value)) { for (const item of value) patchBuildOptions(item); return; } if (!isObject(value)) return; for (const [key, child] of Object.entries(value)) { if (key === 'orientation') value[key] = 'landscape'; if (key === 'separateEngine') value[key] = true; if (key === 'experimentalEraseModules') value[key] = true; if (key === 'useSplashScreen') value[key] = false; if (key === 'sourceMaps') value[key] = false; if (key === 'startScene') value[key] = level1Scene.uuid; if (key === 'includeModules' && isObject(child)) { if ('physics' in child) child.physics = 'off'; if ('physics-2d' in child) child['physics-2d'] = 'off'; if ('gfx-webgl2' in child) child['gfx-webgl2'] = 'off'; } if (key === 'scenes' && Array.isArray(child)) { value.scenes = [level1Scene]; continue; } patchBuildOptions(child); } } async function updateJson(relativePath, patcher) { const file = path.join(root, relativePath); if (!(await exists(file))) { skipped.push(relativePath); return; } const before = await readFile(file, 'utf8'); const json = JSON.parse(before); patcher(json); const after = `${JSON.stringify(json, null, 2)}\n`; if (after !== before) { await writeFile(file, after, 'utf8'); changed.push(relativePath); } } await updateJson('build-templates/wechatgame/game.json', patchWechatBuildJson); await updateJson('build/wechatgame/game.json', patchWechatBuildJson); await updateJson('profiles/v2/packages/wechatgame.json', patchBuildOptions); await updateJson('profiles/v2/packages/builder.json', patchBuildOptions); if (changed.length) { console.log(`Fixed WeChat landscape/minigame settings:\n- ${changed.join('\n- ')}`); } else { console.log('WeChat landscape/minigame settings already fixed.'); } if (skipped.length) { console.log(`Skipped missing files:\n- ${skipped.join('\n- ')}`); }