#!/bin/sh
# script/fmt: format all files (writes). Go via gofmt, everything else
# (markdown, CSS, JS, YAML, JSON) via prettier with the repo's
# .prettierrc. Never hand-rolled substitutions: this script is the only
# sanctioned way to reformat the tree.
set -eu

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"

main() {
    cd "$ROOT"

    gofmt -s -w .

    if command -v goimports >/dev/null 2>&1; then
        goimports -w .
    fi

    # A missing runner (exit 2) is a warning here: script/prettier has
    # already said so on stderr, and failing `make fmt` over it would
    # block work that has nothing to do with markdown.
    "$SCRIPT_DIR/prettier" --write . || [ $? -eq 2 ]
}

main "$@"
