-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpublisher
115 lines (103 loc) · 2.13 KB
/
publisher
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
WAIT=2
function scriptUsage() {
cat << EOF
Publish from remote storage and observe file changes
Usage: ./publisher [-h|-n|-s DIR]
Options:
-h|--help Displays this help
-n|--no-npm-install Publish without npm install
-s|--source-dir [DIR] Source directory name
-w|--wait [NUMBER] Waiting time in seconds (default: 2 seconds)
EOF
}
startTimer() {
start=$(date +%s.%N)
}
endTimer() {
duration=$( echo "$(date +%s.%N)-$start" | bc | cut -b1-5 )
}
fetchFiles() {
startTimer
files=$(find . -type f \( -name "*.*" ! -path "\./\.*" ! -path "\./publisher" \) \
| git check-ignore --verbose --non-matching --stdin \
| sed -n -e 's,\t./,\t,' -e 's,^::\t*,,p')
if [ -z "$files" ]; then
echo "No files found in the given directory"
exit 1
fi
endTimer
echo "Directory scanned in $duration seconds"
}
function parseParams() {
local param
while [[ $# -gt 0 ]]; do
param="$1"
shift
case $param in
-h | --help)
scriptUsage
exit 0
;;
-n | --no-npm-install)
noNpmInstall=true
;;
-s | --source-dir)
srcDir=$1
if [ -z "$srcDir" ]; then
echo "Expecting a directory name after -s option."
exit 1
else
if [ -d "$srcDir" ]; then
cd $srcDir
else
echo "The given directory does not exists."
exit 1
fi
fi
shift
;;
-w | --wait)
WAIT=$1
if ! [[ $WAIT =~ '^[0-9]+$' ]]; then
echo "Error: Wait given is not a number" >&2; exit 1
fi
;;
*)
echo "Invalid parameter was provided: $param."
exit 1
;;
esac
done
}
function publish() {
startTimer
echo
echo "[$(date)]: Publishing worker"
wrangler publish
currentHash=$newHash
endTimer
}
function checkHash() {
newHash=$(sha1sum $files | sha1sum | awk '{print $1}')
if [[ "$newHash" != "$currentHash" ]]; then
publish
echo "Published worker in $duration seconds."
printf "Scanning for file change..."
fi
}
function initScript() {
if [[ -z ${noNpmInstall-} ]]; then
npm install --no-bin-links
fi
while true; do
checkHash
sleep $WAIT
done
}
function main() {
fetchFiles
parseParams "$@"
initScript
}
main "$@"