This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nvm-session.sh
99 lines (88 loc) · 3.15 KB
/
nvm-session.sh
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function nvm-session-usage() {
[ $# -gt 0 ] && printf "%s\n" "$@"
echo "Usage: "
echo " nvm-session use <version> : Switch to the specified version"
echo " nvm-session use : Show installed versions (same as list)"
echo " nvm-session list|ls : Shows installed versions and which is active"
echo " nvm-session <other-nvm-cmd> : Calls nvm to run command"
echo
}
function nvm-session-list() {
local cleanNvmList nodeVersion
if [ $# -ne 0 ]; then
nvm-session-passthru list "$@"
return $?
fi
# Outputs 'nvm list' but without any active versions marked
# get installed versions | leave only version | strip blank lines
cleanNvmList=$(nvm ls 2> /dev/null | sed "s/^[^0-9]* \([0-9][0-9\.]*\).*/\\1/" | sed '/^\s*$/d' 2> /dev/null)
nodeVersion=$(nvm-session-nodever)
echo "Installed versions:"
echo $cleanNvmList | tr " " "\n" | sed "s/^$nodeVersion/* $nodeVersion (active)/" | sed "s/^\([^\\*]\)/ \\1/"
}
function nvm-session-nodever() {
# Node on windows doesn't use tty so you can't parse stdout - Argg. Do some which+ls magic
local nodePath nodeVersionDir nodeVersion nodejsInDir
nodePath=$(which node)
[ $? -ne 0 ] && return 1
if [[ "$nodePath" == */nodejs/node ]]; then
# Using global version, follow the symlink from Program Files
nodejsInDir=$(dirname "$nodePath") # Up to nodejs dir
nodejsInDir=$(dirname "$nodejsInDir") # Up to parent dir
nodePath=$(ls -l "$nodejsInDir" | grep nodejs | awk -F"->" '{print $2}')/node # Get real node dir
fi
nodeVersionDir=$(dirname "$nodePath") # Path to node version dir
nodeVersion=$(basename "$nodeVersionDir" | sed "s/^v//") # Just version
echo $nodeVersion
}
function nvm-session-passthru() {
nvm "$@"
}
function nvm-session-use() {
local useVersion cleanPath nvmHome
if [ $# -ne 1 ]; then
nvm-session-list
return 1
fi
useVersion=$1
nvm-session-list | grep -q $useVersion
if [ $? -ne 0 ]; then
echo "Error: node version '$useVersion' not installed."
nvm-session-list
echo "Or try 'nvm-session list available' and 'nvm-session install <version>'' to add more."
return 1
fi
# Update path with requested version
cleanPath=$(echo $PATH | tr ':' '\n' | grep -v /nvm/v | tr '\n' ':')
nvmHome=$(dirname $(which nvm)) # NVM_HOME is Windows style, need GitBash style path
export PATH="$nvmHome/v$useVersion:$PATH"
echo "nvm-session: now using v$useVersion"
return 0
}
function nvm-session() {
which nvm 2>&1 > /dev/null
if [ $? -ne 0 ]; then
echo "Error: NVM for Windows not found. Please ensure it is in your path."
return 1
fi
case "$1" in
help|-h*|'')
nvm-session-usage
return 1
;;
use)
shift
nvm-session-use "$@"
return $?
;;
list|ls)
shift
nvm-session-list "$@"
return $?
;;
*)
nvm-session-passthru "$@"
return $?
;;
esac
}