|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +PACKAGE=${PACKAGE:-""} |
| 5 | +VERSION=${VERSION:-"latest"} |
| 6 | + |
| 7 | +# Clean up |
| 8 | +rm -rf /var/lib/apt/lists/* |
| 9 | + |
| 10 | +if [ -z "$PACKAGE" ]; then |
| 11 | + echo -e "'package' variable is empty, skipping" |
| 12 | + exit 0 |
| 13 | +fi |
| 14 | + |
| 15 | +if [ "$(id -u)" -ne 0 ]; then |
| 16 | + echo -e 'Script must be run as |
| 17 | + root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +check_packages() { |
| 22 | + # This is part of devcontainers-contrib script library |
| 23 | + # source: https://github.com/devcontainers-contrib/features/tree/v1.1.8/script-library |
| 24 | + if ! dpkg -s "$@" >/dev/null 2>&1; then |
| 25 | + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then |
| 26 | + echo "Running apt-get update..." |
| 27 | + apt-get update -y |
| 28 | + fi |
| 29 | + apt-get -y install --no-install-recommends "$@" |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +install_via_npm() { |
| 34 | + # This is part of devcontainers-contrib script library |
| 35 | + # source: https://github.com/devcontainers-contrib/features/tree/v1.1.8/script-library |
| 36 | + PACKAGE=$1 |
| 37 | + |
| 38 | + # install node+npm if does not exists |
| 39 | + if ! type npm >/dev/null 2>&1; then |
| 40 | + echo "Installing node and npm..." |
| 41 | + check_packages curl ca-certificates |
| 42 | + curl -fsSL https://raw.githubusercontent.com/devcontainers/features/main/src/node/install.sh | bash |
| 43 | + export NVM_DIR=/usr/local/share/nvm |
| 44 | + [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" |
| 45 | + [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" |
| 46 | + fi |
| 47 | + |
| 48 | + if [ "$(npm list --global --parseable --depth 0 --omit dev | grep "$PACKAGE")" != "" ]; then |
| 49 | + echo "$PACKAGE already exists - skipping installation" |
| 50 | + exit 0 |
| 51 | + fi |
| 52 | + |
| 53 | + if [ "$VERSION" = "latest" ]; then |
| 54 | + npm_installation="$PACKAGE" |
| 55 | + else |
| 56 | + npm_installation="$PACKAGE==$VERSION" |
| 57 | + fi |
| 58 | + |
| 59 | + npm install -g --omit=dev "$npm_installation" |
| 60 | +} |
| 61 | + |
| 62 | +install_via_npm "$PACKAGE" "$VERSION" |
0 commit comments