Skip to content

Commit 85aaabe

Browse files
committed
feat: add function to resolve installation directory and update symlink prompt
1 parent 05a4a42 commit 85aaabe

1 file changed

Lines changed: 53 additions & 7 deletions

File tree

build.sh

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ fi
8686

8787
echo "[WebCC] Done."
8888

89+
resolve_install_dir() {
90+
local candidates=()
91+
92+
if [[ "$OSTYPE" == "darwin"* ]]; then
93+
candidates=("/opt/homebrew/bin" "/usr/local/bin")
94+
else
95+
candidates=("/usr/local/bin")
96+
fi
97+
98+
# Prefer a directory that exists and is already in PATH
99+
for dir in "${candidates[@]}"; do
100+
if [ -d "$dir" ] && [[ ":$PATH:" == *":$dir:"* ]]; then
101+
echo "$dir"
102+
return
103+
fi
104+
done
105+
106+
# Fallback to first existing candidate
107+
for dir in "${candidates[@]}"; do
108+
if [ -d "$dir" ]; then
109+
echo "$dir"
110+
return
111+
fi
112+
done
113+
114+
# Last resort for user-local installs
115+
echo "$HOME/.local/bin"
116+
}
117+
89118
# In CI, just exit successfully
90119
if [ -n "$CI" ]; then
91120
exit 0
@@ -98,23 +127,40 @@ if command -v webcc >/dev/null 2>&1 && [ "$(command -v webcc)" -ef "$PWD/webcc"
98127
exit 2 # 2 = rebuilt
99128
fi
100129

101-
# Only offer install if /usr/local/bin exists (common on Linux/macOS)
102-
# And we are in an interactive terminal and not in CIs
103-
if [ -d "/usr/local/bin" ] && [ -t 0 ] && [ -z "$CI" ]; then
130+
# Only offer install in interactive terminals outside CI
131+
if [ -t 0 ] && [ -z "$CI" ]; then
132+
INSTALL_DIR=$(resolve_install_dir)
133+
134+
if [ "$INSTALL_DIR" = "$HOME/.local/bin" ] && [ ! -d "$INSTALL_DIR" ]; then
135+
mkdir -p "$INSTALL_DIR"
136+
fi
137+
104138
echo ""
105139
echo "To use 'webcc' from anywhere, it needs to be in your PATH."
106-
read -p "Would you like to create a symlink in /usr/local/bin? [y/N] " -n 1 -r
140+
read -p "Would you like to create a symlink in $INSTALL_DIR? [y/N] " -n 1 -r
107141
echo
108142
if [[ $REPLY =~ ^[Yy]$ ]]; then
109-
TARGET="/usr/local/bin/webcc"
110-
if [ -w /usr/local/bin ]; then
143+
TARGET="$INSTALL_DIR/webcc"
144+
if [ -w "$INSTALL_DIR" ]; then
111145
ln -sf "$PWD/webcc" "$TARGET"
112146
else
113-
echo "Need sudo access to write to /usr/local/bin"
147+
echo "Need sudo access to write to $INSTALL_DIR"
114148
sudo ln -sf "$PWD/webcc" "$TARGET"
115149
fi
116150
echo "Symlink created: $TARGET -> $PWD/webcc"
117151
echo "You can now use 'webcc' command from any directory."
152+
153+
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
154+
echo ""
155+
echo "Note: $INSTALL_DIR is not currently in your PATH."
156+
if [[ "$OSTYPE" == "darwin"* ]]; then
157+
echo "Add this to ~/.zshrc:"
158+
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
159+
else
160+
echo "Add this to your shell config (e.g. ~/.bashrc):"
161+
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
162+
fi
163+
fi
118164
fi
119165
fi
120166

0 commit comments

Comments
 (0)