1#!/bin/bash
2set -euo pipefail
3IFS=$'\n\t'
4
5# The yarn run command we'd like to run
6command="$1"
7# The file types we'd like to target, use something like '(js|vue)'
8file_types="$2"
9
10# Removing first two arguments
11shift
12shift
13
14# Read all staged non-deleted files into an array
15staged_files=()
16while IFS= read -r line; do
17    staged_files+=( "$line" )
18done < <( git diff --diff-filter=d --cached --name-only | { grep -E ".$file_types$" || true; })
19
20if [ "${#staged_files[@]}" == "0" ]; then
21    echo "No staged '$file_types' files"
22else
23    echo "Running $command on ${#staged_files[@]} staged '$file_types' files"
24    yarn run "$command" "$@" "${staged_files[@]}"
25fi
26