|
#!/bin/bash |
|
# |
|
# VSCode/Cursor Universal CLI Wrapper |
|
# |
|
# Purpose: |
|
# Provides a unified 'code' or 'cursor' command that works correctly in both |
|
# local and Remote SSH environments. |
|
# |
|
# How it works: |
|
# - Detects the environment by examining the VSCODE_GIT_ASKPASS_NODE variable |
|
# - For Remote SSH sessions (.vscode-server or .cursor-server in path): |
|
# Uses the special "remote CLI" that communicates back to your local editor |
|
# window instead of trying to launch an editor on the remote machine |
|
# - For local sessions (.app in path): |
|
# Uses the regular system binary from PATH |
|
# - When VSCODE_GIT_ASKPASS_NODE is not set (fallback mode): |
|
# Uses VSCODE_WRAPPER_DEFAULT if set, otherwise searches PATH for 'code' |
|
# then 'cursor' and uses whichever is found |
|
# - Auto-detects whether you're using VSCode or Cursor and invokes the |
|
# appropriate binary |
|
# |
|
|
|
show_usage() { |
|
echo "Usage: $0 [options] [file...]" |
|
echo "Universal wrapper for VSCode/Cursor CLI in local and remote environments" |
|
} |
|
|
|
detect_editor_from_app_path() { |
|
local node_path="$1" |
|
if [[ "$node_path" == *"Cursor"* ]]; then |
|
echo "cursor" |
|
elif [[ "$node_path" == *"Code"* ]] || [[ "$node_path" == *"Visual Studio Code"* ]]; then |
|
echo "code" |
|
else |
|
return 1 |
|
fi |
|
} |
|
|
|
detect_editor_from_server_path() { |
|
local node_path="$1" |
|
if [[ "$node_path" == *".cursor-server"* ]]; then |
|
echo "cursor" |
|
elif [[ "$node_path" == *".vscode-server"* ]]; then |
|
echo "code" |
|
else |
|
return 1 |
|
fi |
|
} |
|
|
|
auto_detect_editor_in_path() { |
|
if [[ -n "${VSCODE_WRAPPER_DEFAULT:-}" ]]; then |
|
echo "$VSCODE_WRAPPER_DEFAULT" |
|
elif command -v code &>/dev/null; then |
|
echo "code" |
|
elif command -v cursor &>/dev/null; then |
|
echo "cursor" |
|
else |
|
return 1 |
|
fi |
|
} |
|
|
|
get_system_binary_path() { |
|
local editor_binary="$1" |
|
local cli_path |
|
cli_path="$(command -v "$editor_binary")" |
|
if [[ -z "$cli_path" ]]; then |
|
return 1 |
|
fi |
|
echo "$cli_path" |
|
} |
|
|
|
get_remote_cli_path() { |
|
local node_path="$1" |
|
local editor_binary="$2" |
|
echo "$(dirname "$node_path")/bin/remote-cli/$editor_binary" |
|
} |
|
|
|
# ============================================================================ |
|
# Main script logic |
|
# ============================================================================ |
|
|
|
# Check for help flag |
|
if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
|
show_usage |
|
exit 0 |
|
fi |
|
|
|
# Determine the CLI path based on environment |
|
if [[ -z "${VSCODE_GIT_ASKPASS_NODE:-}" ]]; then |
|
# No VSCODE_GIT_ASKPASS_NODE: fallback to system binary |
|
editor_binary=$(auto_detect_editor_in_path) |
|
if [[ $? -ne 0 ]]; then |
|
echo "Error: Neither 'code' nor 'cursor' found in PATH" |
|
echo "Set VSCODE_WRAPPER_DEFAULT or ensure one of these binaries is available" |
|
exit 1 |
|
fi |
|
cli_path=$(get_system_binary_path "$editor_binary") |
|
if [[ $? -ne 0 ]]; then |
|
echo "Error: $editor_binary command not found in PATH" |
|
exit 1 |
|
fi |
|
else |
|
remote_node_path="$VSCODE_GIT_ASKPASS_NODE" |
|
|
|
if [[ "$remote_node_path" == *".app"* ]]; then |
|
# Local app path |
|
editor_binary=$(detect_editor_from_app_path "$remote_node_path") |
|
if [[ $? -ne 0 ]]; then |
|
echo "Error: Cannot determine editor type from local path: $remote_node_path" |
|
exit 1 |
|
fi |
|
cli_path=$(get_system_binary_path "$editor_binary") |
|
if [[ $? -ne 0 ]]; then |
|
echo "Error: $editor_binary command not found in PATH" |
|
exit 1 |
|
fi |
|
else |
|
# Remote server path |
|
editor_binary=$(detect_editor_from_server_path "$remote_node_path") |
|
if [[ $? -ne 0 ]]; then |
|
echo "Error: Cannot determine editor type from path: $remote_node_path" |
|
exit 1 |
|
fi |
|
cli_path=$(get_remote_cli_path "$remote_node_path" "$editor_binary") |
|
fi |
|
fi |
|
|
|
# Check if CLI exists and is executable |
|
if [[ ! -x "$cli_path" ]]; then |
|
echo "Error: CLI not found or not executable at '$cli_path'" |
|
exit 1 |
|
fi |
|
|
|
# Execute the CLI with all arguments |
|
exec "$cli_path" "$@" |