This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
S55_playground
75 lines (69 loc) · 1.68 KB
/
S55_playground
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
mknginxconf() {
tempdir="$1"
port="$2"
cat << EOF > "$tempdir/.nginx.conf"
pid "$tempdir/.nginx.pid";
events { worker_connections 1024; }
http {
default_type application/octet-stream;
server {
access_log "$tempdir/nginx.log";
listen "$port" default;
location / {
root "$tempdir";
}
}
types { text/html html htm shtml; text/css css; text/xml xml; application/x-javascript js; image/jpeg jpeg jpg;
image/png png; application/x-shockwave-flash swf; video/mp4 mp4; }
}
EOF
}
entertempdir() {
tempdir=$(mktemp -d /tmp/playground.XXXXXX)
if [ $? -ne 0 ]; then echo "$0: can't create temporary directory"; return 1; fi
cd "$tempdir"
}
leavetempdir() {
cd -
echo "Hit [enter] to destroy your playground ($tempdir)"
read
rm -fr "$tempdir"
}
safeopen() {
if ! isexe open; then
echo "please open $1 and hit [enter]"
read
else
if [ -n "$2" ]; then
open -g "$1" -a "$2"
else
open -g "$1"
fi
fi
}
freeport() {
while true; do
port=$(($RANDOM+1024))
if nc -z localhost $port > /dev/null; then continue; fi
echo $port
break
done
}
_playground() {
entertempdir || return 1
cp ~/.zsh.d/static/"$1"/* .
port="$(freeport)"
mknginxconf "$tempdir" "$port"
alias nginx='nginx -c "$tempdir"/.nginx.conf'
nginx -c "$tempdir"/.nginx.conf
safeopen http://localhost:$port
vim "$tempdir"/$2
nginx -c "$tempdir"/.nginx.conf -s stop
leavetempdir
}
playground() {
_playground playground index.html
}
mdplayground() {
_playground mdplayground playground.markdown
}