Skip to content

Instantly share code, notes, and snippets.

@matthewbednarski
Last active February 20, 2016 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewbednarski/bc74eb5495f0347ef3f4 to your computer and use it in GitHub Desktop.
Save matthewbednarski/bc74eb5495f0347ef3f4 to your computer and use it in GitHub Desktop.
a very ( too ) simple javascript libs manager
#!/bin/bash
##########
#
# parameters:
# conf: (required) points a cdnjs-client.json file; eg. [cdnjs-client.json](https://gist.github.com/matthewbednarski/30dd4b65221a8c8c67ee)
#
# requires: jq, wget
#
##########
conf=$1
shift
SWITCH="\e["
NORMAL="${SWITCH}0m"
YELLOW="${SWITCH}33m"
RED="${SWITCH}31m"
GREEN="${SWITCH}32m"
BLUE="${SWITCH}34m"
function warn {
echo -e $YELLOW$@$NORMAL
}
function info {
echo -e $BLUE$@$NORMAL
}
function error {
echo -e $RED$@$NORMAL
}
function get_resolved {
local save_dir="$2"
local save_file="$save_dir"/$1
local fetch_url="$3"
if [[ ! -d "$save_dir" ]]; then
mkdir -p "$save_dir"
fi
if [[ -f "$save_file" ]]; then
rm -Rf "$save_file"
fi
info "Fetching $RED$1$BLUE to $GREEN$save_dir$BLUE"
# echo -e $GREEN
wget -q -O "$save_file" $fetch_url
# echo -e $NORMAL
if [[ "$?" != "0" ]]; then
error "Could not download file $BLUE$1"
else
info "Success"
fi
echo -e
}
function get {
local name=$1
local version=$2
local file="$3"
local create=$4
local type=$5
local save_dir=
local fetch_url=
if [[ "$has_file_obj" == "true" ]]; then
local outpath=$(echo $file | jq -r '.outpath')
local file=$(echo $file | jq -r '.file')
local url=$(echo $file | jq -r '.url')
local save_dir=$dir/$outpath
local fetch_url=$url
else
if [[ "$create" == "true" ]]; then
local save_dir=$dir/$name/$version/$type
local fetch_url=$url/$name/$version/$type/$file
else
local save_dir=$dir/$name/$version
local fetch_url=$url/$name/$version/$file
fi
fi
get_resolved "$file" "$save_dir" "$fetch_url"
}
function processFiles {
local files="$1"
oifs=$IFS
IFS=$'\n'
for fileObj in $files; do
warn "Custom location"
local t_outpath=$(echo $fileObj | jq -r '.outpath')
local t_file=$(echo $fileObj | jq -r '.file')
local t_url=$(echo $fileObj | jq -r '.url')
get_resolved "$t_file" "$dir/$t_outpath" $t_url
done
IFS=$oifs
}
function get_fonts {
get $@ "fonts"
}
function get_js {
get $@ "js"
}
function get_css {
get $@ "css"
}
repos=$(jq -r '. as $in| keys[]' $conf)
for repo in $repos; do
cdn=$(jq .$repo $conf)
url=$(echo $cdn | jq -r .root)
dir=$(echo $cdn | jq -r '.["relative-location"]')
deps=$(echo $cdn | jq -r .dependencies)
keys=$(echo $deps | jq -r '. as $in| keys[]')
info "Repo: $BLUE$repo$NORMAL"
info "Url:" $url
echo -e
for d in $keys; do
lib=$d
part=$(echo $deps | jq -r '.["'$d'"]')
version=$(echo $part | jq -r '.["version"]')
js=$(echo $part | jq -r '.["js"]?')
css=$(echo $part | jq -r '.["css"]?')
fonts=$(echo $part | jq -r '.["fonts"]?')
create_fonts_dir=false
fontsA=
if [[ "$fonts" == "null" ]]; then
create_fonts_dir=false
else
fontsAFiles=$(echo $js | jq -r -c '.. | select(has("file")?)')
fontsA=$(echo $fonts | jq -r .[])
create_fonts_dir=$(echo $part | jq -r '.["create-fonts-dir"]?')
if [[ "$create_fonts_dir" == "null" ]]; then
create_fonts_dir=true
fi
fi
create_css_dir=false
cssA=
if [[ "$css" == "null" ]]; then
create_css_dir=false
else
cssAFiles=$(echo $js | jq -r -c '.. | select(has("file")?)')
cssA=$(echo $css | jq -r .[])
create_css_dir=$(echo $part | jq -r '.["create-css-dir"]?')
if [[ "$create_css_dir" == "null" ]]; then
create_css_dir=true
fi
fi
create_js_dir=false
jsA=
if [[ "$js" == "null" ]]; then
create_js_dir=false
else
jsAFiles=$(echo $js | jq -r -c '.. | select(has("file")?)')
jsA=$(echo $js | jq -r '.[] | select( type == "string") ')
create_js_dir=$(echo $part | jq -r '.["create-js-dir"]?')
if [[ "$create_js_dir" == "null" ]]; then
create_js_dir=true
fi
fi
if [[ "" != "$fontsAFiles" ]]; then
processFiles "$fontsAFiles"
fi
if [[ "" != "$cssAFiles" ]]; then
processFiles "$cssAFiles"
fi
if [[ "" != "$jsAFiles" ]]; then
processFiles "$jsAFiles"
fi
for _font in $fontsA; do
get_fonts "$lib" "$version" "$_font" "$create_fonts_dir"
done
for _js in $jsA; do
get_js "$lib" "$version" "$_js" "$create_js_dir"
done
for _css in $cssA; do
get_css "$lib" "$version" "$_css" "$create_css_dir"
done
done
done
{
"gists": {
"root": "https://gist.githubusercontent.com",
"relative-location": "src/test/webapp/motive/libs",
"dependencies": {
"notmasteryet": {
"url": "https://gist.githubusercontent.com/notmasteryet/1057924/raw/d97b4e2e67918e6a8c778ff11d551f7f6a057ca1/hacks.js",
"version": "1057924/raw/d97b4e2e67918e6a8c778ff11d551f7f6a057ca1",
"create-js-dir": false,
"js": [
{
"file": "myD.js",
"url": "https://gist.githubusercontent.com/matthewbednarski/fadc78a75c3bb1c01d6b/raw/2d2bd13c3acc176f0590efd8739805981074d02c/datepicker-directive.js",
"outpath":"directive/test"
},
{
"file": "hacks2.js",
"url": "https://sdafvlsjdflgist.githubusercontent.com/notmasteryet/1057924/raw/d97b4e2e67918e6a8c778ff11d551f7f6a057ca1/hacks.js",
"outpath":"tsdfasdjh"
}
]
}
}
},
"eligrey": {
"root": "https://raw.githubusercontent.com/eligrey",
"relative-location": "src/test/webapp/motive/libs",
"url": "https: //raw.githubusercontent.com/eligrey/Blob.js/master/Blob.js",
"dependencies": {
"Blob.js": {
"version": "master",
"create-js-dir": false,
"js": [
"Blob.js"
]
}
}
},
"cloudflare": {
"root": "https://cdnjs.cloudflare.com/ajax/libs",
"relative-location": "src/test/webapp/motive/libs",
"dependencies": {
"font-awesome": {
"version": "4.4.0",
"css": [
"font-awesome.min.css"
],
"fonts": [
"fontawesome-webfont.woff",
"fontawesome-webfont.woff2",
"fontawesome-webfont.eot",
"fontawesome-webfont.ttf"
]
},
"lodash.js": {
"version": "3.10.1",
"create-js-dir": false,
"js": [
"lodash.min.js"
]
},
"pouchdb": {
"version": "3.6.0",
"create-js-dir": false,
"js": [
"pouchdb.js",
"pouchdb.min.js"
]
},
"moment.js": {
"version": "2.10.3",
"create-js-dir": false,
"js": [
"moment.min.js",
"moment-with-locales.min.js"
]
},
"jquery": {
"version": "2.1.4",
"create-js-dir": false,
"js": [
"jquery.min.js"
]
},
"angular.js": {
"version": "1.3.18",
"create-js-dir": false,
"js": [
"angular.min.js"
]
},
"angular-translate": {
"version": "2.7.2",
"create-js-dir": false,
"js": [
"angular-translate.min.js"
]
},
"angular-ui": {
"version": "0.4.0",
"create-js-dir": false,
"create-css-dir": false,
"css": [
"angular-ui.min.css"
],
"js": [
"angular-ui-ieshiv.min.js",
"angular-ui.min.js"
]
},
"angular-smart-table": {
"version": "2.1.3",
"create-js-dir": false,
"js": [
"smart-table.js",
"smart-table.min.js"
]
},
"bootstrap-datepicker": {
"version": "1.4.0",
"js": [
"bootstrap-datepicker.min.js"
],
"css": [
"bootstrap-datepicker.min.css"
]
},
"twitter-bootstrap": {
"version": "3.3.5",
"js": [
"bootstrap.min.js",
"bootstrap.js"
],
"css": [
"bootstrap.min.css",
"bootstrap.css"
],
"fonts": [
"glyphicons-halflings-regular.woff",
"glyphicons-halflings-regular.eot",
"glyphicons-halflings-regular.ttf",
"glyphicons-halflings-regular.woff2"
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment