diff --git a/.husky/pre-commit b/.husky/pre-commit index 45f8b30339..08803a44e9 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,5 +1,43 @@ -G='\033[0;32m' -P='\033[0;35m' -CLEAN='\033[0;0m' +#!/usr/bin/env bash -yarn run check:mdx || (echo -e "${G}Hint:${CLEAN} execute ${P}yarn run format:mdx${CLEAN} to format files" && exit 1) +REQ_NODE_MAJOR=$(sed -E 's/^v?([0-9]+).*/\1/' .nvmrc) + +if ! command -v node >/dev/null 2>&1 || [ "$(node -p 'process.versions.node.split(".")[0]')" -lt "$REQ_NODE_MAJOR" ]; then + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + + if command -v nvm >/dev/null 2>&1; then + if ! nvm use "$REQ_NODE_MAJOR" >/dev/null 2>&1; then + echo "❔ Node v$REQ_NODE_MAJOR is not installed in nvm. Skipping formatting check." + exit 0 + fi + elif command -v node >/dev/null 2>&1; then + echo "⚠ Node v$REQ_NODE_MAJOR or higher is required, but $(node -v) is on PATH and nvm is not available. Skipping formatting check." + exit 0 + else + echo "✖ Node is not installed and nvm is not available. Skipping formatting check." + exit 0 + fi +fi + +if ! command -v yarn >/dev/null 2>&1; then + if command -v corepack >/dev/null 2>&1; then + if corepack enable >/dev/null 2>&1 && corepack prepare yarn@stable --activate >/dev/null 2>&1; then + echo "✓ Yarn is available through Corepack." + fi + fi + if ! command -v yarn >/dev/null 2>&1 && command -v npm >/dev/null 2>&1; then + if npm install -g yarn >/dev/null 2>&1; then + echo "✓ Yarn installed with npm." + fi + fi + if ! command -v yarn >/dev/null 2>&1; then + echo "❕ Yarn not found. Skipping formatting check." + exit 0 + fi +fi + +if ! yarn run check:mdx; then + echo -e "ℹ Run \`yarn run format:mdx\` to fix formatting." + exit 1 +fi