-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchChanges.sh
executable file
·70 lines (54 loc) · 1.9 KB
/
watchChanges.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
65
66
67
68
69
70
#!/bin/bash
# Define the command to run
COMMAND="npm run host"
# Function to run the command in the background
run_command() {
$COMMAND &
COMMAND_PID=$!
}
# Function to check for local changes and push if no changes for over 5 minutes
check_and_push_local_changes() {
if [[ -n $(git status --porcelain) ]]; then
echo "Local changes detected. Waiting for 5 minutes of inactivity before pushing..."
last_change_time=$(date +%s)
while [[ -n $(git status --porcelain) ]]; do
current_time=$(date +%s)
elapsed_time=$((current_time - last_change_time))
echo "${elapsed_time}"
if [[ $elapsed_time -ge 300 ]]; then
echo "No local changes for 5 minutes. Pushing changes..."
git add -A
git commit -m "Auto-commit"
git push
break
fi
# Wait before checking again
sleep 10
done
fi
}
# Function to watch for git changes and restart the command if changes are pulled
watch_directory() {
while true; do
# First, check and push local changes if any
check_and_push_local_changes
# Then, pull the latest changes
git_output=$(git pull)
# Check if there were any changes pulled
if [[ $git_output != *"Already up to date."* ]]; then
echo "Changes detected from remote. Restarting command..."
# Kill the previous command
kill -9 $COMMAND_PID
tail --pid=$COMMAND_PID -f /dev/null
npm install
# Run the command again
run_command
fi
# Wait for a while before checking again
sleep 30
done
}
# Run the initial command
run_command
# Start watching the directory for changes
watch_directory