Skip to content

Instantly share code, notes, and snippets.

@manuhabitela
Created May 1, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuhabitela/cbb501c394fa719a7ca67c06c72e2b00 to your computer and use it in GitHub Desktop.
Save manuhabitela/cbb501c394fa719a7ca67c06c72e2b00 to your computer and use it in GitHub Desktop.
Start prettier on each file change via watchexec
#!/bin/bash
# Automatically pass prettier when a file changes thanks to watchexec.
#
# start this at the root of a js directory you want to pass prettier on file save
#
# recommended watchexec setup: `watchexec --postpone --exts js`
#
# ```
# cd ~/myproject
# watchexec -p -e js watchexec-prettier [PRETTIER_OPTIONS]
# ```
#
# default prettier options are "--write --use-tabs --no-bracket-spacing"
# to use your own options, pass them to the script.
# A "--" is required for watchexec to work correctly in this case:
#
# `watchexec -p -e js -- watchexec-prettier --write`
#
started_from=$(pwd)
prettier_path="prettier"
prettier_default_config="--write --use-tabs --no-bracket-spacing"
prettier_config="$*"
# use a locally installed prettier if any
if [ -e "$started_from/node_modules/.bin/prettier" ]
then
prettier_path="$started_from/node_modules/.bin/prettier"
fi
# @see http://stackoverflow.com/a/26759734
if [ ! -x "$(command -v $prettier_path)" ]
then
echo -e "No prettier executable found, exiting.\n"
exit 1
fi
if [ -z "$prettier_config" ]
then
prettier_config=$prettier_default_config
fi
relative_path=".${WATCHEXEC_UPDATED_PATH/"$started_from"/""}"
echo $(basename $prettier_path) $prettier_config $relative_path
$prettier_path $prettier_config $WATCHEXEC_UPDATED_PATH
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment