-
Notifications
You must be signed in to change notification settings - Fork 0
/
nva_completion
executable file
·104 lines (92 loc) · 3.03 KB
/
nva_completion
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
100
101
102
103
104
#!/usr/bin/bash
_nva_completion() {
local cur prev opts
script_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
node_versions_dir="$script_dir/nodejs"
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-i --install -s --set -r --remove -h --help -l --list -p --patch -v, --version "
local config_dir="$HOME/.config/nva"
local cache_file="$config_dir/nva_release_cache.txt"
# Check if the cache needs to be updated
local update_interval=86400 # Update every 24 hours
local current_time=$(date +%s)
[ -d "$config_dir" ] || mkdir -p "$config_dir"
if [ -e "$cache_file" ]; then
local last_update=$(stat -c %Y "$cache_file")
local time_diff=$((current_time - last_update))
if [ $time_diff -gt $update_interval ]; then
releases=$(curl -s https://nodejs.org/download/release/ | grep -oP '^<a href="v\d{1,4}\.\d{1,4}\.\d{1,4}' | grep -oP '\d.*' | uniq)
echo "$releases" > "$cache_file"
else
releases=$(cat "$cache_file")
fi
else
# Cache doesn't exist, create it
releases=$(curl -s https://nodejs.org/download/release/ | grep -oP '^<a href="v\d{1,4}\.\d{1,4}\.\d{1,4}' | grep -oP '\d.*' | uniq)
echo "$releases" > "$cache_file"
fi
get_installed_versions() {
if [ -d "$node_versions_dir" ]; then
find "$node_versions_dir" -maxdepth 1 -mindepth 1 -type d -exec basename {} \; | grep -oP '\d+(\.\d+){0,2}'
else
echo ""
fi
}
# Check if -i or --install is used
for ((i=0; i < ${#COMP_WORDS[@]}; i++)); do
if [[ "${COMP_WORDS[i]}" == "-i" || "${COMP_WORDS[i]}" == "--install" ]]; then
install_version="${COMP_WORDS[i+1]}"
break
fi
done
case "${prev}" in
-i|--install)
COMPREPLY=( $(compgen -W "${releases}" -- ${cur}) )
return 0
;;
-r|--remove)
local installed_versions=$(get_installed_versions)
COMPREPLY=( $(compgen -W "${installed_versions}" -- ${cur}) )
return 0
;;
-s|--set)
local installed_versions=$(get_installed_versions)
if [ -n "$install_version" ]; then
# If -i or --install is used, suggest that version
COMPREPLY=( $(compgen -W "$install_version" -- ${cur}) )
elif [ -n "$installed_versions" ]; then
# If there are installed versions, suggest them
COMPREPLY=( $(compgen -W "$installed_versions" -- ${cur}) )
else
# If no installed versions and no -i/--install, suggest from releases
COMPREPLY=( $(compgen -W "${releases}" -- ${cur}) )
fi
return 0
;;
-h|--help)
COMPREPLY=( $(compgen -W "" -- ${cur}) )
return 0
;;
-v|--version)
COMPREPLY=( $(compgen -W "" -- ${cur}) )
return 0
;;
-l|--list)
COMPREPLY=( $(compgen -W "" -- ${cur}) )
return 0
;;
-p|--patch)
if [[ "${cur}" != -* ]]; then
COMPREPLY=( $(compgen -W "clean" -- ${cur}) )
return 0
fi
;;
*)
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _nva_completion nva