]> git.wincent.com - wincent.git/blob - bin/common
chore(node): install latest, on Arch Linux too
[wincent.git] / bin / common
1 #!/bin/bash
2
3 # https://stackoverflow.com/a/2684300
4 if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
5   echo "[error] script \"$0\" is intended to be sourced, not executed directly"
6   exit 1
7 fi
8
9 # Get directory of current script:
10 #
11 #     https://stackoverflow.com/a/12694189/2103996
12
13 # and convert to absolute path:
14 #
15 #     https://stackoverflow.com/a/4045350/2103996
16 #
17 REPO_ROOT=$(cd "${BASH_SOURCE%/*}/.." && pwd)
18
19 BIN_DIR="$REPO_ROOT/bin"
20
21 NODE_VERSION=16.13.2
22
23 export PATH="$BIN_DIR":$PATH
24
25 # https://stackoverflow.com/a/5947802/2103996
26 BOLD='\033[0;1m'
27 GREEN='\033[1;32m'
28 PURPLE='\033[1;35m'
29 RED='\033[1;31m'
30 RESET='\033[0m'
31 YELLOW='\033[1;33m'
32
33 # No associative arrays in Bash < v4, so can't do:
34 #
35 #   declare -A LOG_LEVEL
36 #
37 LOG_LEVEL_EMERGENCY=0
38 LOG_LEVEL_ALERT=1
39 LOG_LEVEL_CRITICAL=2
40 LOG_LEVEL_ERROR=3
41 LOG_LEVEL_WARNING=4
42 LOG_LEVEL_NOTICE=5
43 LOG_LEVEL_INFO=6
44 LOG_LEVEL_DEBUG=7
45
46 LOG_LEVEL=$LOG_LEVEL_INFO
47
48 for VAR in "$@"
49 do
50   if [[ $VAR = "--debug" ]]; then
51     LOG_LEVEL=$LOG_LEVEL_DEBUG
52     DEBUG=1
53   elif [[ $VAR = "--quiet" || $VAR = "-q" ]]; then
54     LOG_LEVEL=$LOG_LEVEL_ERROR
55     unset DEBUG
56   fi
57 done
58
59 log_debug() {
60   if [[ $LOG_LEVEL -ge $LOG_LEVEL_DEBUG ]]; then
61     echo -e "${PURPLE}[debug]  ${RESET} $*" > /dev/stderr
62   fi
63 }
64
65 log_error() {
66   if [[ $LOG_LEVEL -ge $LOG_LEVEL_ERROR ]]; then
67     echo -e "${RED}[error]  ${RESET} $*" > /dev/stderr
68   fi
69 }
70
71 log_info() {
72   if [[ $LOG_LEVEL -ge $LOG_LEVEL_INFO ]]; then
73     echo -e "${BOLD}[info]   ${RESET} $*" > /dev/stderr
74   fi
75 }
76
77 log_notice() {
78   if [[ $LOG_LEVEL -ge $LOG_LEVEL_NOTICE ]]; then
79     echo -e "${GREEN}[notice] ${RESET} $*" > /dev/stderr
80   fi
81 }
82
83 log_warn() {
84   if [[ $LOG_LEVEL -ge $LOG_LEVEL_WARNING ]]; then
85     echo -e "${YELLOW}[warning]${RESET} $*" > /dev/stderr
86   fi
87 }