all.bash: update prettier script to use dockerized prettier install

Updates all.bash to use a dockerized prettier install as a fallback
when prettier is not installed globally. For now the script will
only use the container if it has already been built. We could change
this in the future to automatically build the image but this will give
everyone a chance to test it out first. To build the image run
'docker-compose -f devtools/config/docker-compose.yaml build' from the
repo root.

Change-Id: I56ad2cd2da2fe0dbbf46636024f44cc614adb168
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/275532
Trust: Jamal Carvalho <jamal@golang.org>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/all.bash b/all.bash
index e3fa642..75c2343 100755
--- a/all.bash
+++ b/all.bash
@@ -127,13 +127,19 @@
   runcmd go run ./devtools/cmd/csphash content/static/html/**/*.tmpl
 }
 
+# run_prettier runs prettier on CSS, JS, and MD files. Uses globally
+# installed prettier if available or a dockerized installation as a
+# fallback.
 run_prettier() {
-  if ! [ -x "$(command -v prettier)" ]; then
+  FILES='content/static/**/*.{js,css} **/*.md'
+  if [[ -x "$(command -v prettier)" ]]; then
+    runcmd prettier --write $FILES
+  elif [[ -x "$(command -v docker-compose)" && "$(docker images -q pkgsite_npm)" ]]; then
+    runcmd docker-compose -f devtools/config/docker-compose.yaml run --entrypoint=npx \
+    npm prettier --write $FILES
+  else
     err "prettier must be installed: see https://prettier.io/docs/en/install.html"
   fi
-  runcmd prettier --write content/static/css/*.css
-  runcmd prettier --write content/static/js/*.js
-  runcmd prettier --write **/*.md
 }
 
 standard_linters() {
diff --git a/doc/precommit.md b/doc/precommit.md
index c1ffd02..526d2ca 100644
--- a/doc/precommit.md
+++ b/doc/precommit.md
@@ -41,6 +41,6 @@
 because it has a dependency on nodejs, which is otherwise not needed and which
 not all developers have installed on their system.
 
-If you are modifying CSS or Javascript, install prettier as described at
-https://prettier.io/docs/en/install.html, and run `./all.bash prettier` to
-format your changes before mailing your CL.
+If you are modifying CSS or Javascript and you do not have docker installed,
+install prettier as described at https://prettier.io/docs/en/install.html,
+and run `./all.bash prettier` to format your changes before mailing your CL.