# Expo Run Button Bootstrap

This is the canonical bootstrap contract for connecting an Expo app to the
Codex app action bar.

When an Expo project does not already have a Codex run entrypoint:

1. Create one project-local `script/build_and_run.sh`.
2. Make it executable.
3. Keep the default path as the fastest low-risk local run loop: `expo start`.
4. Write `.codex/environments/environment.toml` so the Codex app exposes a
   `Run` action wired to that script.
5. Add more action buttons only when they match the project and the user's ask.

## `script/build_and_run.sh`

Use this shape as the default Expo run script. Keep the dev server in the
foreground: the Codex action terminal owns the running process and shows Metro /
Expo CLI logs.

```bash
#!/usr/bin/env bash
set -euo pipefail

MODE="${1:-start}"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

cd "$ROOT_DIR"

show_usage() {
  cat <<'USAGE'
usage: ./script/build_and_run.sh [mode]

Modes:
  start, run        Start the Expo dev server
  --ios, ios        Start Expo and open iOS
  --android, android
                   Start Expo and open Android
  --web, web        Start Expo for web
  --dev-client, dev-client
                   Start Expo in development-client mode
  --tunnel, tunnel Start Expo using tunnel transport
  --export-web, export-web
                   Export the web build locally
  --doctor, doctor Run Expo diagnostics
  --help, help     Show this help
USAGE
}

resolve_expo_cmd() {
  if [[ -n "${EXPO_CLI:-}" ]]; then
    # Optional escape hatch for projects that need a wrapper command.
    # shellcheck disable=SC2206
    EXPO_CMD=(${EXPO_CLI})
    return
  fi

  if [[ -f pnpm-lock.yaml ]] && command -v pnpm >/dev/null 2>&1; then
    EXPO_CMD=(pnpm exec expo)
  elif [[ -f yarn.lock ]] && command -v yarn >/dev/null 2>&1; then
    EXPO_CMD=(yarn expo)
  elif { [[ -f bun.lock ]] || [[ -f bun.lockb ]]; } && command -v bun >/dev/null 2>&1; then
    EXPO_CMD=(bunx expo)
  else
    EXPO_CMD=(npx expo)
  fi
}

run_doctor() {
  if [[ -f pnpm-lock.yaml ]] && command -v pnpm >/dev/null 2>&1; then
    pnpm exec expo-doctor
  elif [[ -f yarn.lock ]] && command -v yarn >/dev/null 2>&1; then
    yarn expo-doctor
  elif { [[ -f bun.lock ]] || [[ -f bun.lockb ]]; } && command -v bun >/dev/null 2>&1; then
    bunx expo-doctor
  else
    npx expo-doctor
  fi
}

resolve_expo_cmd

case "$MODE" in
  start|run)
    exec "${EXPO_CMD[@]}" start
    ;;
  --ios|ios)
    exec "${EXPO_CMD[@]}" start --ios
    ;;
  --android|android)
    exec "${EXPO_CMD[@]}" start --android
    ;;
  --web|web)
    exec "${EXPO_CMD[@]}" start --web
    ;;
  --dev-client|dev-client)
    exec "${EXPO_CMD[@]}" start --dev-client
    ;;
  --tunnel|tunnel)
    exec "${EXPO_CMD[@]}" start --tunnel
    ;;
  --export-web|export-web)
    exec "${EXPO_CMD[@]}" export --platform web
    ;;
  --doctor|doctor)
    run_doctor
    ;;
  --help|help)
    show_usage
    ;;
  *)
    show_usage >&2
    exit 2
    ;;
esac
```

## Primary Codex environment

Write the environment file at this exact path:

`.codex/environments/environment.toml`

Use one primary `Run` action by default:

```toml
# THIS IS AUTOGENERATED. DO NOT EDIT MANUALLY
version = 1
name = "<project-name>"

[setup]
script = ""

[[actions]]
name = "Run"
icon = "run"
command = "./script/build_and_run.sh"
```

If the project already has an environment file, update the existing `Run`
action's command instead of adding a second `Run` action.

## Optional action buttons

Add these only when useful for the project and the user has asked for direct
buttons. Keep the primary `Run` button even if you add platform-specific buttons.

```toml
[[actions]]
name = "Run iOS"
icon = "run"
command = "./script/build_and_run.sh --ios"

[[actions]]
name = "Run Android"
icon = "run"
command = "./script/build_and_run.sh --android"

[[actions]]
name = "Run Web"
icon = "run"
command = "./script/build_and_run.sh --web"

[[actions]]
name = "Run Dev Client"
icon = "run"
command = "./script/build_and_run.sh --dev-client"

[[actions]]
name = "Expo Doctor"
icon = "tool"
command = "./script/build_and_run.sh --doctor"
```

## Build actions

Prefer a safe local export/check action over cloud or store operations:

```toml
[[actions]]
name = "Export Web"
icon = "tool"
command = "./script/build_and_run.sh --export-web"
```

Only wire native or cloud builds when the user explicitly asks:

- `expo run:ios` / `expo run:android` can generate or require native project state.
- `eas build` is remote, authenticated, and can take a long time.
- `eas submit` and store workflows can publish to external stores.

## Testing the wiring

For setup-only changes, test syntax without starting a long-lived server:

```bash
bash -n ./script/build_and_run.sh
./script/build_and_run.sh --help
```

When the user asks you to run the app, use:

```bash
./script/build_and_run.sh
```

The server should stay in the foreground until the action terminal or shell
stops it.
