sergeykuzmich
3/26/2018 - 1:56 PM

lazy-load.sh

# BEGIN UGLY HACK
# This lets us initialize nvm, node and any other global node binaries on demand (lazily)
# to save on shell startup time
export NVM_DIR="$HOME/.nvm"
declare -a NODE_GLOBALS=(`find $NVM_DIR/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)

NODE_GLOBALS+=("node")
NODE_GLOBALS+=("nvm")

# Bootstrap nvm on demand
load_nvm() {
    source "/usr/local/opt/nvm/nvm.sh"

    # (Optional) Set the version of node to use from ~/.nvmrc if available
    nvm use 2> /dev/null 1>&2 || true

    # Do not reload nvm again
    export NVM_LOADED=1
}

for node_cmd in "${NODE_GLOBALS[@]}"; do
    eval "${node_cmd}() { unset -f ${node_cmd}; [ -z \${NVM_LOADED+x} ] && load_nvm; ${node_cmd} \$@; }"
done
# END UGLY HACK