Skip to content

Instantly share code, notes, and snippets.

@chetstone
Last active July 1, 2017 19:37
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 chetstone/18f9022951c614e0f53887624d65acfe to your computer and use it in GitHub Desktop.
Save chetstone/18f9022951c614e0f53887624d65acfe to your computer and use it in GitHub Desktop.
Configurable per-project options for [prettier-emacs](https://github.com/prettier/prettier-emacs)

Given an invocation of prettier in package.json like this:

  "scripts": {
    "prettier": "prettier --write --single-quote --no-semi --trailing-comma es5 --print-width 80 \"*.js\""
  },

Move the options to a config variable like this:

  "scripts": {                  
    "prettier": "prettier --write $npm_package_config_prettier \"*.js\""
  },
  "config": {
    "prettier": "--single-quote --no-semi --trailing-comma es5 --print-width 80"
  },

Then create a shell script called prettier-emacs that pulls the config variable and invokes prettier, and customize prettier-js-command in emacs to invoke the new script.

#!/bin/sh
# Search for a package.json that has the config variable in this and parent directories
path=`pwd`
while [[ $path != "" &&
( ! -e "$path/package.json" ||
! `node -p "require('$path/package.json').config.prettier" 2>/dev/null` ) ]];
do
path=${path%/*}
#echo $path
done
pkg=$path/package.json
args=""
if [[ ! -e $pkg ]]
then
echo "WARNING: No package.json found with config.prettier"
else
args=`node -p "require('$pkg').config.prettier"`
#echo $args
prettier $args $*
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment