Last active
December 21, 2017 12:48
-
-
Save nybblr/43c2ff7b83c390283081945e0e535d74 to your computer and use it in GitHub Desktop.
Crawl a local Ghost blog instance and convert to a production-ready static site!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DEV_HOST="localhost:2368" | |
PROD_HOST="blog.yellowscale.com" | |
BUILD_DEST=${1:-./build} | |
DEV_URL="http://${DEV_HOST}" | |
PROD_URL="https://${PROD_HOST}" | |
# Hop into build directory | |
pushd $BUILD_DEST > /dev/null | |
# Clean build directory | |
git rm -rfq . | |
git clean -fxdq | |
git reset -q | |
git checkout -q .gitignore | |
# Crawl the entire site | |
# Requires wget: brew install wget | |
wget -q -r -nH \ | |
"${DEV_URL}/" \ | |
"${DEV_URL}/sitemap.xml" \ | |
"${DEV_URL}/sitemap-pages.xml" \ | |
"${DEV_URL}/sitemap-posts.xml" \ | |
"${DEV_URL}/sitemap-authors.xml" \ | |
"${DEV_URL}/sitemap-tags.xml" \ | |
"${DEV_URL}/rss" | |
wget -q -x -nH --content-on-error \ | |
"${DEV_URL}/404.html" | |
# Strip query params from file names | |
for i in `find . -type d -path "./.git" -prune -o -type f -print` | |
do | |
mv $i `echo $i | cut -d? -f1` | |
done | |
# Update production URL | |
# Requires gnu-sed on macOS: brew install gnu-sed | |
ESCAPE_REGEX='s/[]\/$*.^[]/\\&/g' | |
ESCAPED_DEV_HOST="$(echo $DEV_HOST | sed -e $ESCAPE_REGEX)" | |
ESCAPED_PROD_HOST="$(echo $PROD_HOST | sed -e $ESCAPE_REGEX)" | |
ESCAPED_DEV_URL="$(echo $DEV_URL | sed -e $ESCAPE_REGEX)" | |
ESCAPED_PROD_URL="$(echo $PROD_URL | sed -e $ESCAPE_REGEX)" | |
find . -type d -path "./.git" -prune -o -type f -exec gsed -i'' -e "s/${ESCAPED_DEV_URL}/${ESCAPED_PROD_URL}/g" {} + | |
find . -type d -path "./.git" -prune -o -type f -exec gsed -i'' -e "s/${ESCAPED_DEV_HOST}/${ESCAPED_PROD_HOST}/g" {} + | |
# Return to original directory | |
popd > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment