Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"shellcheck": "./scripts/shellCheck.sh",
"spell": "cspell --color **/*",
"spell-changed": "cspell --color --no-must-find-files",
"prettier": "prettier --experimental-cli --write .",
"prettier": "./scripts/runPrettier.sh",
"prettier-changed": "prettier --experimental-cli --write --ignore-unknown $(git diff --diff-filter=AMR --name-only origin/main HEAD)",
"prettier-watch": "onchange \"**/*.{js,mjs,ts,tsx}\" -- prettier --experimental-cli --write --ignore-unknown {{changed}}",
"print-version": "echo $npm_package_version",
Expand Down
42 changes: 42 additions & 0 deletions scripts/runPrettier.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
#
# Prettier can intermittently fail due to a stale cache. This wrapper runs
# prettier once, and if it fails, clears the cache and retries.

set -eu

TOP="$(realpath "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/..")"
readonly TOP
source "${TOP}/scripts/shellUtils.sh"

declare -r PRETTIER_CACHE_DIR="${TOP}/node_modules/.cache/prettier"

function run_prettier() {
npx prettier --experimental-cli --write . "$@"
}

function clear_cache() {
if [[ -d "${PRETTIER_CACHE_DIR}" ]]; then
info "Clearing prettier cache at ${PRETTIER_CACHE_DIR}"
rm -rf "${PRETTIER_CACHE_DIR}"
fi
}

function main() {
if run_prettier "$@"; then
success "Prettier finished successfully"
return
fi

error "Prettier failed — clearing cache and retrying"
clear_cache

if run_prettier "$@"; then
success "Prettier finished successfully after clearing cache"
else
error "Prettier failed again after clearing cache"
exit 1
fi
}

main "$@"
Loading