Skip to content

Commit 94d4fee

Browse files
committed
feat(install-npm-package-globally): add feature to install npm package globally
1 parent d8d6bbe commit 94d4fee

File tree

8 files changed

+193
-0
lines changed

8 files changed

+193
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# install-npm-package-globally devcontainer feature
2+
3+
This feature will be install npm packages globally
4+
5+
## Example Usage
6+
7+
Install latest version
8+
9+
```json
10+
"features": {
11+
"ghcr.io/ebizbase/devcontainer-features/install-npm-package-globally:0": {
12+
"packages": ["ts-node"]
13+
}
14+
}
15+
```
16+
17+
Install specific version
18+
19+
```json
20+
"features": {
21+
"ghcr.io/ebizbase/devcontainer-features/install-npm-package-globally:0": {
22+
"packages": ["[email protected]"]
23+
}
24+
}
25+
```
26+
27+
Install semver version
28+
29+
```json
30+
"features": {
31+
"ghcr.io/ebizbase/devcontainer-features/install-npm-package-globally:0": {
32+
"packages": ["ts-node@~10.9.2"]
33+
}
34+
}
35+
```
36+
37+
## Options
38+
39+
| Options Id | Description | Type | Default |
40+
| ---------- | ------------------------------------------------------------------------------------------------------------------------------- | ----- | ------- |
41+
| packages | List of npm packages to install globally sparate by comma. We accept specific version with semver (eg 'typescript,nx@^20.0.12') | array | [] |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "install-npm-package-globally",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "devcontainer-features/install-npm-package-globally/src",
5+
"projectType": "application",
6+
"tags": [],
7+
"targets": {
8+
"test": {
9+
"executor": "nx:run-commands",
10+
"options": {
11+
"command": "devcontainer features test --skip-autogenerated -p devcontainer-features/install-npm-package-globally -f install-npm-package-globally -i mcr.microsoft.com/devcontainers/base:ubuntu"
12+
}
13+
},
14+
"publish": {
15+
"executor": "nx:run-commands",
16+
"options": {
17+
"command": "devcontainer features publish devcontainer-features/install-npm-package-globally/src/install-npm-package-globally --registry ghcr.io --namespace=ebizbase/devcontainer-features",
18+
"parallel": false
19+
}
20+
},
21+
"version": {
22+
"executor": "@jscutlery/semver:version",
23+
"options": {
24+
"preset": "angular"
25+
}
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "install-npm-package-globally",
3+
"version": "0.0.1",
4+
"name": "Installs an npm packages globally",
5+
"description": "Installs an npm packages globally via npm feature",
6+
"documentationURL": "https://github.com/ebizbase/dev-infras/tree/main/devcontainer-features/npm-global-package/README.md",
7+
"options": {
8+
"packages": {
9+
"type": "string",
10+
"default": "",
11+
"description": "List of npm packages to install globally sparate by comma. You can specific version with semver (eg 'typescript,nx^20.0.12')"
12+
}
13+
},
14+
"dependsOn": {
15+
"ghcr.io/devcontainers/features/node": {}
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
# trunk-ignore-all(shellcheck/SC2312)
4+
5+
PACKAGES=${PACKAGES:-""}
6+
7+
# Clean up
8+
rm -rf /var/lib/apt/lists/*
9+
10+
if [[ -z ${PACKAGES} ]]; then
11+
echo -e "'packages' 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+
PACKAGES=$(echo "${PACKAGES}" | tr ',' ' ')
22+
npm install -g "${PACKAGES}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# This test file will be executed against one of the scenarios devcontainer.json test that
4+
# includes the 'color' feature with "greeting": "hello" option.
5+
6+
set -e
7+
8+
# Optional: Import test library bundled with the devcontainer CLI
9+
source dev-container-features-test-lib
10+
11+
# Feature-specific tests
12+
# The 'check' command comes from the dev-container-features-test-lib.
13+
check "Checking ts-node avaiable" ts-node --version
14+
15+
# Report results
16+
# If any of the checks above exited with a non-zero exit code, the test will fail.
17+
reportResults
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"latest": {
3+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
4+
"remoteUser": "vscode",
5+
"features": {
6+
"install-npm-package-globally": {
7+
"packages": ["ts-node"]
8+
}
9+
}
10+
},
11+
"specific-version": {
12+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
13+
"remoteUser": "vscode",
14+
"features": {
15+
"install-npm-package-globally": {
16+
"packages": ["[email protected]"]
17+
}
18+
}
19+
},
20+
"semver-version": {
21+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
22+
"remoteUser": "vscode",
23+
"features": {
24+
"install-npm-package-globally": {
25+
"packages": ["ts-node@~8.10.0"]
26+
}
27+
}
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
# trunk-ignore-all(shellcheck/SC2312)
3+
4+
# This test file will be executed against one of the scenarios devcontainer.json test that
5+
# includes the 'color' feature with "greeting": "hello" option.
6+
7+
set -e
8+
9+
# Optional: Import test library bundled with the devcontainer CLI
10+
source dev-container-features-test-lib
11+
12+
# Feature-specific tests
13+
# The 'check' command comes from the dev-container-features-test-lib.
14+
15+
check "Checking ts-node avaiable" ts-node --version
16+
check "Checking ts-node version must satisfy semver" ts-node --version | grep -q "v8.10.*"
17+
check "Checking ts-node version must not v8.10.0" ts-node --version | grep -q "v8.10.0" && exit 0 || exit 1
18+
19+
# Report results
20+
# If any of the checks above exited with a non-zero exit code, the test will fail.
21+
reportResults
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# trunk-ignore-all(shellcheck/SC2312)
3+
# This test file will be executed against one of the scenarios devcontainer.json test that
4+
# includes the 'color' feature with "greeting": "hello" option.
5+
6+
set -e
7+
8+
# Optional: Import test library bundled with the devcontainer CLI
9+
source dev-container-features-test-lib
10+
11+
# Feature-specific tests
12+
# The 'check' command comes from the dev-container-features-test-lib.
13+
check "Checking ts-node avaiable" ts-node --version
14+
check "Checking ts-node version must be v9.1.1" ts-node --version | grep -q "v9.1.1"
15+
16+
# Report results
17+
# If any of the checks above exited with a non-zero exit code, the test will fail.
18+
reportResults

0 commit comments

Comments
 (0)