From 812a4f3fe84b1892bc39a0774a2582443a9acd1c Mon Sep 17 00:00:00 2001 From: Christian Nunciato Date: Mon, 20 May 2024 11:49:49 -0700 Subject: [PATCH] Add pre-commit linting, version checking --- .husky/pre-commit | 1 + Makefile | 2 +- package.json | 4 +++- scripts/ensure.sh | 31 +++++++++++++++++++++++-------- scripts/format.sh | 5 +++++ yarn.lock | 5 +++++ 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 .husky/pre-commit create mode 100755 scripts/format.sh diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000000000..9c4aa94166dd2 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1 @@ +make lint diff --git a/Makefile b/Makefile index 70725a72f41fb..30e2d7fc13ec0 100644 --- a/Makefile +++ b/Makefile @@ -142,4 +142,4 @@ lint: .PHONY: format format: - yarn prettier --write . + ./scripts/format.sh diff --git a/package.json b/package.json index ac50576e168b9..8e2f36f418434 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "private": true, "license": "Apache-2.0", "scripts": { - "minify-css": "node scripts/minify-css.js" + "minify-css": "node scripts/minify-css.js", + "prepare": "husky" }, "dependencies": { "@aws-sdk/client-s3": "^3.427.0", @@ -34,6 +35,7 @@ "typescript": "^4.9.5" }, "devDependencies": { + "husky": "^9.0.11", "prettier": "^2.6.2" } } diff --git a/scripts/ensure.sh b/scripts/ensure.sh index 2f7b7cfd21a2f..a3229305101d5 100755 --- a/scripts/ensure.sh +++ b/scripts/ensure.sh @@ -1,14 +1,29 @@ #!/bin/bash -# Make sure the Node version matches what's in .nvmrc (and in our GitHub Actions workflows). -current_version="$(node -v)" -required_version="$(cat .nvmrc)" +set -o errexit -o pipefail -if [ ! $(echo ${current_version} | grep ${required_version}) ]; then - printf "\nIt looks like you're running Node %s, but this project uses Node %s.\n" ${current_version} ${required_version} - printf "If you're using nvm, try running nvm use.\n\n" - exit 1 -fi +check_version() { + tool_name="$1" + executable="$2" + version_string="$(eval $3)" + required_version="$4" + details="See the README at https://github.com/pulumi/docs for a list of required tools and versions." + + if ! command -v "$executable" &> /dev/null; then + echo "This project requires $1, but the '$2' executable doesn't seem to be installed and on your PATH." + echo $details + exit 1 + fi + + if [ ! $(echo ${version_string} | grep ${required_version}) ]; then + printf "It looks like you're running %s %s, but this project uses version %s.\n" ${tool_name} ${version_string} ${required_version} + echo $details + exit 1 + fi +} +check_version "Node.js" "node" "node -v | sed 's/v\([0-9\.]*\).*$/\1/'" "18" +check_version "Hugo" "hugo" "hugo version | sed 's/hugo v\([0-9\.]*\).*$/\1/'" "0.111.0" +check_version "Yarn" "yarn" "yarn -v | sed 's/v\([0-9\.]*\).*$/\1/'" "1.22" # Install the Node dependencies for the website and the infrastructure. yarn install diff --git a/scripts/format.sh b/scripts/format.sh new file mode 100755 index 0000000000000..db8eabdb7570d --- /dev/null +++ b/scripts/format.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -o errexit -o pipefail + +yarn prettier --write . diff --git a/yarn.lock b/yarn.lock index 934c9d39c427f..6d789733c2355 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2894,6 +2894,11 @@ humanize-duration@^3.9.1: resolved "https://registry.yarnpkg.com/humanize-duration/-/humanize-duration-3.27.3.tgz#db654e72ebf5ccfe232c7f56bc58aa3a6fe4df88" integrity sha512-iimHkHPfIAQ8zCDQLgn08pRqSVioyWvnGfaQ8gond2wf7Jq2jJ+24ykmnRyiz3fIldcn4oUuQXpjqKLhSVR7lw== +husky@^9.0.11: + version "9.0.11" + resolved "https://registry.yarnpkg.com/husky/-/husky-9.0.11.tgz#fc91df4c756050de41b3e478b2158b87c1e79af9" + integrity sha512-AB6lFlbwwyIqMdHYhwPe+kjOC3Oc5P3nThEoW/AaO2BX3vJDjWPFxYLxokUZOo6RNX20He3AaT8sESs9NJcmEw== + iconv-lite@0.6.3: version "0.6.3" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.6.3.tgz#a52f80bf38da1952eb5c681790719871a1a72501"