-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpm-proxy.sh
More file actions
executable file
·80 lines (70 loc) · 2.04 KB
/
npm-proxy.sh
File metadata and controls
executable file
·80 lines (70 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
echo -e "\033[34mnpm-proxy.sh\033[0m"
# Array if package managers and their lock files
PACKAGE_MANAGERS=("npm:package-lock.json" "pnpm:pnpm-lock.yaml" "yarn:yarn.lock" "bun:bun.lockb")
# A function to validate package manager name
is_valid_package_manager() {
local pm=$1
for entry in "${PACKAGE_MANAGERS[@]}"; do
IFS=":" read -r manager _ <<<"$entry"
if [ "$manager" = "$pm" ]; then
return 0
fi
done
return 1
}
# A function to detect the package manager based on lock files
detect_package_manager() {
for entry in "${PACKAGE_MANAGERS[@]}"; do
IFS=":" read -r manager lockfile <<<"$entry"
if [ -f "$lockfile" ]; then
echo "$manager"
return
fi
done
echo "npm" # Default to npm if no lock file is found
}
# Function to display usage
show_help() {
echo "Usage: npm-proxy.sh [options] [command] [arguments...]"
echo "Proxies npm commands to the appropriate package manager."
echo ""
echo "Options:"
echo " --use <manager> Force using specific package manager (npm|pnpm|yarn|bun)"
echo ""
echo "Examples:"
echo " npm-proxy.sh --use pnpm install"
echo " npm-proxy.sh --use yarn add express"
echo " npm-proxy.sh --use bun run start"
}
# If no arguments are provided, show help and exit
if [ $# -eq 0 ]; then
show_help
exit 1
fi
# Check for forced package manager
PACKAGE_MANAGER=""
if [ "$1" = "--use" ]; then
if [ -z "$2" ]; then
echo "Error: Package manager name required"
exit 1
fi
if ! is_valid_package_manager "$2"; then
echo "Error: Invalid package manager '$2'"
exit 1
fi
PACKAGE_MANAGER="$2"
shift 2
else
# Detect the package manager
PACKAGE_MANAGER=$(detect_package_manager)
fi
echo -e "\033[90mUsing package manager: $PACKAGE_MANAGER\033[0m"
echo ""
# Check if there are any remaining arguments
if [ $# -eq 0 ]; then
show_help
exit 1
fi
# Proxy the command to the detected package manager
"$PACKAGE_MANAGER" "$@"