Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

chore(mobile): add dev network and iOS preparation scripts

Richie McIlroy67c454ca7d26
diff --git a/apps/mobile/scripts/mobile-development-network.mjs b/apps/mobile/scripts/mobile-development-network.mjs
new file mode 100644
index 000000000..a66bc3277
--- /dev/null
+++ b/apps/mobile/scripts/mobile-development-network.mjs
@@ -0,0 +1,59 @@
+import { networkInterfaces } from "node:os";
+
+const preferredInterfaceNames = ["en0", "en1", "en2"];
+const virtualInterfacePattern =
+ /^(awdl|bridge|docker|llw|lo|tailscale|utun|vbox|vmnet)/i;
+
+export const isPrivateIpv4 = ({ address, family, internal }) =>
+ (family === "IPv4" || family === 4) &&
+ !internal &&
+ (address.startsWith("10.") ||
+ address.startsWith("192.168.") ||
+ /^172\.(1[6-9]|2\d|3[01])\./.test(address));
+
+const privateAddressFor = (interfaces, name) =>
+ interfaces[name]?.find(isPrivateIpv4)?.address;
+
+export const findLanAddress = (interfaces = networkInterfaces()) => {
+ for (const name of preferredInterfaceNames) {
+ const address = privateAddressFor(interfaces, name);
+ if (address) return address;
+ }
+
+ for (const [name, addresses] of Object.entries(interfaces)) {
+ if (virtualInterfacePattern.test(name)) continue;
+ const address = addresses?.find(isPrivateIpv4)?.address;
+ if (address) return address;
+ }
+};
+
+const validatedPort = (value) => {
+ const port = Number(value);
+ if (!Number.isInteger(port) || port < 1 || port > 65_535) {
+ throw new Error(`Invalid local API port: ${value}`);
+ }
+ return String(port);
+};
+
+export const resolvePhysicalApiBaseUrl = ({
+ env = process.env,
+ interfaces = networkInterfaces(),
+} = {}) => {
+ if (env.CAP_MOBILE_DEVICE_API_URL) {
+ return env.CAP_MOBILE_DEVICE_API_URL;
+ }
+
+ const lanAddress = env.CAP_MOBILE_LAN_IP ?? findLanAddress(interfaces);
+ if (lanAddress) {
+ const port = validatedPort(env.CAP_MOBILE_LOCAL_API_PORT ?? "3000");
+ return `http://${lanAddress}:${port}`;
+ }
+
+ if (env.EXPO_PUBLIC_CAP_WEB_URL) {
+ return env.EXPO_PUBLIC_CAP_WEB_URL;
+ }
+
+ throw new Error(
+ "Unable to find the Mac LAN address. Set CAP_MOBILE_DEVICE_API_URL explicitly.",
+ );
+};
diff --git a/apps/mobile/scripts/prepare-ios-development.mjs b/apps/mobile/scripts/prepare-ios-development.mjs
new file mode 100644
index 000000000..7f5c50a17
--- /dev/null
+++ b/apps/mobile/scripts/prepare-ios-development.mjs
@@ -0,0 +1,40 @@
+import { spawnSync } from "node:child_process";
+import { existsSync, readFileSync } from "node:fs";
+import { join } from "node:path";
+
+const dryRun = process.env.CAP_MOBILE_DRY_RUN === "1";
+
+const run = (command, args) => {
+ if (dryRun) {
+ console.log(`${command} ${args.join(" ")}`);
+ return;
+ }
+
+ const result = spawnSync(command, args, {
+ stdio: "inherit",
+ env: process.env,
+ });
+ if (result.error) throw result.error;
+ if (result.status !== 0) process.exit(result.status ?? 1);
+};
+
+run("pnpm", ["exec", "expo", "prebuild", "--platform", "ios", "--no-install"]);
+
+const podfileLockPath = join(process.cwd(), "ios", "Podfile.lock");
+const podfileLock = existsSync(podfileLockPath)
+ ? readFileSync(podfileLockPath, "utf8")
+ : "";
+const podsManifestPath = join(process.cwd(), "ios", "Pods", "Manifest.lock");
+const podsManifest = existsSync(podsManifestPath)
+ ? readFileSync(podsManifestPath, "utf8")
+ : "";
+const needsPodInstall =
+ !podfileLock ||
+ podfileLock !== podsManifest ||
+ podfileLock.includes("React-Core-prebuilt") ||
+ podfileLock.includes("ReactNativeDependencies") ||
+ !podfileLock.includes("ExpoAppleAuthentication");
+
+if (needsPodInstall) {
+ run("pod", ["install", "--project-directory=ios"]);
+}
diff --git a/apps/mobile/scripts/run-ios-device.mjs b/apps/mobile/scripts/run-ios-device.mjs
index 095a61a2a..f7d701b96 100644
--- a/apps/mobile/scripts/run-ios-device.mjs
+++ b/apps/mobile/scripts/run-ios-device.mjs
@@ -1,36 +1,11 @@
import { spawnSync } from "node:child_process";
-import { networkInterfaces } from "node:os";
-
-const isPrivateIpv4 = ({ address, family, internal }) =>
- family === "IPv4" &&
- !internal &&
- (address.startsWith("10.") ||
- address.startsWith("192.168.") ||
- /^172\.(1[6-9]|2\d|3[01])\./.test(address));
-
-const findLanAddress = () => {
- const interfaces = networkInterfaces();
- for (const name of ["en0", "en1"]) {
- const address = interfaces[name]?.find(isPrivateIpv4)?.address;
- if (address) return address;
- }
-
- return Object.values(interfaces)
- .flat()
- .find((address) => address && isPrivateIpv4(address))?.address;
-};
+import {
+ findLanAddress,
+ resolvePhysicalApiBaseUrl,
+} from "./mobile-development-network.mjs";
const lanAddress = process.env.CAP_MOBILE_LAN_IP ?? findLanAddress();
-const localApiPort = process.env.CAP_MOBILE_LOCAL_API_PORT ?? "3000";
-const apiBaseUrl =
- process.env.EXPO_PUBLIC_CAP_WEB_URL ??
- (lanAddress ? `http://${lanAddress}:${localApiPort}` : null);
-
-if (!apiBaseUrl) {
- throw new Error(
- "Unable to find the Mac LAN address. Set EXPO_PUBLIC_CAP_WEB_URL explicitly.",
- );
-}
+const apiBaseUrl = resolvePhysicalApiBaseUrl();
const forwardedArgs = process.argv.slice(2);
if (forwardedArgs[0] === "--") forwardedArgs.shift();
@@ -48,6 +23,9 @@ const result = spawnSync("pnpm", command, {
...process.env,
CAP_MOBILE_DISABLE_ASSOCIATED_DOMAINS: "1",
EXPO_PUBLIC_CAP_WEB_URL: apiBaseUrl,
+ ...(lanAddress && !process.env.REACT_NATIVE_PACKAGER_HOSTNAME
+ ? { REACT_NATIVE_PACKAGER_HOSTNAME: lanAddress }
+ : {}),
},
});