Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created January 14, 2022 08:59
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 OliverJAsh/18aa530b1606346d00ac89b72d38cdc1 to your computer and use it in GitHub Desktop.
Save OliverJAsh/18aa530b1606346d00ac89b72d38cdc1 to your computer and use it in GitHub Desktop.
prettier-diff
#!/bin/bash
# Exit immediately if a command exits with a non-zero status, amongst other improvements
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -eux
#
# Example usage:
# ./prettier-diff.sh --no-index my-minified-file-before.js my-minified-file-after.js
#
# Inspired by https://github.com/josephfrazier/prettier-diff but this script is a lot simpler
# because it doesn't need to (dangerously) modify git files.
#
cat $2 | prettier --parser babel > a.js
cat $3 | prettier --parser babel > b.js
set +e
git diff $1 a.js b.js
set -e
rm -rf a.js b.js
@paularmstrong
Copy link

instead of writing to a.js and b.js, you might consider using mktemp to create temp files. That way they aren't local and you don't need to worry about cleaning them up.

A=$(mktemp)
B=$(mktemp)

cat $2 | prettier --parser babel > $A
cat $3 | prettier --parser babel > $B

set +e
git diff $1 $A $B
set -e

@OliverJAsh
Copy link
Author

Amazing, thanks Paul!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment