#!/bin/sh
# script/fmt: format all files (writes): gofmt for Go, prettier for
# markdown.
set -eu

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

GO_DIRS="cmd internal pkg"

# Debian and Ubuntu name the yarn command yarnpkg.
run_yarn() {
    if command -v yarn >/dev/null 2>&1; then
        yarn "$@"
    elif command -v yarnpkg >/dev/null 2>&1; then
        yarnpkg "$@"
    else
        echo "no yarn; run script/bootstrap first" >&2
        exit 1
    fi
}

main() {
    cd "$ROOT"
    # shellcheck disable=SC2086 # GO_DIRS is a word list on purpose
    gofmt -w $GO_DIRS
    run_yarn run prettier --write '**/*.md' --tab-width 4 --prose-wrap always
}

main "$@"
