Skip to content

Commit 315ca23

Browse files
npm package feature
1 parent edabf7f commit 315ca23

6 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "npm package",
3+
"id": "npm-package",
4+
"version": "1.0.0",
5+
"description": "Installs an npm package.",
6+
"documentationURL": "http://github.com/devcontainers-contrib/features/tree/main/src/npm-package",
7+
"installAfter": [
8+
"ghcr.io/devcontainers/features/node:latest"
9+
],
10+
"options": {
11+
"package": {
12+
"type": "string",
13+
"proposals": [
14+
"typescript",
15+
"vtop",
16+
"fkill-cli"
17+
],
18+
"default": "",
19+
"description": "Select the npm package to install."
20+
},
21+
"version": {
22+
"type": "string",
23+
"proposals": [
24+
"latest"
25+
],
26+
"default": "latest",
27+
"description": "Select the version of the npm package to install."
28+
}
29+
}
30+
}

src/npm-package/install.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "list --short | grep black" list --short | grep black
8+
9+
10+
reportResults
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "list --short | grep black" list --short | grep black
8+
9+
10+
reportResults

test/npm-package/scenarios.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"install_angular_cli": {
3+
"image": "debian:bullseye",
4+
"features": {
5+
"npm-package": {
6+
"version": "latest",
7+
"package": "@angular/cli"
8+
}
9+
}
10+
},
11+
"install_typescript": {
12+
"image": "debian:bullseye",
13+
"features": {
14+
"npm-package": {
15+
"version": "latest",
16+
"package": "typescript"
17+
}
18+
}
19+
}
20+
}

test/npm-package/test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "nothing is installted when package is empty" exit 0
8+
9+
reportResults

0 commit comments

Comments
 (0)