Skip to content

Commit 68cfe8d

Browse files
committed
add open-editor.sh
1 parent cc85325 commit 68cfe8d

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

install-ai-agent.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ cp -rf /opt/cursor/.local/bin/cursor-agent /opt/npm/bin
1515

1616
# 全局配置
1717
echo "prefix=/root/.npm-packages" > /opt/node/lib/node_modules/npm/npmrc
18+
19+
# 可使用 vscode / cursor 快捷打开项目目录,
20+
# 注: code 会按当前所属编辑器打开
21+
ln -sf /opt/scripts/open-editor.sh /opt/bin/vscode
22+
ln -sf /opt/scripts/open-editor.sh /opt/bin/cursor

scripts/open-editor.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
# Smart opener for VSCode / Cursor remote server instances.
3+
# Create symlinks named 'vscode' and 'cursor' pointing to this script,
4+
# then use: vscode <path> / cursor <path>
5+
set -euo pipefail
6+
7+
CMD_NAME="$(basename "$0")"
8+
9+
case "$CMD_NAME" in
10+
vscode|code)
11+
SERVER_DIR_PATTERN=".vscode-server"
12+
DISPLAY_NAME="VSCode"
13+
;;
14+
cursor)
15+
SERVER_DIR_PATTERN=".cursor-server"
16+
DISPLAY_NAME="Cursor"
17+
;;
18+
*)
19+
echo "Error: invoke as 'vscode <path>' or 'cursor <path>'"
20+
echo "Create symlinks: ln -s $(readlink -f "$0") /usr/local/bin/vscode"
21+
echo " ln -s $(readlink -f "$0") /usr/local/bin/cursor"
22+
exit 1
23+
;;
24+
esac
25+
26+
# Find the main server process (server-main.js, NOT extensionHost)
27+
SERVER_PID=""
28+
SERVER_NODE=""
29+
while IFS= read -r pid; do
30+
cmdline=$(tr '\0' ' ' < /proc/"$pid"/cmdline 2>/dev/null) || continue
31+
if [[ "$cmdline" == *"$SERVER_DIR_PATTERN"* ]] \
32+
&& [[ "$cmdline" == *"server-main.js"* ]] \
33+
&& [[ "$cmdline" != *"--type=extensionHost"* ]]; then
34+
SERVER_PID="$pid"
35+
SERVER_NODE="${cmdline%% *}"
36+
break
37+
fi
38+
done < <(pgrep -x node 2>/dev/null)
39+
40+
if [[ -z "$SERVER_PID" ]]; then
41+
echo "✗ No running $DISPLAY_NAME server process found." >&2
42+
exit 1
43+
fi
44+
45+
# Derive install dir from the node binary path
46+
SERVER_BASE="$(dirname "$SERVER_NODE")"
47+
48+
# Locate CLI binary: prefer 'cursor' for Cursor, fallback to 'code'
49+
CLI_BIN=""
50+
for name in "$CMD_NAME" code; do
51+
candidate="$SERVER_BASE/bin/remote-cli/$name"
52+
if [[ -x "$candidate" ]]; then
53+
CLI_BIN="$candidate"
54+
break
55+
fi
56+
done
57+
58+
if [[ -z "$CLI_BIN" ]]; then
59+
echo "✗ CLI binary not found under $SERVER_BASE/bin/remote-cli/" >&2
60+
exit 1
61+
fi
62+
63+
# Find IPC socket owned by the main server PID
64+
IPC_SOCK="$(ss -lnpx 2>/dev/null \
65+
| grep "pid=${SERVER_PID}," \
66+
| grep -oP '/tmp/vscode-ipc-\S+' \
67+
| head -1)"
68+
69+
if [[ -z "$IPC_SOCK" ]]; then
70+
echo "✗ No available IPC socket for $DISPLAY_NAME (server PID=$SERVER_PID)." >&2
71+
exit 1
72+
fi
73+
74+
export VSCODE_IPC_HOOK_CLI="$IPC_SOCK"
75+
exec "$CLI_BIN" "$@"

0 commit comments

Comments
 (0)