grezzo/featlib/npm.sh

116 lines
2.5 KiB
Bash
Executable File

#! /bin/bash --
[ -n "${GREZZO_1577142009-}" ] && return; GREZZO_1577142009=0
function feat_addpacks_npm_dev() {
if ! feat_missing "apt-get"; then
sudo apt-get update
sudo apt-get install -y wget curl tar
elif ! feat_missing "pacman"; then
sudo pacman -S --needed base-devel wget curl tar
elif ! feat_missing "apk"; then
sudo apk add --no-cache wget curl tar bash
elif ! feat_missing "yum"; then
sudo yum install wget curl tar
elif ! feat_missing "zypper"; then
sudo zypper in wget curl tar
fi
}
function feat_add_npm() {
feat_addpacks_npm_dev
[ -f "$HOME/.bashrc.before" ] || touch "$HOME/.bashrc.before"
cat - "$HOME/.bashrc.before" <<- EOF > "$HOME/.bashrc.before.tmp"
export N_PREFIX="\$HOME/.local"
export PATH="\$N_PREFIX/bin:\$PATH"
EOF
mv "$HOME/.bashrc.before.tmp" "$HOME/.bashrc.before"
feat_bashrc_before_needed
. "$HOME/.bashrc.before"
mkdir -p "$N_PREFIX"
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s latest
npm install -g n
npm install -g npm-check-updates
}
function npm_initialized() {
[ -s 'package.json' ]
}
function npm_feat_added() {
npm list 2>&1 | grep -q "$FEAT@"
}
function npm-g_feat_added() {
npm list -g 2>&1 | grep -q "$FEAT@"
}
function npm_feat_missing() {
npm list 2>&1 | grep -q 'missing[^ ]* *'"$FEAT"'@'
}
function npm_feat_ensure_ncu() {
if feat_missing "ncu"; then
npm install -g npm-check-updates
fi
}
function npm_feat_update_latest() {
npm_feat_ensure_ncu
ncu -u
npm install
}
function npm-g_feat_add() {
npm install -g npm
npm install -g "$1"
}
function npm_feat_add() {
npm install -g npm
npm install "$1"
}
function npm_feat_ensure() {
# Ricarica .bashrc.before per eventuali modifiche al PATH
if [ -f "$HOME/.bashrc.before" ]; then
source "$HOME/.bashrc.before"
fi
# Se npm funziona, ok
if npm --version >/dev/null 2>&1; then
return
fi
# Altrimenti installiamo tutto
feat_needed "npm"
}
function npm_feat_needed() {
npm_feat_ensure
FEAT="$1"
npm_feat_ensure_ncu
if ! npm_initialized; then
npm init -y
fi
if ! npm_feat_added "$FEAT"; then
if type "npm_feat_add_$FEAT" > /dev/null 2>&1 ; then
eval "npm_feat_add_$FEAT"
else
npm_feat_add "$FEAT"
fi
fi
npm_feat_update_latest
}
function npm-g_feat_needed() {
npm_feat_ensure
FEAT="$1"
if ! npm-g_feat_added "$FEAT"; then
if type "npm-g_feat_add_$FEAT" > /dev/null 2>&1 ; then
eval "npm-g_feat_add_$FEAT"
else
npm-g_feat_add "$FEAT"
fi
fi
}