-
Notifications
You must be signed in to change notification settings - Fork 0
/
.direnvrc
130 lines (102 loc) · 2.88 KB
/
.direnvrc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
__getnodebinpath() {
local targetversion="$1"
local nodedir="$N_PREFIX/n/versions/node"
local latest=$(ls "$nodedir" | egrep "^$targetversion.*$" | sort --version-sort | tail -n 1)
local nodebinpath="$nodedir/$latest/bin"
if [ ! -d "$nodebinpath" ]; then
echo "Downloading node $targetversion via n ..." >&2
currentnodeversion=$(node --version)
n $targetversion >&2
n $currentnodeversion >&2
latest=$(ls "$nodedir" | egrep "^$targetversion.*$" | sort --version-sort | tail -n 1)
nodebinpath="$nodedir/$latest/bin"
fi
echo "$nodebinpath"
}
__get_alias_path() {
echo "$HOME/.config/direnv/aliases"
}
# modified from https://github.com/direnv/direnv/issues/73#issuecomment-1242969787
__export_alias() {
local name=$1
shift
local alias_dir="$(__get_alias_path)"
local target="$alias_dir/$name"
local old_path="$PATH"
mkdir -p "$alias_dir"
echo "#!/usr/bin/env bash" > "$target"
echo "PATH=$old_path" >> "$target"
echo "$@" >> "$target"
chmod +x "$target"
}
__mise_path_add() {
local env_path="env_path = [ \"$1\" ]"
if [ ! -f .mise.toml ]; then
printf '%s\n' "$env_path" > .mise.toml
else
if ! grep "env_path" .mise.toml; then
printf '%s\n\n%s\n' "$env_path" "$(cat .mise.toml)" > .mise.toml
fi
fi
}
use_bun() {
local version="$1"
if [ ! -z $version ]; then
use_node $version
fi
__export_alias npm 'bun "$@"'
__export_alias yarn 'bun "$@"'
if command -v mise > /dev/null; then
mise use --path .mise.toml bun@latest
fi
__mise_path_add "$(__get_alias_path)"
PATH_add "$(__get_alias_path)"
}
use_pnpm() {
layout_node
local version="$1"
if [ ! -z $version ]; then
use_node $version
fi
__export_alias npm 'pnpm "$@"'
__export_alias yarn 'pnpm "$@"'
if command -v mise > /dev/null; then
mise use --path .mise.toml pnpm@latest
fi
__mise_path_add "$(__get_alias_path)"
PATH_add "$(__get_alias_path)"
}
use_node() {
layout_node
local version="$1"
local NPM_BIN_PATH=""
# place global npm dir in path if present
if [ -d "$HOME/local/npm/bin" ]; then
NPM_BIN_PATH="$HOME/local/npm/bin"
fi
if [ ! -z $NODE_VERSION_PREFIX ] && [ ! -z $NODE_VERSIONS ]; then
local mv=`echo $version | cut -d. -f1`
local folder="$NODE_VERSIONS/$NODE_VERSION_PREFIX$mv"
if [ -d "$folder" ] && [ -f "$folder/bin/node" ]; then
export PATH=$PATH:$NPM_BIN_PATH:$folder/bin
export ENVROOT="$folder"
return 0
fi
fi
if command -v mise > /dev/null; then
mise use --path .mise.toml "node@$version"
export PATH=$PATH:$NPM_BIN_PATH
return 0
fi
local NODE_BIN_PATH="$(__getnodebinpath $version)"
export PATH=$PATH:$NPM_BIN_PATH:$NODE_BIN_PATH
}
use_python() {
local version="$1"
if command -v mise > /dev/null 2>&1; then
mise use --path .mise.toml "python@$version"
return 0
fi
echo "[~/.direnvrc] use python is not supported"
return 1
}