From 4d2ad7ed9d0956d633feb366616e6d131c7f51b9 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Tue, 14 Sep 2021 13:35:03 -0700 Subject: [PATCH] Add nvm and yarn --- init.sh | 16 +++----- lib/nvm.sh | 85 +++++++++++++++++++++++++++++++++++++++++++ lib/shasum.sh | 10 +++++ lib/yarn.sh | 19 ++++++++++ test/shasum_test.bats | 16 ++++++++ 5 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 lib/nvm.sh create mode 100644 lib/yarn.sh diff --git a/init.sh b/init.sh index fe0e0be..bf2f1fe 100644 --- a/init.sh +++ b/init.sh @@ -23,11 +23,10 @@ set +ex export SCRIPT_SOURCE="$(cd "$(/usr/bin/dirname "${BASH_SOURCE[0]:-"${(%):-%x}"}")" || exit 1; pwd -P)" -export PATH="/usr/local/bin:/usr/bin:/bin:/sbin:${PATH}" +export PATH="${PATH}:/usr/local/bin:/usr/bin:/bin:/sbin" export BASHMATIC_HOME=${BASHMATIC_HOME:-${SCRIPT_SOURCE}} export BASHMATIC_MAIN=${BASHMATIC_HOME}/init.sh - function bashmatic.home.valid() { [[ -n $BASHMATIC_HOME && -d ${BASHMATIC_HOME} && -s ${BASHMATIC_HOME}/init.sh ]] } @@ -59,8 +58,6 @@ bashmatic.home.valid || { return 1 } -source ${BASHMATIC_HOME}/.bash_path_resolver - export GREP_CMD="$(command -v /usr/bin/grep || command -v /bin/grep || command -v /usr/local/bin/grep || echo grep)" # shellcheck disable=SC2002 export BASHMATIC_VERSION="$(cat "${BASHMATIC_HOME}/.version" | /usr/bin/tr -d '\n')" @@ -227,11 +224,9 @@ function bashmatic.init.paths() { function bashmatic.init() { - local -a args=($@) - - for file in "${args[@]}"; do - [[ $0 =~ "$1" ]] && { - log.inf "skipping the first file ${file}" + for file in "$@"; do + [[ $0 =~ $file ]] && { + log.inf "skipping the first file ${file}" continue } local env_file="${BASHMATIC_HOME}/.envrc.${file}" @@ -252,9 +247,10 @@ function bashmatic.init() { log.inf "calling bashhmatic.init" bashmatic.init "$@" } + local init_func=".bashmatic.init.${BASHMATIC_OS}" - [[ -n $(type "${init_func}" 2>/dev/null) ]] && ${init_func} + [[ -n $(typle "${init_func}" 2>/dev/null) ]] && ${init_func} local setup_script="${BASHMATIC_LIBDIR}/bashmatic.sh" diff --git a/lib/nvm.sh b/lib/nvm.sh new file mode 100644 index 0000000..642282f --- /dev/null +++ b/lib/nvm.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash + +declare -a __bashmatic_nvm_dirs + +export __bashmatic_nvm_dirs=( + "${HOME}/.nvm" + /usr/local/Cellar/nvm + /usr/local/opt/nvm +) +export __default_nvm_home="${__bashmatic_nvm_dirs[0]}" + +# @description Returns true if NVM_DIR is correctly set, OR if +# a directory passed as an argument contains nvm.sh +function nvm.is-valid-dir() { + [[ -z $1 ]] && return 1 + local d="$1" + [[ -d "${d}" && -s "${d}/nvm.sh" ]] +} + +# @description +# Returns success and exports NVM_DIR whenver nvm.sh is found underneath any +# of the possible locations tried. +function nvm.detect() { + for dir in "${__bashmatic_nvm_dirs[@]}"; do + nvm.is-valid-dir "${dir}" && { + export NVM_DIR="${dir}" + source "${NVM_DIR}/nvm.sh" + return 0 + } + done + return 1 +} + +# @description Installs NVM via Curl if not already installed. +function nvm.install() { + h.blue "Installing nvm_.." + # This both installs NVM and also evals the lines it outputs during installation matching NVM_DIR + # this ensures that NVM is fully loaded after this line + local temp="/tmp/nvm_$$" + (curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh 2>/dev/null| bash 2>/dev/null) | grep NVM_DIR > "${temp}" + # Honestly we probably don't need to do this, since we still call nvm_load later... + # But then we have to guess where NVM_DIR once again, when we can just eval it and be done with it... + source "${temp}" + rm -f "${temp}" + return 0 +} + +function nvm.use() { + [[ -s .nvmrc ]] || return 1 + is.a-function nvm || nvm.load + local node_version="$(cat .nvmrc | head -1)" + info "Activating NodeJS ${bldred}${node_version}..." + nvm use "${node_version}" && return 0 + + warning "No version ${node_version} was detected, installing" + run.set-next show-output-on + run "nvm install ${node_version}" + + nvm use "${node_version}" && return 0 + error "Unable to install node version ${node_version}" + return 1 +} + +# @description Loadd +function nvm.load() { + nvm.detect || nvm.install + is.a-function nvm || { + [[ -s "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh" + [[ -s "$NVM_DIR/bash_completion" ]] && source "$NVM_DIR/bash_completion" + } + + [[ -f .nvmrc ]] && { + nvm use 1>/dev/null 2>&1 || { + h3 "NVM must install Node version $(cat .nvmrc), please wait..." + run "nvm install && nvm use" + } + } +} + + +function nvm.activate() { + nvm.load +} + +[[ -f .nvmrc ]] && nvm.activate diff --git a/lib/shasum.sh b/lib/shasum.sh index 763a698..d5eebfc 100644 --- a/lib/shasum.sh +++ b/lib/shasum.sh @@ -111,3 +111,13 @@ shasum.all-files-in-dir() { # shellcheck disable=2046 shasum.sha $(eval "find \"${dir}\" -type f ${name_pattern}" ) | awk '{print $2 " " $1}' | sort | .sha-only } + +# @description sha256 +function sha() { + if output.has-stdin; then + shasum -a 256 "$@" | cut -d ' ' -f 1 + else + shasum -a 256 "$@" | cut -d ' ' -f 1 + fi +} + diff --git a/lib/yarn.sh b/lib/yarn.sh new file mode 100644 index 0000000..984eaa0 --- /dev/null +++ b/lib/yarn.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# @description Installs YARN via npm if not found; then runs yarn install +# Note that yarn install is skipped if package.json and yarn.lock haven't +# changed since the last run of yarn install. +function yarn_install() { + command -v yarn>/dev/null || npm install -g yarn + if [[ ! -f .yarn.sha || "$(cat .yarn.sha)" != "$(yarn_sha)" ]]; then + set -x + yarn install + set +x + yarn_sha > .yarn.sha + fi +} + +# @description Prints to STDOUT the SHA based on package.json and yarn.lock +function yarn_sha() { + [[ -f package.json && -f yarn.lock ]] && cat package.json yarn.lock | sha +} diff --git a/test/shasum_test.bats b/test/shasum_test.bats index bd6e7e0..da6ddff 100644 --- a/test/shasum_test.bats +++ b/test/shasum_test.bats @@ -43,4 +43,20 @@ set -e [[ ${sha1} == "f9f90c8b04fa1bb7cbe04f6e8e468d163a87e66c" ]] } +export sha="5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" +@test "sha() file" { + set -e + local t="/tmp/hello.txt" + echo "hello" > $t + local sha_file="$(sha $t)" + [[ "${sha}" == "${sha_file}" ]] +} + + +@test "sha() pipe" { + set -e + local sha="5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" + local sha_pipe="$(echo hello | sha)" + [[ "${sha}" == "${sha_pipe}" ]] +}