From f5ede513eb458ff8fe43bf08dd0a54f3d3f036f9 Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Tue, 7 Mar 2017 22:55:43 -0500 Subject: [PATCH] update the README to add a pre-commit hook (#944) * update the README to add a pre-commit hook * remove comments * better wording * better quoting * consistent prettier binary * move the tr and fix the quoting * fix quotes one last time --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 58cf66076..c50d20321 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,23 @@ and add this config to your `package.json`: See https://github.com/okonet/lint-staged#configuration for more details about how you can configure 🚫💩 lint-staged. +Alternately you can just save this script as `.git/hooks/pre-commit` and give it execute permission: + +```bash +#!/bin/sh +jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.js$' | tr '\n' ' ') +[ -z "$jsfiles" ] && exit 0 + +diffs=$(node_modules/.bin/prettier -l $jsfiles) +[ -z "$diffs" ] && exit 0 + +echo "here" +echo >&2 "Javascript files must be formatted with prettier. Please run:" +echo >&2 "node_modules/.bin/prettier --write "$diffs"" + +exit 1 +``` + ### API The API is a single function exported as `format`. The options