-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvm.sh
91 lines (77 loc) · 2.21 KB
/
nvm.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
#!/bin/bash
# this file is to operate NVM - Node Version Manager
# Python dosent know 'nvm', because nvm need to be load in section Linux
# if used 'subprocess.getstatusoutput('nvm --version'), Python dosent know
# but, in shell is to easy to load and to operate
# this function load the env of NVM
# Use:
# bash nvm.sh --version -> get the version of nvm
# bash nvm.sh version x.y.z -> get if specify version of node is installed
# bash nvm.sh install x.y.z
# bash nvm.sh npm install ./path/to/test x.y.z
# bash nvm.sh npm test ./path/to/test x.y.z
loads_nvm() {
export NVM_DIR="$HOME/.config/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
}
# get the current version of nvm
nvm_version() {
loads_nvm
nvm --version # here, nvm is a program
}
# get info if specify version is installed
node_version() {
loads_nvm
nvm version $1
}
# install the specify version $2
nvm_install() {
loads_nvm
nvm install $1 --force # if the package has himself as dependencies
}
# change the version of nvm
nvm_use() {
loads_nvm
nvm use $1
}
# execute npm install in ./path/to/install in the node version x.y.z
npm_install() {
nvm_use $2
npm install --prefix $1 --silent
}
# execute npm test in ./path/to/test in the node version x.y.z
npm_test() {
nvm_use $2
npm test --prefix $1
}
correct_usage() {
echo "bash nvm.sh [version] x.y.z | [install] x.y.z | npm [test|install] ./path/to/operation x.y.x"
}
# bash nvm.sh version
# bash nvm.sh install x.y.z
# bash nvm.sh npm install ./path/to/test x.y.z
# bash nvm.sh npm test ./path/to/test x.y.z
main() {
if [ $1 = "--version" ]; then
nvm_version
elif [ -z $2 ]; then # the follows need the $2, version
correct_usage
elif [ $1 = "version" ]; then
node_version $2
elif [ $1 = "install" ]; then
nvm_install $2
elif [ -z $3 ] || [ -z $4 ]; then # the follows need the $3 - ./path/to - and $4 - x.y.z
correct_usage
elif [ $1 = "npm" ]; then
if [ $2 = "install" ]; then
npm_install $3 $4
elif [ $2 = "test" ]; then
npm_test $3 $4
else
correct_usage
fi
else
correct_usage
fi
}
main $1 $2 $3 $4