File size: 4,720 Bytes
4a5ef51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node

import {spawn, spawnSync} from "node:child_process"
import {setTimeout as delay} from "node:timers/promises"
import {expoDeviceName, resolveAndroidPhoneTarget} from "./resolve-android-phone.mjs"

const port = Number(process.env.EXPO_DEV_SERVER_PORT || process.env.RCT_METRO_PORT || 8081)
const metroHost = process.env.EXPO_DEV_SERVER_HOST || "localhost"
const scheme = process.env.EXPO_DEV_CLIENT_SCHEME || "exp+mentra-sdk-rn-example"
const appId = process.env.EXPO_ANDROID_APP_ID || "com.mentra.bluetoothsdk.example.reactnative"
const metroUrl = `http://${metroHost}:${port}`
const devClientUrl = `${scheme}://expo-development-client/?url=${encodeURIComponent(metroUrl)}`

process.env.REACT_NATIVE_PACKAGER_HOSTNAME ||= metroHost

const args = new Set(process.argv.slice(2))
if (args.has("-h") || args.has("--help")) {
  console.log(`

Usage:

  bun run android:dev



Starts Metro first, installs/runs the Android development build on a connected

phone (never Mentra Live glasses), without spawning a second bundler, then

explicitly opens the Expo dev-client URL.



Environment overrides:

  EXPO_DEV_SERVER_PORT       Metro port. Defaults to 8081.

  EXPO_DEV_SERVER_HOST       Metro host in the dev-client URL. Defaults to localhost.

  EXPO_DEV_CLIENT_SCHEME     Dev-client scheme. Defaults to exp+mentra-sdk-rn-example.

  EXPO_ANDROID_APP_ID        Android app id. Defaults to com.mentra.bluetoothsdk.example.reactnative.

  ANDROID_SERIAL             Force a specific phone serial when multiple phones are connected.

  ALLOW_MENTRA_LIVE=1        Allow targeting Mentra Live (not recommended for this example).

`)
  process.exit(0)
}

function run(command, commandArgs, options = {}) {
  const result = spawnSync(command, commandArgs, {
    stdio: "inherit",
    encoding: "utf8",
    ...options,
  })

  if (result.error) {
    throw result.error
  }

  if (result.status !== 0) {
    throw new Error(`${command} ${commandArgs.join(" ")} failed with exit code ${result.status}`)
  }
}

async function isMetroRunning() {
  const controller = new AbortController()
  const timeout = setTimeout(() => controller.abort(), 1000)

  try {
    const response = await fetch(`${metroUrl}/status`, {signal: controller.signal})
    const text = await response.text()
    return text.includes("packager-status:running")
  } catch {
    return false
  } finally {
    clearTimeout(timeout)
  }
}

async function waitForMetro() {
  const deadline = Date.now() + 90_000

  while (Date.now() < deadline) {
    if (await isMetroRunning()) {
      return
    }
    await delay(500)
  }

  throw new Error(`Metro did not become ready at ${metroUrl} within 90 seconds.`)
}

async function main() {
  const target = resolveAndroidPhoneTarget()
  const serial = target.serial
  const deviceName = expoDeviceName(target)
  let metroProcess = null

  console.log(`Using Android phone: ${deviceName} (serial ${serial})`)

  if (await isMetroRunning()) {
    console.log(`Metro is already running at ${metroUrl}`)
  } else {
    console.log(`Starting Metro at ${metroUrl}`)
    metroProcess = spawn(
      "bunx",
      ["expo", "start", "--dev-client", "--host", "localhost", "--port", String(port)],
      {
        stdio: "inherit",
        env: process.env,
      },
    )

    metroProcess.on("exit", (code) => {
      if (code !== 0 && code !== null) {
        console.error(`Metro exited with code ${code}`)
        process.exitCode = code
      }
    })

    await waitForMetro()
  }

  console.log(`Forwarding Android device localhost:${port} to this Mac`)
  run("adb", ["-s", serial, "reverse", `tcp:${port}`, `tcp:${port}`])

  console.log("Installing and launching the Android development build")
  run("bunx", ["expo", "run:android", "--no-bundler", "--device", deviceName])

  console.log(`Opening Expo dev-client URL: ${devClientUrl}`)
  run("adb", ["-s", serial, "shell", "am", "force-stop", appId])
  run("adb", [
    "-s",
    serial,
    "shell",
    "am",
    "start",
    "-a",
    "android.intent.action.VIEW",
    "-d",
    devClientUrl,
    "-p",
    appId,
  ])

  if (!metroProcess) {
    console.log("Using existing Metro process; android:dev is done.")
    return
  }

  console.log("Metro is running. Press Ctrl-C to stop the dev server.")
  await new Promise((resolve) => {
    const stop = () => {
      metroProcess.kill("SIGINT")
      resolve()
    }

    process.on("SIGINT", stop)
    process.on("SIGTERM", stop)
    metroProcess.on("exit", resolve)
  })
}

main().catch((error) => {
  console.error(error.message)
  process.exit(1)
})