#!/bin/bash
# Common setup for JS SDK dev scripts. Source this file, don't execute directly.
set -eou pipefail

# Resolve repo root. Prefer git, fall back to jj for non-colocated jj
# workspaces (e.g. workspaces created via `jj workspace add`).
if ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"; then
    :
elif ROOT="$(jj root 2>/dev/null)"; then
    :
else
    echo "error: not in a git or jj working tree" >&2
    exit 1
fi
SDK_ROOT="${ROOT}/sdks/js"

ensure_nix_shell() {
    if [[ "${XMTP_DEV_SHELL:-}" != "js" && "${XMTP_DEV_SHELL:-}" != "local" ]]; then
        exec nix develop "${ROOT}#js" --command "$0" "$@"
    fi
}
