diff --git a/src/prettier/README.md b/src/prettier/README.md index ef31093f9..c9a2183cf 100644 --- a/src/prettier/README.md +++ b/src/prettier/README.md @@ -16,5 +16,6 @@ Prettier is an opinionated code formatter. | Options Id | Description | Type | Default Value | |-----|-----|-----|-----| | version | Select the version to install. | string | latest | +| plugins | Comma-separated list of prettier plugins to install. | string | | diff --git a/src/prettier/devcontainer-feature.json b/src/prettier/devcontainer-feature.json index 321bb266b..24a8fac29 100644 --- a/src/prettier/devcontainer-feature.json +++ b/src/prettier/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "prettier", - "version": "1.0.0", + "version": "1.1.0", "name": "Prettier (via npm)", "documentationURL": "http://github.com/devcontainers-contrib/features/tree/main/src/prettier", "description": "Prettier is an opinionated code formatter.", @@ -12,6 +12,11 @@ "latest" ], "type": "string" + }, + "plugins": { + "default": "", + "description": "Comma-separated list of prettier plugins to install.", + "type": "string" } }, "installsAfter": [ diff --git a/src/prettier/install.sh b/src/prettier/install.sh index 1b3b0a434..134c03401 100755 --- a/src/prettier/install.sh +++ b/src/prettier/install.sh @@ -19,6 +19,36 @@ $nanolayer_location \ --option package='prettier' --option version="$VERSION" +PRETTIER_PLUGINS=${PLUGINS:-""} + +setup_npm() { + export NVM_DIR=/usr/local/share/nvm + [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" +} + +install_prettier_plugin() { + echo "Installing prettier plugin - $1" + if ! npm list $1 >/dev/null; then + npm install --save-dev prettier $1 + fi +} + +# Prettier plugins are expected to be installed locally, not globally +# In particular, VSCode + extensions tend to have issues with this +if [ -n "${PRETTIER_PLUGINS}" ]; then + if ! type npm >/dev/null 2>&1; then + setup_npm + fi + + OIFS=$IFS + IFS=',' + for plugin in $PRETTIER_PLUGINS; do + install_prettier_plugin $plugin + done + IFS=$OIFS +fi + + echo 'Done!' diff --git a/test/prettier/test.sh b/test/prettier/latest.sh similarity index 100% rename from test/prettier/test.sh rename to test/prettier/latest.sh diff --git a/test/prettier/scenarios.json b/test/prettier/scenarios.json index 3337bab98..b8cab6a32 100644 --- a/test/prettier/scenarios.json +++ b/test/prettier/scenarios.json @@ -1,8 +1,26 @@ { - "test": { + "latest": { "image": "mcr.microsoft.com/devcontainers/base:debian", "features": { "prettier": {} } + }, + "v3-plugin": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "prettier": { + "version": "3.0.0", + "plugins": "prettier-plugin-ini" + } + } + }, + "v2-plugins": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "prettier": { + "version": "2.8.8", + "plugins": "@prettier/plugin-php@0.19.6,@prettier/plugin-ruby@4.0.2,@prettier/plugin-xml@3.1.0" + } + } } } \ No newline at end of file diff --git a/test/prettier/v2-plugins.sh b/test/prettier/v2-plugins.sh new file mode 100755 index 000000000..fec37633c --- /dev/null +++ b/test/prettier/v2-plugins.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + +source dev-container-features-test-lib + +check "prettier --version = 2.8.8" [ "$(prettier --version)" = "2.8.8" ] + +for p in php ruby xml; do + plugin="@prettier/plugin-${p}" + check "npm list --parseable --depth 0 | grep ${plugin}" npm list --parseable --depth 0 | grep "${plugin}" +done + +reportResults diff --git a/test/prettier/v3-plugin.sh b/test/prettier/v3-plugin.sh new file mode 100755 index 000000000..c47fa936e --- /dev/null +++ b/test/prettier/v3-plugin.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +source dev-container-features-test-lib + +check "prettier --version = 3.0.0" [ "$(prettier --version)" = "3.0.0" ] + +check "npm list --parseable --depth 0 | grep prettier-plugin-ini" npm list --parseable --depth 0 | grep "prettier-plugin-ini" + +reportResults