From 488d84fdd6d7b7dab2c62f5b6579c07eba818938 Mon Sep 17 00:00:00 2001 From: Andres Rojas Date: Tue, 18 Jul 2023 00:50:25 -0700 Subject: [PATCH 1/2] Add plugin support for Prettier --- src/prettier/README.md | 1 + src/prettier/devcontainer-feature.json | 7 +++++- src/prettier/install.sh | 30 ++++++++++++++++++++++++++ test/prettier/plugin.sh | 9 ++++++++ test/prettier/scenarios.json | 9 ++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100755 test/prettier/plugin.sh 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/plugin.sh b/test/prettier/plugin.sh new file mode 100755 index 000000000..c274d91d4 --- /dev/null +++ b/test/prettier/plugin.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -e + +source dev-container-features-test-lib + +check "npm list --parseable --depth 0 | grep prettier-plugin-ini" npm list --parseable --depth 0 | grep "prettier-plugin-ini" + +reportResults diff --git a/test/prettier/scenarios.json b/test/prettier/scenarios.json index 3337bab98..29fec4cc6 100644 --- a/test/prettier/scenarios.json +++ b/test/prettier/scenarios.json @@ -4,5 +4,14 @@ "features": { "prettier": {} } + }, + "plugin": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "prettier": { + "version": "latest", + "plugins": "prettier-plugin-ini" + } + } } } \ No newline at end of file From 8e6952aaf3d99c5465ce7b4cf3451b9347ee3746 Mon Sep 17 00:00:00 2001 From: Andres Rojas Date: Tue, 18 Jul 2023 14:40:37 -0700 Subject: [PATCH 2/2] Extend prettier tests --- test/prettier/{test.sh => latest.sh} | 0 test/prettier/scenarios.json | 15 ++++++++++++--- test/prettier/v2-plugins.sh | 14 ++++++++++++++ test/prettier/{plugin.sh => v3-plugin.sh} | 2 ++ 4 files changed, 28 insertions(+), 3 deletions(-) rename test/prettier/{test.sh => latest.sh} (100%) create mode 100755 test/prettier/v2-plugins.sh rename test/prettier/{plugin.sh => v3-plugin.sh} (73%) 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 29fec4cc6..b8cab6a32 100644 --- a/test/prettier/scenarios.json +++ b/test/prettier/scenarios.json @@ -1,17 +1,26 @@ { - "test": { + "latest": { "image": "mcr.microsoft.com/devcontainers/base:debian", "features": { "prettier": {} } }, - "plugin": { + "v3-plugin": { "image": "mcr.microsoft.com/devcontainers/base:debian", "features": { "prettier": { - "version": "latest", + "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/plugin.sh b/test/prettier/v3-plugin.sh similarity index 73% rename from test/prettier/plugin.sh rename to test/prettier/v3-plugin.sh index c274d91d4..c47fa936e 100755 --- a/test/prettier/plugin.sh +++ b/test/prettier/v3-plugin.sh @@ -4,6 +4,8 @@ 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