Skip to content

Instantly share code, notes, and snippets.

@philippbosch
Created July 26, 2011 18:53
Show Gist options
  • Save philippbosch/1107566 to your computer and use it in GitHub Desktop.
Save philippbosch/1107566 to your computer and use it in GitHub Desktop.
Small bash script to fetch the latest version of some JS libraries from the net and store it locally

latest

latest is a small bash script that fetches the latest version of a JavaScript library from the net and stores it in a local file.

Installation

Copy latest to a directory in your path, e.g. /usr/local/bin or ~/bin.

Usage

latest LIBRARY [DESTINATION]

e.g. latest jquery jquery.js

Available libraries

Contribute

Fork this Gist and add more libraries or features.

#!/bin/bash
SCRIPTNAME=${0##*/}
LIBRARIES="jquery zepto backbone underscore prototype"
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
txtred=$(tput setaf 1) # Red
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtrst=$(tput sgr0) # Text reset
function usage() {
echo "${txtund}Usage:${txtrst} $SCRIPTNAME LIBRARY [DESTINATION]"
echo
echo "Libraries available:"
for LIB in $LIBRARIES
do
echo " ${txtbld}›${txtrst} $LIB"
done
exit 2
}
if [ -z "$1" ]
then
usage
fi
if [ -n "$2" ]
then
DESTINATION=$2
else
DESTINATION=""
fi
function check_exists() {
if [ -e $1 ]
then
read -p "${txtylw}${txtbld}$1 exists. Overwrite [yN]?${txtrst} " REALLY
if [ "$REALLY" != "y" ]
then
exit 99
fi
fi
}
function simple_get() {
getting $3
check_exists $2
curl -s $1 > $2
downloaded $3 $2
}
function getting() {
echo "${txtblu}${txtbld}Getting $1 …${txtrst}"
}
function downloaded() {
echo "${txtgrn}${txtbld}$1 downloaded and saved as $2.${txtrst}"
}
case $1 in
jquery)
[[ -n "$DESTINATION" ]] && FILENAME=$DESTINATION || FILENAME="jquery.min.js"
simple_get "http://code.jquery.com/jquery.min.js" $FILENAME "jQuery"
;;
zepto|zeptojs)
getting "Zepto.js"
[[ -n "$DESTINATION" ]] && FILENAME=$DESTINATION || FILENAME="zepto.min.js"
check_exists $FILENAME
HTML=`curl -s http://zeptojs.com/`
ZIP_PATH=`echo $HTML | perl -wlne 'print $1 if /<a href="(\/downloads\/zepto-[^"]+)"/'`
ZIP_URL="http://zeptojs.com${ZIP_PATH}"
ZIP_TEMP=`mktemp -t get.zepto`
wget -q -O $ZIP_TEMP $ZIP_URL
unzip -p $ZIP_TEMP */dist/zepto.min.js > $FILENAME
rm $ZIP_TEMP
downloaded "Zepto.js" $FILENAME
;;
backbone|backbonejs)
[[ -n "$DESTINATION" ]] && FILENAME=$DESTINATION || FILENAME="backbone.min.js"
simple_get "http://documentcloud.github.com/backbone/backbone-min.js" $FILENAME "Backbone.js"
read -p "Should I also download Underscore.js, a requirement of Backbone.js? [yN] " UNDERSCORE
if [ "$UNDERSCORE" = "y" ]
then
$0 underscore
fi
;;
underscore|underscorejs)
[[ -n "$DESTINATION" ]] && FILENAME=$DESTINATION || FILENAME="underscore.min.js"
simple_get "http://documentcloud.github.com/underscore/underscore-min.js" $FILENAME "Underscore.js"
;;
prototype|prototypejs)
getting "Prototype"
[[ -n "$DESTINATION" ]] && FILENAME=$DESTINATION || FILENAME="prototype.js"
check_exists $FILENAME
HTML=`curl -s http://www.prototypejs.org/download`
URL=`echo $HTML | perl -wlne 'print $1 if /(https:\/\/ajax.googleapis.com\/[^"]+prototype.js)/'`
curl -s $URL > $FILENAME
downloaded "Prototype" $FILENAME
;;
*)
echo "${txtund}${txtred}Error:${txtrst} Unknown library: $1"
exit 99
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment