Add `prettier` support for //scouting
This patch doesn't actually enable `prettier`. The next patch will
enable it and reformat the files in //scouting.
We can expand coverage to other directories in future patches.
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: Ib3dae3aa921ab11b3d5deea692e71185d5fa2a23
diff --git a/tools/lint/prettier.sh b/tools/lint/prettier.sh
new file mode 100755
index 0000000..dc29522
--- /dev/null
+++ b/tools/lint/prettier.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+
+# --- begin runfiles.bash initialization v2 ---
+# Copy-pasted from the Bazel Bash runfiles library v2.
+set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
+source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
+ source "$0.runfiles/$f" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+ { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
+# --- end runfiles.bash initialization v2 ---
+
+PRETTIER="$(rlocation npm/prettier/bin/prettier.sh)"
+PRETTIER="$(readlink -f "${PRETTIER}")"
+readonly PRETTIER
+
+# Find the files to format at the root of the repo. For some reason, the
+# prettier binary (probably because of the rules_nodejs wrapper), breaks when
+# invoked from a directory outside of the runfiles tree.
+pushd "${BUILD_WORKSPACE_DIRECTORY}"
+
+# Find web-related files in the repo.
+# TODO(phil): Support more than just //scouting.
+web_files=($(git ls-tree --name-only --full-tree -r @ \
+ | grep '^scouting/' \
+ | (grep \
+ -e '\.ts$' \
+ -e '\.js$' \
+ -e '\.css$' \
+ -e '\.html$' \
+ || :)))
+
+web_files_abs_paths=($(for file in "${web_files[@]}"; do
+ readlink -f "${file}"
+done))
+
+CONFIG_FILE="$(readlink -f .prettierrc.json)"
+readonly CONFIG_FILE
+
+# Go back to the runfiles directory so that prettier doesn't break.
+popd
+
+# If we have any web-related files, format them.
+if ((${#web_files_abs_paths[@]} > 0)); then
+ "${PRETTIER}" \
+ --config "${CONFIG_FILE}" \
+ --write \
+ "${web_files_abs_paths[@]}"
+fi