Building and Serving with Shell Tools
This site is a work-in-progress. Some of the information is incomplete and may not work as described. See the homepage for details.
Table of Contents:
Aliases
alias clean='bundle exec jekyll clean'
alias build='bundle exec jekyll build'
alias serve='bundle exec jekyll serve --livereload'
Where you can get into trouble with aliases is trying to do too much at once. Below you can see an couple of examples. I wanted to run two commands and instead of using a function or a script, I setup the parallelshell NPM package and called that. Not my finest bit of programming, but it does work.
alias serve='parallelshell "bundle exec jekyll serve --livereload" "~/.local/share/firefox_dev/firefox 'http://localhost:4000/'"'
alias serve-bs='parallelshell "bundle exec jekyll build --watch" "browser-sync start --server '_site' --files '_site' --browser '~/.local/share/firefox_dev/firefox'"'
Functions
After deciding that aliases weren’t going to serve me1, I moved on to functions. Mostly I was looking to check for multiple configuration files. Some projects I work on use _config.local.yml for local development variables, some use _config-dev.yml.
# Jekyll serve commands
# `2>/dev/null` added to suppress the "No such file or directory" output when the file isn't found.
serve() {
if [[ $(ls -A "_config.local.yml" 2>/dev/null) ]]; then
command bundle exec jekyll serve --livereload --config _config.yml,_config.local.yml
elif [[ $(ls -A "_config-dev.yml" 2>/dev/null) ]]; then
command bundle exec jekyll serve --livereload --config _config.yml,_config-dev.yml
else
command bundle exec jekyll serve --livereload
fi
}
Makefiles
As you can see from the example file below, there is only a two character difference between typing make install
versus bundle install
. And if you’ve already created an alias or a function for your serve and build commands, a Makefile is redundant.
Where a Makefile might be useful to you is multi-developer teams. You can commit your Makefile to the git repo so you know everyone on the team is running the same commands.
install:
bundle install
upgrade:
bundle update
s serve:
bundle exec jekyll serve --livereload
b build:
JEKYLL_ENV=production bundle exec jekyll build
The one thing a Makefile can do that is more useful is run multiple commands and even other scripts. Let’s look at this example:
build:
JEKYLL_ENV=production bundle exec jekyll build
publish: build gh-pages
gh-pages:
shell/publish
In this example you could publish your site to GitHub Pages by typing make publish
. This would build the site and call your publish script.
Note make
is not avaliable natively in Windows. If you are using the Linux subsystem, you should be fine. Check the docs to be for sure: https://www.gnu.org/software/make/manual/html_node/index.html
NPM
If you’re running Webpack or some other Javascript compilation tool, you might want to consider calling Jekyll from your package file.
"scripts": {
"serve": "./node_modules/.bin/webpack --watch | bundle exec jekyll serve --livereload",
"build-jekyll": "bundle exec jekyll build",
"build-webpack": "./node_modules/.bin/webpack",
"watch": "./node_modules/.bin/webpack --watch"
}
Footnotes
-
Who doesn’t like a good pun? ↩