summaryrefslogtreecommitdiff
path: root/scripts/frontend/execute-on-staged-files.sh
blob: f218926f098f18e636a8c215665589dcc67c04c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# The yarn run command we'd like to run
command="$1"
# The file types we'd like to target, use something like '(js|vue)'
file_types="$2"

# Removing first two arguments
shift
shift

# Read all staged non-deleted files into an array
staged_files=()
while IFS= read -r line; do
    staged_files+=( "$line" )
done < <( git diff --diff-filter=d --cached --name-only | { grep -E ".$file_types$" || true; })

if [ "${#staged_files[@]}" == "0" ]; then
    echo "No staged '$file_types' files"
else
    echo "Running $command on ${#staged_files[@]} staged '$file_types' files"
    yarn run "$command" "$@" "${staged_files[@]}"
fi