forked from fstrube/closure-auto-compiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
js-auto-compiler
executable file
·97 lines (84 loc) · 2.32 KB
/
js-auto-compiler
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
#!/bin/bash
# Global defaults
CLOSURE_COMPILER="/opt/local/lib/closure/compiler.jar"
FREQUENCY=5
# System override the defaults
if [ -f /etc/default/closure ]; then
. /etc/default/closure
fi
if ! [ -r "$CLOSURE_COMPILER" ]; then
echo "Error: cannot find the closer compiler $CLOSURE_COMPILER" >&2
exit 1
fi
COMPILE="java -jar $CLOSURE_COMPILER"
# First argument can be the directory
if [ -n "$1" ]; then
cd "$1" || exit 1
fi
### THE MEAT ###
################
# Cross-platform growl notification #
notify() {
TEXT="$@"
if which growlnotify >/dev/null 2>&1; then
growlnotify -n "Closure Compiler" -w << _EOM_
$TEXT
_EOM_
elif which notify-send >/dev/null 2>&1; then
notify-send -t 3000 "Closure Compiler" "$TEXT" 2>/dev/null || echo "$TEXT"
else
echo " $TEXT" | sed 's/\n/\n /'
fi
}
# Clear a line #
clearline() {
echo -e -n "\r \r" > /dev/stdout
}
# Cross-platform stat command #
if stat -c %Y . >/dev/null 2>&1 ; then
STAT="stat -c %Y"
elif stat -f %c . >/dev/null 2>&1 ; then
STAT="stat -f %c"
else
echo "Error: Invalid stat version: " >&2
stat --version >&2
exit 1
fi
DIRECTORY=$(pwd)
echo "Scanning directory: $DIRECTORY ..."
COUNT=0
SPINNER="|/-\\"
while true; do
clearline
echo -n " "${SPINNER:$COUNT:1} > /dev/stdout
find . -maxdepth 1 -name '*.js' | while read JSFILE; do
# IGNORE already minified files
[ $(basename $JSFILE .min.js) != $(basename $JSFILE) ] && continue
JSMINFILE="$(basename $JSFILE .js).min.js"
if [ -f $JSMINFILE ]; then
# checking if min file is older
if [ $($STAT $JSMINFILE) -lt $($STAT $JSFILE) ]; then
clearline
echo " minifying: $JSFILE => $JSMINFILE"
MESSAGE=$($COMPILE --js $JSFILE --js_output_file $JSMINFILE 2>&1)
if [ $? -eq 0 ]; then
notify ${MESSAGE:-$DIRECTORY/$(basename $JSFILE) => $JSMINFILE}
else
notify "Error minifying $JSFILE, see the log for more details"
echo "$MESSAGE" >&2
fi
fi
else
echo " minifying: $JSFILE => $JSMINFILE"
MESSAGE=$($COMPILE --js $JSFILE --js_output_file $JSMINFILE 2>&1)
if [ $? -eq 0 ]; then
notify ${MESSAGE:-$DIRECTORY/$(basename $JSFILE) => $JSMINFILE}
else
notify "Error minifying $JSFILE, see the log for more details"
echo "$MESSAGE" >&2
fi
fi
done
sleep $FREQUENCY
COUNT=$(expr $(expr $COUNT + 1) % 4)
done