dotfiles/devel/nodejs.sh

112 lines
3.3 KiB
Bash
Raw Normal View History

2020-11-21 18:07:26 +00:00
#!/bin/sh
DIR=$(dirname "$(readlink -f "$0")")
. "$DIR/../env.sh"
FILES_PATH=$PREFIX/nodejs
NODE_BIN=$PREFIX/bin/node
NODE_URL=https://nodejs.org/dist
2021-10-09 15:19:35 +00:00
NODE_VERSION=v14.18.0
NPM_BIN=npm
2021-10-09 15:19:35 +00:00
NPM_CONFIG_REGISTRY=
if in_china; then
2021-10-09 15:19:35 +00:00
export NPM_CONFIG_REGISTRY=https://registry.npmmirror.com
NODE_URL=https://npmmirror.com/mirrors/node
2021-08-10 08:30:05 +00:00
NPM_BIN=npm
fi
export NPM_BIN
2020-11-21 18:07:26 +00:00
log "Setting up nodejs"
lspkgs() {
curl -s $NODE_URL/index.json \
| jq '.[] | "\(.version) \(if .lts then "(lts)" else "" end)"' -r
}
uninstall() {
if [ -f "$FILES_PATH" ]; then
tac "$FILES_PATH" | grep -v '/$' | xargs -n 10 -- sudo rm -f
fi
2020-11-21 18:07:26 +00:00
}
install() {
VERSION=$1
ARCH=x86
if [ -x "$NODE_BIN" ] && [ "$("$NODE_BIN" --version)" = "$VERSION" ]; then
return
fi
2020-11-21 18:07:26 +00:00
case "$(uname -m)" in
x86_64)
ARCH=x64
;;
armv6|armv6l)
ARCH=armv6l
;;
armv7|armv7l)
ARCH=armv7l
;;
armv8|armv8l|aarch64)
ARCH=arm64
;;
esac
NAME=node-$VERSION-linux-$ARCH.tar.gz
# download package
echo wget -O "/tmp/$NAME" "$NODE_URL/$VERSION/$NAME"
wget -O "/tmp/$NAME" "$NODE_URL/$VERSION/$NAME"
# remove previous installed
uninstall
# install and save extracted paths
sudo tar zxvf "/tmp/$NAME" --strip 1 -C "$PREFIX" \
| sed -r "s|^[^/]+/|$PREFIX/|" \
| sudo tee "$FILES_PATH" >/dev/null
# remove download path
rm "/tmp/$NAME"
2021-09-13 04:17:53 +00:00
configure_nodejs
}
configure_nodejs() {
if in_china; then
2021-10-09 15:19:35 +00:00
echo "registry=$NPM_CONFIG_REGISTRY" > ~/.npmrc
! has_cmd yarnpkg && sudo npm install -g yarn
yarnpkg config set registry $NPM_CONFIG_REGISTRY --global
2021-09-13 04:17:53 +00:00
#yarnpkg config set disturl https://npm.taobao.org/dist --global
#yarnpkg config set sass_binary_site https://npm.taobao.org/mirrors/node-sass --global
#yarnpkg config set electron_mirror https://npm.taobao.org/mirrors/electron/ --global
#yarnpkg config set puppeteer_download_host https://npm.taobao.org/mirrors --global
#yarnpkg config set chromedriver_cdnurl https://npm.taobao.org/mirrors/chromedriver --global
#yarnpkg config set operadriver_cdnurl https://npm.taobao.org/mirrors/operadriver --global
#yarnpkg config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs --global
#yarnpkg config set selenium_cdnurl https://npm.taobao.org/mirrors/selenium --global
#yarnpkg config set sqlite3_binary_host_mirror https://foxgis.oss-cn-shanghai.aliyuncs.com/ --global
#yarnpkg config set profiler_binary_host_mirror https://npm.taobao.org/mirrors/node-inspector/ --global
#yarnpkg config set chromedriver_cdnurl https://cdn.npm.taobao.org/dist/chromedriver --global
#yarnpkg config set node_inspector_cdnurl https://npm.taobao.org/mirrors/node-inspector --global
#yarnpkg config set sentrycli_cdnurl https://npm.taobao.org/mirrors/sentry-cli
fi
if enhance_vim; then
v -c "CocInstall -sync coc-json coc-vetur coc-tsserver coc-eslint|qall"
fi
2020-11-21 18:07:26 +00:00
}
case "$1" in
ls|list)
lspkgs | less
;;
2021-09-13 04:17:53 +00:00
config)
configure_nodejs
;;
2020-11-21 18:07:26 +00:00
uninstall)
uninstall
;;
'')
install "$NODE_VERSION"
;;
*)
install "$1"
;;
esac