aboutsummaryrefslogtreecommitdiffstats
path: root/.config/init/funcs/cofe
blob: 509e24b114dbd52323eab694ad30061272d94795 (plain)
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
#!/usr/bin/env zsh
# compile coffeescript. sorry about the mess x-x

local strval
local rval
rval=0

[[ $#@ -lt 1 ]] \
	&& echo "\e[1;31merr:\e[0m please specify one or more files to compile" \
	&& return 1

for f in $@; do
	echo -n "\e[1m[${f}]...\e[0m"

	# check if file is readable
	if [[ ! -r $f || ! -f $f ]] then
		echo " \e[1;31mx\e[0m" \
			&& >&2 echo "err: file could not be read"
		continue
	fi

	# check if containing dir and outfile are writeable
	if [[ ! -w ${f:h} || ! -w ${f} ]] then
		echo " \e[1;31mx\e[0m" \
			&& >&2 echo "err: could not write to file"
		continue
	fi

	# try compile
	(cat $f | coffee -sc | babel --minified --no-babelrc --no-comments \
		| tr -d '\n' > ${f:r}.js) 2>&1 \
		| read -r -d "\0" strval

	# err check
	[[ $strval ]] \
		&& echo " \e[1;31mx\e[0m" \
		&& >&2 (echo $strval | sed 's/^\[stdin\]://') \
		&& rval=1 && rm ${f:r}.js && continue

	echo " \e[1;32mo\e[0m"
done

return $rval