diff --git a/.husky/pre-commit b/.husky/pre-commit index 4fa0541919..0d418d1aac 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,14 +1,71 @@ -G='\033[0;32m' -P='\033[0;35m' -R='\033[0;31m' -CLEAN='\033[0;0m' +#!/usr/bin/env sh -pnpm check:mdx || (echo -e "${G}Hint:${CLEAN} execute ${P}pnpm format:mdx${CLEAN} to format files" && exit 1) +fail() { + printf '%s\n' "$1" + exit 1 +} + +REPO_ROOT=$(git rev-parse --show-toplevel) || + fail "✖ Could not determine the repository root." + +cd "$REPO_ROOT" || + fail "✖ Could not enter the repository root." + +REQUIRED_NODE_MAJOR=$(sed 's/^v//; s/\..*$//' .nvmrc) || + fail "✖ Could not determine the required Node version from .nvmrc." + +SETUP_URL="https://github.com/stellar/stellar-docs#prerequisites" + +if ! command -v node >/dev/null 2>&1; then + fail "✖ Node v${REQUIRED_NODE_MAJOR}+ was not found. +Install the required Node version, then run: + corepack pnpm install +Setup instructions: ${SETUP_URL}" +fi + +NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null) || + fail "✖ Node is present but could not be executed." + +if [ "$NODE_MAJOR" -lt "$REQUIRED_NODE_MAJOR" ]; then + fail "✖ Node v${REQUIRED_NODE_MAJOR}+ is required, but $(node -v) is active. +Activate the Node version listed in .nvmrc and try again. +Setup instructions: ${SETUP_URL}" +fi + +PRETTIER="node_modules/prettier/bin/prettier.cjs" + +if [ ! -f "$PRETTIER" ]; then + fail "✖ Project dependencies are not installed. +Run: + corepack pnpm install +Setup instructions: ${SETUP_URL}" +fi + +if ! node "$PRETTIER" --version >/dev/null; then + fail "✖ Prettier could not run successfully. +The project dependencies may be incomplete or corrupted. Run: + corepack pnpm install +Setup instructions: ${SETUP_URL}" +fi + +corepack pnpm check:mdx +CHECK_STATUS=$? + +if [ "$CHECK_STATUS" -eq 1 ]; then + printf '%s\n' 'ℹ Run `pnpm format:mdx` to fix the formatting.' + printf '%s\n' ' If that also fails, your dependencies may be out of sync: `corepack pnpm install`' + exit 1 +fi + +if [ "$CHECK_STATUS" -ne 0 ]; then + fail "✖ The formatting check could not run successfully. +The project dependencies may be incomplete or corrupted. Run: + corepack pnpm install +Setup instructions: ${SETUP_URL}" +fi # No new absolute internal /docs links in staged docs. Canonical rule lives in # scripts/check-relative-links.sh (shared with CI) — only flags links introduced # in staged changes, never pre-existing content in untouched lines. -scripts/check-relative-links.sh --staged || { - echo -e "${R}Error:${CLEAN} absolute /docs link(s) found above. Use a relative ${P}../path/file.mdx${CLEAN} link with the .mdx extension instead." - exit 1 -} +scripts/check-relative-links.sh --staged || + fail "✖ Absolute /docs link check failed. Use a relative ../path/file.mdx link with the .mdx extension instead."