#! /bin/bash

# PURPOSE
# =======
#
# This script is to be run when editor finishes editing a file.
#
# INSTALLATION
# ============
#
# Visual Studio Code
# ------------------
#
# 1. Install "Run On Save" extension
#   - Marketplace ID: pucelle.run-on-save
#   - GitHub Repo: https://github.com/pucelle/vscode-run-on-save
#
# 2. Add the following section to your `settings.json`:
#    "runOnSave.commands": [
#      {
#        "match": ".*",
#        "command": "sh -c 'if [ -x ./run-on-save ]; then FILE=${file} ./run-on-save; fi'",
#        "runIn": "backend",
#      }
#    ]

set -o errexit -o pipefail -o nounset

# TASK: Use sane-fmt to format TypeScript/JavaScript file
# <https://github.com/sane-fmt/sane-fmt>
for ext in js ts jsx tsx; do
  if [[ "$FILE" == *.$ext && "$FILE" != *node_modules* ]]; then
    if which sane-fmt &>/dev/null; then
      echo "Run sane-fmt on $FILE"
      sane-fmt --color=never --write "$FILE"
    else
      echo 'Fail to detect sane-fmt in PATH'
      echo 'See <https://github.com/sane-fmt/sane-fmt> for installation instruction'
      exit 1
    fi
    break
  fi
done
