|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import cp from "node:child_process"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | + |
| 6 | +const REACT_NATIVE_VERSION = "0.79.6"; |
| 7 | +const ROOT_PATH = path.join(import.meta.dirname, ".."); |
| 8 | +const APP_PATH = path.join(ROOT_PATH, "apps", "macos-test-app"); |
| 9 | +const HOST_PACKAGE_PATH = path.join(ROOT_PATH, "packages", "host"); |
| 10 | + |
| 11 | +function exec(command: string, args: string[], options: cp.SpawnOptions = {}) { |
| 12 | + const { status } = cp.spawnSync(command, args, { |
| 13 | + stdio: "inherit", |
| 14 | + ...options, |
| 15 | + }); |
| 16 | + assert.equal(status, 0, `Failed to execute '${command}'`); |
| 17 | +} |
| 18 | + |
| 19 | +async function deletePreviousApp() { |
| 20 | + if (fs.existsSync(APP_PATH)) { |
| 21 | + console.log("Deleting existing app directory"); |
| 22 | + await fs.promises.rm(APP_PATH, { recursive: true, force: true }); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +async function initializeReactNativeTemplate() { |
| 27 | + console.log("Initializing community template"); |
| 28 | + exec("npx", [ |
| 29 | + "@react-native-community/cli", |
| 30 | + "init", |
| 31 | + "MacOSTestApp", |
| 32 | + "--skip-install", |
| 33 | + "--skip-git-init", |
| 34 | + // "--platform-name", |
| 35 | + // "react-native-macos", |
| 36 | + "--version", |
| 37 | + REACT_NATIVE_VERSION, |
| 38 | + "--directory", |
| 39 | + APP_PATH, |
| 40 | + ]); |
| 41 | + |
| 42 | + // Clean up |
| 43 | + const CLEANUP_PATHS = ["ios", "android", "__tests__"]; |
| 44 | + |
| 45 | + for (const cleanupPath of CLEANUP_PATHS) { |
| 46 | + await fs.promises.rm(path.join(APP_PATH, cleanupPath), { |
| 47 | + recursive: true, |
| 48 | + force: true, |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function installDependencies() { |
| 54 | + console.log("Installing dependencies"); |
| 55 | + exec( |
| 56 | + "npm", |
| 57 | + [ |
| 58 | + "install", |
| 59 | + "--save-dev", |
| 60 | + "--prefer-offline", |
| 61 | + "--install-links", |
| 62 | + "react-native-macos-init", |
| 63 | + path.relative(APP_PATH, HOST_PACKAGE_PATH), |
| 64 | + ], |
| 65 | + { |
| 66 | + cwd: APP_PATH, |
| 67 | + }, |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +function initializeReactNativeMacOSTemplate() { |
| 72 | + console.log("Initializing react-native-macos template"); |
| 73 | + exec("npx", ["react-native-macos-init"], { |
| 74 | + cwd: APP_PATH, |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +async function patchPodfile() { |
| 79 | + console.log("Patching Podfile"); |
| 80 | + // Patch Podfile as per https://github.com/microsoft/react-native-macos/issues/2723#issuecomment-3392930688 |
| 81 | + const podfilePath = path.join(APP_PATH, "macos", "Podfile"); |
| 82 | + const podfileContents = await fs.promises.readFile(podfilePath, "utf8"); |
| 83 | + const unwantedLines = [ |
| 84 | + "require_relative '../node_modules/react-native-macos/scripts/react_native_pods'", |
| 85 | + "require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'", |
| 86 | + "require_relative '../node_modules/react-native-macos/scripts/cocoapods/autolinking'", |
| 87 | + ]; |
| 88 | + const podfilePatched = [ |
| 89 | + "require_relative '../node_modules/react-native-macos/scripts/cocoapods/autolinking'", |
| 90 | + ...podfileContents |
| 91 | + .split("\n") |
| 92 | + .filter((line) => !unwantedLines.includes(line)), |
| 93 | + ]; |
| 94 | + |
| 95 | + await fs.promises.writeFile(podfilePath, podfilePatched.join("\n"), "utf8"); |
| 96 | +} |
| 97 | + |
| 98 | +function installCocoapods() { |
| 99 | + console.log("Installing cocoapods"); |
| 100 | + exec("pod", ["install", "--project-directory=macos"], { |
| 101 | + cwd: APP_PATH, |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +await deletePreviousApp(); |
| 106 | +await initializeReactNativeTemplate(); |
| 107 | +installDependencies(); |
| 108 | +initializeReactNativeMacOSTemplate(); |
| 109 | +await patchPodfile(); |
| 110 | +installCocoapods(); |
0 commit comments