import { RNX_CLOUD_SESSION_ENV, RNX_SHELL_UPDATE_SET_PREFIX, RNX_SHELL_UPDATE_UNSET, } from './cloud-session' export function renderRnxShellInit(shell: string): string { if (shell !== 'bash' && shell !== 'zsh') { throw new Error('RNX shell integration supports bash and zsh') } return `rnx() { local -a _rnx_shell_args local _rnx_shell_command _rnx_shell_create _rnx_shell_update local _rnx_shell_session _rnx_shell_status _rnx_shell_traced _rnx_shell_args=("$@") _rnx_shell_command='' _rnx_shell_create=0 while [ "$#" -gt 0 ]; do case "$1" in -h|--help|-V|--version|-v|--verbose|--headless) shift ;; -p|--port|-d|--device|-t|--theme|--driver|--sim|--session|--tab) if [ "$#" -ge 2 ]; then shift 2; else shift; fi ;; --) break ;; -*) shift ;; *) _rnx_shell_command="$1"; shift; break ;; esac done case "$_rnx_shell_command" in claim|close) ;; ios) for _rnx_shell_session in "$@"; do if [ "$_rnx_shell_session" = '--remote' ]; then _rnx_shell_create=1 break fi done if [ "$_rnx_shell_create" -ne 1 ]; then command rnx "\${_rnx_shell_args[@]}" return $? fi ;; *) command rnx "\${_rnx_shell_args[@]}" return $? ;; esac if { [ -n "\${BASH_VERSION:-}" ] && [ "\${BASH_SUBSHELL:-0}" -ne 0 ]; } || { [ -n "\${ZSH_VERSION:-}" ] && [ "\${ZSH_SUBSHELL:-0}" -ne 0 ]; }; then printf '%s\\n' 'rnx remote simulator lifecycle commands must run directly in the current shell, not in a pipeline or command substitution' >&2 return 1 fi if ( : >&9 ) 2>/dev/null; then printf '%s\\n' 'rnx remote simulator shell integration requires file descriptor 9 to be unused' >&2 return 1 fi if ! ( export ${RNX_CLOUD_SESSION_ENV}=__rnx_shell_probe && unset ${RNX_CLOUD_SESSION_ENV} ) 2>/dev/null; then printf '%s\\n' 'rnx remote simulator shell integration cannot update RNX_CLOUD_SESSION in this shell' >&2 return 1 fi case "$-" in *x*) _rnx_shell_traced=1; set +x ;; *) _rnx_shell_traced=0 ;; esac exec 9>&1 _rnx_shell_update="$(RNX_SHELL_UPDATE_FD=3 command rnx "\${_rnx_shell_args[@]}" 3>&1 1>&9)" _rnx_shell_status=$? exec 9>&- if [ "$_rnx_shell_status" -eq 0 ]; then case "$_rnx_shell_update" in ${RNX_SHELL_UPDATE_SET_PREFIX}*) _rnx_shell_session="\${_rnx_shell_update#${RNX_SHELL_UPDATE_SET_PREFIX}}" case "$_rnx_shell_session" in ''|*$'\\n'*) _rnx_shell_status=1 ;; *) export ${RNX_CLOUD_SESSION_ENV}="$_rnx_shell_session" || _rnx_shell_status=$? ;; esac ;; ${RNX_SHELL_UPDATE_UNSET}) unset ${RNX_CLOUD_SESSION_ENV} || _rnx_shell_status=$? ;; *) _rnx_shell_status=1 ;; esac if [ "$_rnx_shell_status" -ne 0 ]; then printf '%s\\n' 'rnx refused an invalid shell session update' >&2 elif [ "$_rnx_shell_create" -eq 1 ]; then command rnx __cloud-confirm _rnx_shell_status=$? fi fi if [ "$_rnx_shell_traced" -eq 1 ]; then set -x; fi return "$_rnx_shell_status" } ` }