-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-changed.sh
64 lines (53 loc) · 1.72 KB
/
run-changed.sh
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
set -e # Exit on error
set -u # Treat unset variables as an error
set -o pipefail # Prevents errors in pipelines from being masked
# Check if a command (e.g., "lint" or "test") was passed
if [[ $# -eq 0 ]]; then
echo "Usage: $0 <command>"
exit 1
fi
command_name="$1" # First argument (e.g., "lint", "test", "build")
# Get the list of changed files in the last commit
changed_files=$(git diff --name-only HEAD~1 || echo "")
if [[ -z "$changed_files" ]]; then
echo "No changed files found."
exit 0
fi
changed_packages=()
# Find the unique package directories that contain changed files
while IFS= read -r file; do
package_dir=$(echo "$file" | awk -F'/' '{print $1"/"$2}' | grep '^packages/' || echo "")
if [[ -n "$package_dir" && -d "$package_dir" ]]; then
found="false"
for pkg in "${changed_packages[@]:-}"; do
if [[ "$pkg" == "$package_dir" ]]; then
found="true"
break
fi
done
if [[ "$found" == "false" ]]; then
changed_packages+=("$package_dir")
fi
fi
done <<< "$changed_files"
if [[ ${#changed_packages[@]} -eq 0 ]]; then
echo "No changed packages found."
exit 0
fi
# Run command in each package sequentially
for package in "${changed_packages[@]:-}"; do
package_json="$package/package.json"
if [[ -f "$package_json" ]]; then
if grep -q "\"$command_name\"" "$package_json"; then
echo "Running '$command_name' in $package..."
cd "$package"
npm run "$command_name" || yarn "$command_name" || pnpm "$command_name"
cd - > /dev/null # Return to original directory without printing
else
echo "Skipping $package (no '$command_name' script found)."
fi
else
echo "Skipping $package (package.json not found)."
fi
done