Skip to content

Instantly share code, notes, and snippets.

@mgussekloo
Last active August 29, 2015 14:04
Show Gist options
  • Save mgussekloo/9077f382beac43cfe622 to your computer and use it in GitHub Desktop.
Save mgussekloo/9077f382beac43cfe622 to your computer and use it in GitHub Desktop.
Using simple bash script to compile js and css (without Grunt)
#!/bin/bash
Curpath="`pwd`"
WatchEnabled=false
SassEnabled=false
JsEnabled=false
VersionEnabled=false
while [ $# -gt 0 ]
do
key="$1"
case $key in
watch)
WatchEnabled=true
;;
# sass source
-ss)
SassEnabled=true
SassSource="$2"
SassSource=${SassSource%/}
[ "$SassSource" ] || SassSource="assets/source/sass"
shift
;;
# sass destination
-sd)
SassDestination="$2"
SassDestination=${SassDestination%/}
[ "$SassDestination" ] || SassDestination="assets/build/css/all.css"
shift
;;
# js source
-js)
JsEnabled=true
JsSource="$2"
JsSource=${JsSource%/}
[ "$JsSource" ] || JsSource="assets/source/js"
shift
;;
# js destination
-jd)
JsDestination="$2"
JsDestination=${JsDestination%/}
[ "$JsDestination" ] || JsDestination="assets/build/js/all.js"
shift
;;
# file versioning
-v)
VersionEnabled=true
VersionManifestFile="$2"
VersionSource="$3"
shift
shift
;;
*)
;;
esac
shift
done
DoWatch () {
echo "Hoi";
}
DoBuild () {
SassOutputFile=false
JsOutputFile=false
Result=0
if [ "$SassEnabled" = true ]; then
hash sass 2>/dev/null || { echo >&2 "I require sass but it's not installed. See: http://sass-lang.com/install"; exit 1; }
hash postcss 2>/dev/null || { echo >&2 "I require postcss but it's not installed. See: https://github.com/postcss/autoprefixer"; exit 1; }
# determine input of sass files
if [[ -d $SassSource ]]; then
SassFiles="${SassSource}/*.scss"
else
SassFiles=$(echo $SassSource | tr "," "\n")
fi
if [ ${#SassFiles} -gt 0 ]; then
# determine output of sass files
# can be a directory or a single output file
if [[ -d $SassDestination ]]; then
rm -f "${SassDestination}"/*.min.css
else
SassOutputFile=$SassDestination
mkdir -p "/var/tmp/guild_tmp_files"
SassDestination="/var/tmp/guild_tmp_files"
rm -f "${SassDestination}"/*.min.css
fi
Result=$(($Result || $?))
# compile each sass file
for f in $SassFiles; do
BaseF=$(basename "/${f}")
BaseF=${BaseF%%.*}
if [ ${f: -4} = ".css" ]; then
echo "Copying ${BaseF}"
cp -f "${f}" "${SassDestination}/${BaseF}.min.css"
elif [ ${BaseF:0:1} != "_" ]; then
echo "Compiling ${BaseF}"
sass --sourcemap=none --quiet "${f}" "${SassDestination}/${BaseF}.min.css"
fi
done
Result=$(($Result || $?))
# start autoprefixing
for f in $SassFiles; do
BaseF=$(basename "/${f}")
BaseF=${BaseF%%.*}
echo "Prefixing ${BaseF}"
postcss -u autoprefixer -o "${SassDestination}/${BaseF}.min.css" "${SassDestination}/${BaseF}.min.css"
done
Result=$(($Result || $?))
# concatenate if needed
if [ $SassOutputFile ]; then
echo "Concatenating..."
rm -f "${SassOutputFile}"
Result=$(($Result || $?))
for f in $SassFiles; do
BaseF=$(basename "/${f}")
BaseF=${BaseF%%.*}
(cat "${SassDestination}/${BaseF}.min.css" ; echo) >> "${SassOutputFile}"
Result=$(($Result || $?))
done
fi
fi
fi
if [ "$JsEnabled" = true ]; then
hash uglifyjs 2>/dev/null || { echo >&2 "I require uglifyjs but it's not installed. See: https://www.npmjs.com/package/uglify-js"; exit 1; }
# determine input of js files
if [ -d $JsSource ]; then
JsFiles="${JsSource}/*.js"
else
JsFiles=$(echo $JsSource | tr "," "\n")
fi
if [ ${#JsFiles} -gt 0 ]; then
# determine output of js files
# can be a directory or a single output file
if [[ -d $JsDestination ]]; then
rm -f "${JsDestination}"/*.min.js
else
JsOutputFile=$JsDestination
mkdir -p "/var/tmp/guild_tmp_files"
JsDestination="/var/tmp/guild_tmp_files"
rm -f "${JsDestination}"/*.min.js
fi
Result=$(($Result || $?))
# compile each js file
for f in $JsFiles; do
BaseF=$(basename "/${f}")
BaseF=${BaseF%%.*}
if [ ${f: -7} = ".min.js" ]; then
echo "Copying ${BaseF}"
cp -f "${f}" "${JsDestination}/${BaseF}.min.js"
elif [ ${BaseF:0:1} != "_" ]; then
echo "Compressing ${BaseF}"
uglifyjs "${f}" -c -o "${JsDestination}/${BaseF}.min.js"
fi
done
Result=$(($Result || $?))
# concatenate if needed
if [ $JsOutputFile ]; then
echo "Concatenating..."
rm -f "${JsOutputFile}"
Result=$(($Result || $?))
for f in $JsFiles; do
BaseF=$(basename "/${f}")
BaseF=${BaseF%%.*}
(cat "${JsDestination}/${BaseF}.min.js" ; echo) >> "${JsOutputFile}"
Result=$(($Result || $?))
done
fi
fi
fi
if [ "$VersionEnabled" = true ]; then
# determine input of version files
if [ -d $VersionSource ]; then
VersionFiles="${VersionSource}/*"
else
VersionFiles=$(echo $VersionSource | tr "," "\n")
fi
if [ ${#VersionFiles} -gt 0 ]; then
echo "Versioning..."
# dir to write files to
VersionManifestDir=$(dirname "${VersionManifestFile}")
# clear manifest
rm -rf "${VersionManifestDir}"/*
# enter first line
echo "{" >> "${VersionManifestFile}"
# keep count
Count=0
for f in $VersionFiles; do
MD5=($(md5sum "${f}"))
MD5=${MD5%% }
BaseFileName=$(basename "/${f}")
MD5File="${MD5}.${BaseFileName}"
RelativeFileName="${f/#public\/}"
cp -f "${f}" "${VersionManifestDir}/${MD5File}"
if [[ "$Count" -gt 0 ]]; then
echo "," >> "${VersionManifestFile}"
fi
echo "\"${RelativeFileName}\": \"${MD5File}\"" >> "${VersionManifestFile}"
Count=$((Count+1))
done
# close
echo "}" >> "${VersionManifestFile}"
fi
fi
if [ $Result -ne 0 ]; then
echo -e "Error\a"
fi
}
if [ "$WatchEnabled" = "true" ]; then
echo "Watching ..."
DoWatch
else
echo "Single run ..."
DoBuild
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment