Skip to content

Instantly share code, notes, and snippets.

@xopez
Created March 6, 2021 13:29
Show Gist options
  • Save xopez/8a19bc001945d95ffc4faad2c7cf729d to your computer and use it in GitHub Desktop.
Save xopez/8a19bc001945d95ffc4faad2c7cf729d to your computer and use it in GitHub Desktop.
cdnjs downloader
#!/usr/bin/env bash
command_exists() {
command -v "$@" >/dev/null 2>&1
}
TARGET_PATH="$PWD"
OWNER=$(id -u)
REQUIRED_PACKAGES="curl jq"
for package in $REQUIRED_PACKAGES; do
if ! command_exists "$package"; then
MISSING_PACKAGES=1
echo -e "Executable $package not found!"
fi
done
if [ -n "$MISSING_PACKAGES" ]; then
exit 1
fi;
if [ -n "$1" ]; then
TARGET_PATH="$1"
fi
TARGET_PATH="$TARGET_PATH/$3/$4"
if [ -n "$2" ]; then
OWNER="$2"
fi
if [ -z "$3" ]; then
echo "Package name not provided"
exit 1
fi
if [ -z "$4" ]; then
echo "Version number for $3 not provided"
exit 1
fi
if [ ! -d "$TARGET_PATH" ]; then
mkdir -p "$TARGET_PATH"
fi
API_URL=$(printf 'https://api.cdnjs.com/libraries/%s/%s?fields=files' "$3" "$4")
FILE_BASE_URL=$(printf 'https://cdnjs.cloudflare.com/ajax/libs/%s/%s' "$3" "$4")
for file in $(curl -sS "$API_URL" | jq -r '.files | .[]'); do
echo -e "Downloading $file..."
curl -sS "$FILE_BASE_URL/$file" --create-dirs -o "$TARGET_PATH/$file"
done
chown -R "$OWNER:" "$TARGET_PATH/../"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment