#!/usr/bin/env bash # # Pre-commit hook to run lightweight checks and auto-format the code. It's designed # to be blazingly fast, so it checks only changed files. # # You can install this hook and some of its dependencies by running `scripts/init.sh`. set -euo pipefail # Using `readlink` because the pre-commit hook is installed via a symlink, so # we need to resolve it before we can make path relative to this script's file. . "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../scripts/lib.sh" function command_exists() { bin_name=$(basename "$1") if command -v "$1" &> /dev/null; then return 0 fi warn "$bin_name CLI was not found. Ignoring it..." return 1 } files=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g') if [[ -z "$files" ]]; then info "No files changed. Exiting the pre-commit hook..." exit 0 fi if command_exists typos; then echo "$files" | step xargs typos fi if command_exists npx; then echo "$files" | step xargs npx prettier --ignore-unknown --write fi if command_exists cargo; then # `rustfmt` doesn't ignore non-rust files automatically rust_files=$(echo "$files" | { grep -E '\.rs$' || true; }) if [[ -n "$rust_files" ]]; then echo "$rust_files" | step xargs cargo fmt --manifest-path native/Cargo.toml -- fi fi if command_exists mix; then echo "$files" | step xargs mix format fi # Add the modified/prettified files to staging echo "$files" | step xargs git add