Skip to content

Commit

Permalink
Add nvm and yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster committed Sep 14, 2021
1 parent 2bf7c80 commit 4d2ad7e
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 10 deletions.
16 changes: 6 additions & 10 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]]
}
Expand Down Expand Up @@ -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')"
Expand Down Expand Up @@ -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}"
Expand All @@ -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"

Expand Down
85 changes: 85 additions & 0 deletions lib/nvm.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions lib/shasum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

19 changes: 19 additions & 0 deletions lib/yarn.sh
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions test/shasum_test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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}" ]]
}

0 comments on commit 4d2ad7e

Please sign in to comment.