-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdn.sh
executable file
·734 lines (630 loc) · 18.3 KB
/
dn.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#!/usr/bin/env bash
shopt -s extglob
set -e
## useful resource: https://hackernoon.com/inspecting-docker-images-without-pulling-them-4de53d34a604
# "inspecting docker image without pulling"
DN_VERSION='0.4.1'
DN_REPO_URL='https://github.com/sfertman/dn'
DN_PREFIX=/usr/local
DN_DEFAULT_NODE_VERSION=14
DN_DISABLED=false
DN_CONFIG_DIR=${HOME}/.dn
config_file="${DN_CONFIG_DIR}/config.env"
test -f "${config_file}" && source "${config_file}"
DN_INSTALL_DIR=${DN_PREFIX}/lib/dn
DN_BIN_DIR=${DN_PREFIX}/bin
DN_LOCAL_BIN_DIR=${HOME}/.local/bin
DN_HOME_BIN_DIR=${DN_CONFIG_DIR}/bin
OS_TYPE=$(uname)
echoerr() { echo "$@" 1>&2; }
ERRNIMPL() { echoerr "ERROR: Not implemented! 🤖"; }
write_exe_file() {
local cmd="${1}";
cat > "${DN_HOME_BIN_DIR}/${cmd}" <<EOF
#!/bin/sh
dn run ${cmd} \$@
EOF
chmod +x "${DN_HOME_BIN_DIR}/${cmd}";
}
prefix_if_not_exist_PATH() {
## prefixes $1 to $PATH if not in there already
echo "${PATH}" | grep -Eoq "^${1}:|:${1}:|:${1}$" || export PATH="${1}:${PATH}";
}
create_link () {
ln -sf "${DN_HOME_BIN_DIR}/${1}" "${DN_LOCAL_BIN_DIR}/${1}";
}
create_links() {
create_link node;
create_link npm;
create_link npx;
create_link yarn;
create_link yarnpkg;
}
dn_on_q() {
create_links;
delete_disabled_config;
}
dn_on() {
dn_on_q;
PATH="${PATH}"; # forces rehash
echo "DN is enabled.";
}
delete_link() {
rm "${DN_LOCAL_BIN_DIR}/${1}";
}
delete_links() {
delete_link node;
delete_link npm;
delete_link npx;
delete_link yarn;
delete_link yarnpkg;
}
delete_disabled_config() {
case "${OS_TYPE}" in
Linux) sed -i '/DN_DISABLED/d' "${config_file}" ;;
Darwin) sed -i '' '/DN_DISABLED/d' "${config_file}" ;;
*) echoerr "Shell ${OS_TYPE} is not supported!"; return 1 ;;
esac
}
dn_off_q() {
delete_links;
delete_disabled_config;
}
dn_off() {
dn_off_q;
echo "DN_DISABLED=true" >> ${config_file};
echo "DN has been disabled. Run 'dn on' to enable."
}
dn_install() {
echo "Installing DN...";
echo "";
mkdir -p ${DN_INSTALL_DIR};
local script_path;
case "${SHELL}" in
*/zsh)
script_path="${0}"
local rc_file="${HOME}/.zshrc"
echo "###GENERATED BY DN.SH--DO NOT MODIFY" >> ${rc_file}
echo "echo \"\${PATH}\" | grep -Eoq \"^${DN_LOCAL_BIN_DIR}:|:${DN_LOCAL_BIN_DIR}:|:${DN_LOCAL_BIN_DIR}$\" || export PATH=\"${DN_LOCAL_BIN_DIR}:\${PATH}\";" >> ${rc_file};
;;
*/bash|*/sh)
script_path="$(realpath "${0}")"
local rc_file="${HOME}/.bashrc"
echo "###GENERATED BY DN.SH--DO NOT MODIFY" >> ${rc_file}
echo "echo \"\${PATH}\" | grep -Eoq \"^${DN_LOCAL_BIN_DIR}:|:${DN_LOCAL_BIN_DIR}:|:${DN_LOCAL_BIN_DIR}$\" || export PATH=\"${DN_LOCAL_BIN_DIR}:\${PATH}\";" >> ${rc_file};
;;
*)
echoerr "Shell ${SHELL} is not supported! Installation FAILED 😱"
echoerr
echoerr "To add support for ${SHELL} asap, please open an issue at:"
echoerr
echoerr " ${DN_REPO_URL}/issues"
echoerr
return 1
;;
esac
# copy dn.sh script into install dir
cp -afp "${script_path}" "${DN_INSTALL_DIR}";
# make dn.sh executable
chmod +x ${DN_INSTALL_DIR}/dn.sh;
# link executable into bin dir so that we can invoke as a command
ln -sf ${DN_INSTALL_DIR}/dn.sh ${DN_BIN_DIR}/dn;
# add and switch to default version
dn_add_and_switch 1 0 $DN_DEFAULT_NODE_VERSION
write_exe_file node;
write_exe_file npm;
write_exe_file npx;
write_exe_file yarn;
write_exe_file yarnpkg;
dn_on_q;
prefix_if_not_exist_PATH "${DN_LOCAL_BIN_DIR}";
PATH="${PATH}";
# write config file if not exist
if [ ! -f "${config_file}" ]; then
mkdir -p ${DN_CONFIG_DIR};
touch "${config_file}";
echo "DN_PREFIX=${DN_PREFIX}" >> "${config_file}";
echo "DN_DEFAULT_NODE_VERSION=${DN_DEFAULT_NODE_VERSION}" >> "${config_file}";
echo "DN_DISABLED=${DN_DISABLED}" >> "${config_file}";
fi
echo
echo "DN has been successfully INSTALLED. 🎉"
dn_info
echo
echo "Run 'dn --help' to get started. 📖";
}
dn_uninstall() {
read -p "This will UNINSTALL DN from your system; proceed (y/N)? " -n 1 -r
echo
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Whew! 😌"
return 1;
fi
echo "Uninstalling DN...";
echo "";
dn_off_q;
rm -f ${DN_BIN_DIR}/dn;
rm -rf ${DN_INSTALL_DIR};
local rc_file;
case "${SHELL}" in
*/zsh) rc_file="${HOME}/.zshrc" ;;
*/bash|*/sh) rc_file="${HOME}/.bashrc" ;;
*)
echoerr "Shell ${SHELL} is not supported! Uninstallation FAILED 😱"
echoerr
echoerr "To add support for ${SHELL} asap, please open an issue at:"
echoerr
echoerr " ${DN_REPO_URL}/issues"
echoerr
return 1
;;
esac
case "${OS_TYPE}" in
Linux) sed -i '/###GENERATED BY DN.SH--DO NOT MODIFY/{N;d;}' ${rc_file} ;;
Darwin) sed -i '' '/###GENERATED BY DN.SH--DO NOT MODIFY/{N;d;}' ${rc_file} ;;
*) echoerr "Shell ${OS_TYPE} is not supported!"; return 1 ;;
esac
echo "DN has been successfully UNINSTALLED 🤔"
echo
echo "Sorry to see you go. If this is due to a bug or lack"
echo "of features please let me know by opening an issue at:"
echo
echo " ${DN_REPO_URL}/issues"
echo
}
validate_version() {
if [ -z "$(echo "${1}" | grep -Eo '^[0-9]+(\.[0-9]+){0,2}$')" ]; then
echo 'INVALID_VERSION';
fi
}
is_installed_version() {
# echoes input if version installed and nothing otherwize
local version="${1}"
if [ ! -z $(docker images node:${version}-alpine -q) ] ; then
echo "${version}"
fi
}
dn_add() {
_help() { echoerr "
Usage: dn add VERSION [VERSION...]
Install one or more node versions.";}
if [ "$#" -le 0 ] ; then
_help
else
case "$1" in
-h|--help)
_help
;;
*)
for v ; do
docker pull "library/node:${v}-alpine";
done
;;
esac
fi
}
dn_rm() {
_help() { echoerr "
Usage: dn rm VERSION [VERSION...]
Uninstall one or more node versions from your system.";}
if [ $# -le 0 ] ; then
_help
else
case "$1" in
-h|--help)
_help
;;
*)
for v ; do
docker rmi "node:${v}-alpine"
done
;;
esac
fi
}
realpath1() {
local this_dir="${PWD}";
cd "$(dirname "${1}")";
local fullpath="${PWD}/$(basename "${1}")";
cd "${this_dir}";
echo "${fullpath}";
}
dn_run() {
_help() { echoerr "
Usage: dn run COMMAND [ARG...]
Commands:
node Run node [ARG...]
npm Run npm [ARG...]
npx Run npx [ARG...]
yarn Run yarn [ARG...]";}
validate_command() { # still not sure if I should validate command
if [[ ! $1 =~ ^(node|npm|npx|yarn|yarnpkg|sh)$ ]]; then
echo 'INVALID_COMMAND';
fi
}
if [[ "$#" -le 0 || "$1" = '-h' || "$1" = '--help' ]]; then
_help;
else
if [ ! -z $(validate_command $1) ]; then
echoerr "Unknown command $1";
return 1;
else
local node_version=$(get_active_version);
local node_major_version=$(echo "${node_version}" | grep --color=never -Eo ^[0-9]+)
local docker_vol="dnode_modules_${node_major_version}";
docker volume create "${docker_vol}" > /dev/null;
local CMD=(docker run);
## TODO: figure out how to create a container once and then just keep running stuff in it
## figure out if it ha any performance advantages
CMD=(${CMD[*]} --rm);
CMD=(${CMD[*]} -it);
## ^ should not open tty by default for everything
## should detect and add flag only when needed
CMD=(${CMD[*]} --network host);
CMD=(${CMD[*]} -w "/.dnode${PWD}");
if [ 'node' = "$1" ]; then
local js_file;
for arg in $@; do
if [ -f "$arg" ]; then
js_file="$arg";
break;
fi
done
if [ ! -z "$js_file" ]; then
local mount_dir="$(dirname "$(realpath1 "${js_file}")")"
CMD=(${CMD[*]} -v "${mount_dir}:/.dnode${mount_dir}");
else
CMD=(${CMD[*]} -v "${PWD}:/.dnode${PWD}");
fi
else
CMD=(${CMD[*]} -v "${PWD}:/.dnode${PWD}");
fi
CMD=(${CMD[*]} -v "${docker_vol}:/usr/local/lib/node_modules");
CMD=(${CMD[*]} "node:${node_version}-alpine");
CMD=(${CMD[*]} $@);
${CMD[*]};
fi
fi
}
validate_img_tag() {
docker image inspect "${1}" >/dev/null 2>&1 || echo 'INVALID_IMAGE';
}
dn_switch() {
_help() { echoerr "
Usage: dn switch [OPTIONS] VERSION
Switch to specified installed VERSION. Will fail if VERSION is not installed.
Options:
-g, --global Affect global version when switching
-q, --quiet Do not show after switch";}
if [ $# -le 0 ] ; then
_help;
else
local is_global is_help is_quiet;
while [[ $# -gt 0 && $1 == -* ]]; do
case "$1" in
-g|--global) is_global=1 ;;
-h|--help) is_help=1 ;;
-q|--quiet) is_quiet=1 ;;
*)
echoerr "Unknown option $1";
return 1
;;
esac
shift
done
if [ ! -z $is_help ]; then
_help;
return 0;
fi
local node_version="${1}"
if [ ! -z $(validate_version "${node_version}") ]; then
echoerr "Invalid version: ${node_version}";
return 1;
fi
if [ ! -z $(validate_img_tag "node:${node_version}-alpine") ]; then
echoerr "Version ${node_version} is not installed; to install, run: dn add ${node_version}";
return 1;
fi
if [ ! -z $is_global ]; then
printf "${node_version}" > "${DN_INSTALL_DIR}/.dnode_version";
else
printf "${node_version}" > .dnode_version;
fi
if [ -z $is_quiet ]; then
dn_show;
fi
fi
}
dn_forget() {
_help() { echoerr "
Usage: dn forget
Forget local Node version setting and use the next in line version.";}
if [ $# -gt 0 ]; then
_help;
else
rm -f .dnode_version
fi
}
dn_use_global() {
_help() { echoerr "
Usage: dn use-global
Use the current global Node version setting.";}
if [ $# -gt 0 ]; then
_help;
else
printf '@global' > .dnode_version;
dn_show;
fi
}
dn_info() {
local node_version node_version_type;
local flag_file_path=$(get_local_version_flag_path);
if [[ ! -f ${flag_file_path} || $(< ${flag_file_path}) = '@global' ]]; then
node_version_type='global';
node_version=$(get_active_version_global);
if [ -z $node_version ]; then node_version='N/A'; fi
else
node_version_type='local';
node_version=$(< ${flag_file_path});
fi
echoerr "
DN: Docker Powered Node Version Manager 🐳 + ⬢
DN version: $DN_VERSION
Status: $( test $DN_DISABLED = 'true' && echo DISABLED || echo ENABLED )
Node version: $node_version ($node_version_type)
Source: ${DN_REPO_URL}"
}
dn_add_and_switch() {
_help() { echoerr "
Usage: dn_add_and_switch IS_GLOBAL IS_PULL VERSION
IS_GLOBAL Determines whether to change global version (1) or not (0)
IS_PULL Determines whether to force pull docker image (1) or not (0)
VERSION Node version to add/switch";}
local is_global=$1 is_pull=$2 version=$3;
if [ "${is_pull}" = '1' ] || [ ! -z $(validate_img_tag "node:${version}-alpine") ] ; then
dn_add "${version}";
fi
if [ "${is_global}" = '1' ]; then
dn_switch -g "${version}";
else
dn_switch "${version}";
fi
}
dn_ls() {
_help() { echoerr "
Usage: dn ls
List installed versions.";}
case "$1" in
-h|--help)
_help
;;
*)
local node_version=( $(docker image list "node:*-alpine" --format={{.Tag}} \
| tag_to_version \
| sort --version-sort) )
local active_version=$(get_active_version);
local prefix=' ';
for i in ${!node_version[*]} ; do
if [ "${node_version[$i]}" != "${active_version}" ]; then
echo " ${node_version[$i]}"
else
echo "* ${node_version[$i]}"
fi
done
;;
esac
}
get_local_version_flag_path() {
local this_dir="$1"
if [ -z "${this_dir}" ]; then
this_dir="$PWD"
fi
if [ -f "${this_dir}/.dnode_version" ]; then
echo "${this_dir}/.dnode_version";
elif [ "/" != "${this_dir}" ]; then
get_local_version_flag_path "$(dirname "${this_dir}")"
fi
}
get_active_version_local() {
# returns the active node version. If no local version found, returns the global configured
local flag_file_path=$(get_local_version_flag_path);
if [ -f "${flag_file_path}" ]; then
local active_version_local="$(< ${flag_file_path})";
if [ "${active_version_local}" = '@global' ]; then
get_active_version_global;
else
echo "${active_version_local}";
fi
fi
}
get_active_version_global() {
if [ -f "${DN_INSTALL_DIR}/.dnode_version" ]; then
echo "$(< "${DN_INSTALL_DIR}/.dnode_version")"
fi
}
get_active_version() {
local local_version="$(get_active_version_local)";
if [ ! -z "${local_version}" ]; then
echo "${local_version}"
else
get_active_version_global;
fi
}
dn_show() {
_help() { echoerr "
Usage: dn show
Display information about currently active node version.";}
case "$1" in
-h|--help)
_help
;;
*)
local active_version=$(get_active_version);
if [ -z ${active_version} ]; then
echo 'N/A';
else
local full_version=$(docker image inspect node:${active_version}-alpine \
| jq -r '.[].Config.Env[]' \
| grep NODE_VERSION \
| grep -Eo --color=never '[0-9]+(\.[0-9]+){0,2}');
echo "$active_version ($full_version)"
fi
;;
esac
}
dn_inspect() {
local active_version=$(get_active_version);
if [ -z ${active_version} ]; then
echoerr "Node version undefined! There may be something wrong with your installation."
return 1;
else
docker run --rm node:${active_version}-alpine sh -c "echo \"
Node version: \$NODE_VERSION
Npm version: \$(npm -v)
Yarn version: \$YARN_VERSION
Container OS: \$(cat /etc/os-release | grep -- 'PRETTY_NAME=' | grep -o [^=]*$)\"";
fi
}
get_auth_token() {
# returns a docker hub annonymous auth token to pull from $repo
# Example: get_auth_token library/node
local repo=$1
curl \
--silent \
"https://auth.docker.io/token?scope=repository:$repo:pull&service=registry.docker.io" \
| jq -r '.token'
}
get_tags() {
# Retrieves all $repo tags from dockerhub
# Exapmple: get_tags library/node
local repo=$1
local token=$(get_auth_token $repo)
curl\
--silent \
--header "Authorization: Bearer $token" \
"https://registry-1.docker.io/v2/$repo/tags/list" \
| jq -r '.tags[]'
}
tag_to_version() {
# Converts x[.y[.z]]-alpine tags to versions. Accepts pipe.
# Example: tag_to_version 12-alpine 12.1.3-alpine 14.0.4-alpine
# docker images "node:*-alpine" --format={{.Tag}} | tag_to_version
if [ "$#" -gt 0 ]; then
for t in $@ ; do echo "$t"; done \
| grep --color=never -E '^[0-9]+(\.[0-9]+){0,2}-alpine$' \
| grep --color=never -Eo '^[0-9]+(\.[0-9]+){0,2}'
else
while read t ; do echo "$t"; done \
| grep --color=never -E '^[0-9]+(\.[0-9]+){0,2}-alpine$' \
| grep --color=never -Eo '^[0-9]+(\.[0-9]+){0,2}'
fi
}
get_node_versions() {
# Retrieves all available alpine versions from docker-hub
get_tags library/node | tag_to_version | sort --version-sort
}
dn_search() {
_help() { echoerr "
Search available versions.
Usage:
dn search PREFIX Search versions by version prefix
dn search -a|--all Get full list of versions";}
if [ $# -le 0 ]; then
_help;
else
case "${1}" in
-h|--help)
_help
;;
-a|--all)
get_node_versions | grep --color=never -Eo '^[0-9]+\.[0-9]+\.[0-9]+'
;;
*)
local prefix="${1}";
if [ ! -z $(validate_version "${prefix}") ]; then
echoerr 'Prefix must be a valid version number of the form x[.y[.z]]';
_help;
return 1;
else
get_node_versions \
| grep --color=never -Eo "^${prefix}.*" \
| grep --color=never -Eo '^[0-9]+\.[0-9]+\.[0-9]+';
fi
;;
esac
fi
}
_main() {
_help() { echoerr "
DN: Docker Powered Node Version Manager 🐳 + ⬢
Usage: dn [OPTIONS] COMMAND
Options:
-g, --global Affect global version when switching
-p, --pull Forces pull when switching
-v, --version Show installed dn version
Commands:
x[.y[.z]] Switch to version x.y.z (install if missing)
add Install one or more node version
forget Forget directory local setting
info Display detailed informatoin about dn
inspect Display detailed information about active node version
install Install dn on your system
ls List installed versions
off Disable dn
on Enable dn
rm Delete a version
search Search available versions
show Show active Node version
switch Switch to installed version
uninstall Uninstall dn from your system
use-global Use global setting in directory
Run 'dn COMMAND --help' for more information on a command."; }
if [ $# -le 0 ]; then
_help;
return 0;
else
local is_global is_pull is_help is_version;
while [[ $# -gt 0 && "${1}" == -* ]]; do
case "$1" in
-g|--global) is_global=1 ;;
-p|--pull) is_pull=1 ;;
-h|--help) is_help=1 ;;
-v|--version) is_version=1 ;;
*) echoerr "Unknown option $1"; return 1 ;;
esac
shift
done
if [ ! -z "${is_help}" ]; then
_help;
elif [ ! -z "${is_version}" ]; then
echo "${DN_VERSION}";
else
local args=("$@");
local rest_args=${args[*]:1};
case "$1" in
+([0-9])?(\.+([0-9]))?(\.+([0-9])))
dn_add_and_switch "${is_global}" "${is_pull}" "${1}" ;;
add) dn_add ${rest_args[*]} ;;
forget) dn_forget ;;
inf|info) dn_info ;;
inspect) dn_inspect ;;
install) dn_install ;;
ls|ll|list) dn_ls ${rest_args[*]} ;;
off) dn_off ;;
on) dn_on ;;
rm|remove) dn_rm ${rest_args[*]} ;;
run) dn_run ${rest_args[*]} ;;
search) dn_search ${rest_args[*]} ;;
show) dn_show ${rest_args[*]} ;;
switch) dn_switch ${rest_args[*]} ;;
uninstall) dn_uninstall ;;
use-global) dn_use_global ;;
*) _help ;;
esac
fi
fi
}
_main $@
# remove flags in case this is sourced
set +e