Skip to content

Instantly share code, notes, and snippets.

@krisdante
Created March 30, 2023 10:45
Show Gist options
  • Save krisdante/cb7b08754c9157f128274c06c6887f29 to your computer and use it in GitHub Desktop.
Save krisdante/cb7b08754c9157f128274c06c6887f29 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Common library parts and setup sensible envirionment
# on magento cloud deployment env
#
[[ "${BASH_VERSINFO[0]}" -lt 4 ]] && echo "FATAL: bash >= 4 required" >&2 && exit 199
if [ -z "${LIB_MGS_COMMON_INCLUDED}" ];then
LIB_MGS_COMMON_INCLUDED=1
set -e
unalias echo 2>/dev/null || true
log::_parent() {
local offset=${1:-0}
printf "%-25s" "${FUNCNAME[2+offset]}:${BASH_LINENO[1+offset]}"
}
log::info() {
echo -e "\033[0;34m$(log::_parent) [INFO] $*\033[0m" >&2
}
log::warn() {
echo -e "\033[0;33m$(log::_parent) [WARN] $*\033[0m" >&2
}
log::success() {
echo -e "\033[0;32m$(log::_parent) [ OK ] $*\033[0m" >&2
}
log::error() {
echo -e "\033[0;31m$(log::_parent) [ !! ] $*\033[0m" >&2
}
# $1 is offset for responsible error line,
# should used only in lib parts
# exits with error code
log::panic() {
local offset=${1:-0}
shift
echo -e "\033[0;31m$(log::_parent "$offset") [ !! ] $*\033[0m" >&2
exit 1
}
exec::is_cmd_available() {
local cmd=$1
command -v "$cmd" &>/dev/null || return 1
}
# Aborts
exec::assert_command() {
local cmd=$1
local error_msg=${2:-$cmd is not available}
if ! exec::is_cmd_available "$cmd";then
log::panic 1 "$error_msg"
fi
}
fs::find_writable_dir() {
local dir
for dir in "$@";do
if [ -d "$dir" ] && [ -r "$dir" ] \
&& [ -w "$dir" ] && [ -x "$dir" ];then
echo $dir;
return
fi
done
log::error "Failed to locate writable directory!"
exit 1
}
utils::join_by() {
local IFS="$1"
shift
echo "$*"
}
fi
#!/usr/bin/env bash
#
# Library to support custom node version
#
# Usage:
#
# nvm::init 10
# nvm::npm install -g some_cli_tool
# nvm::run some_cli_tool -v
# nvm::cleanup
#
if [ -z "${LIB_MGS_NVM_INCLUDED}" ];then
LIB_MGS_NVM_INCLUDED=1
. "$(dirname ${BASH_SOURCE[0]})/mgs-common.sh"
nvm__dir=""
nvm__home=""
nvm__version=""
nvm__installed_version=""
nvm__nvm_version="0.35.2"
# configure module
nvm::init() {
local version=$1
nvm__version="$version"
}
# cleanup installed files
nvm::cleanup() {
if [ -n "$nvm__dir" ];then
rm -rf "$nvm__dir"
nvm__dir=""
nvm__home=""
nvm__installed_version=""
fi
}
# run command in nvm env
nvm::run() {
nvm::_assert_install
nvm::_exec "$@"
}
# run npm from nvm env
nvm::npm() {
nvm::_assert_install
nvm::_npm_no_install "$@"
}
nvm::_exec() {
HOME="$nvm__home" NVM_DIR="$nvm__dir" NPM_CONFIG_PREFIX="" . "${nvm__dir}/nvm.sh" &> /dev/null
HOME="$nvm__home" NVM_DIR="$nvm__dir" NPM_CONFIG_PREFIX="" "$@" || log::panic 1 "$@ failed"
HOME="$nvm__home" NVM_DIR="$nvm__dir" NPM_CONFIG_PREFIX="" nvm deactivate &> /dev/null
}
nvm::_npm_no_install() {
nvm::_exec npm "$@"
}
nvm::_nvm_no_install() {
nvm::_exec nvm "$@"
}
nvm::_assert_install() {
if [ -z "${nvm__version}" ];then
# npm -> _npm_no_install -> _assert_install
log::panic 3 'Node version not selected, use nvm::init "<version>"'
fi
if [ "${nvm__installed_version}" != "${nvm__version}" ];then
nvm::_install
fi
}
nvm::_install() {
log::info "Starting nvm instalation"
exec::assert_command curl
nvm__dir="$(mktemp -d)"
nvm__home="${nvm__dir}"
log::success "Selected directory for nvm instalation: ${nvm__dir}"
# Make sure we don't use any previous incomplete or outdate installation
rm -rf "$nvm__dir" "$nvm__home"
mkdir -p "$nvm__dir" "$nvm__home" >/dev/null
curl -o- "https://raw.githubusercontent.com/nvm-sh/nvm/v${nvm__nvm_version}/install.sh" | NVM_DIR="${nvm__dir}" METHOD="script" PROFILE="/dev/null" bash
log::info "Installing node:${nvm__version}"
nvm::_nvm_no_install install "${nvm__version}"
nvm::_nvm_no_install use "${nvm__version}"
nvm::_npm_no_install config set update-notifier false &> /dev/null || log::panic 0 "npm configuration failed"
log::success "Node:${nvm__version} installed"
nvm__installed_version="${nvm__version}"
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment