No Time Dad

A blog about web development written by a busy dad

Automating the Small Things

Quick Word of Caution

I really do believe that automating even the smallest of tasks will save a huge amount of time in the long run. But, if you find yourself starting to think “I’ll just automate a few more things then I’ll work on my project” or you are just spending a lot of time figuring out how to automate a task I’d suggest stopping immediately and getting back to working on the project itself. Staying focused is the key to maximizing your time and finishing your project…automating things is really just meant to help support that objective, not replace it.

Context

When I talk about automating tasks here I am referring to tasks you need to perform during development of a project (software or otherwise). If your goal is learn about automation in general or to learn a new technology then I urge you to take your time and actually learn. For the purposes of this post, I simply want to convey how automation can get your project across the finish line faster. Additionally, this post isn’t meant to be a tutorial on automation, it is just my thoughts and process on automating repetitive tasks.

When to Automate

For me, I tend to automate tasks that require me to enter 2 or more commands in the command line or if the command is complex and has numerous arguments or flags. I will also look to automate commands that I know I will repeat on a semi-regular basis during development on a project. It does obviously take time to write a script and get it working, but even automating 3 commands that take 30 seconds to run by hand on a daily basis will save you a huge amount of time in the long run. You also have to consider the fact that once you have the script working correctly, it won’t make mistakes. It will do exactly what you tell it to.

How to Automate

Short answer: whatever language you are most comfortable with. Don’t overthink this too much. Just use whatever you can get going with quickly and is available on the system you are working with. I personally reach for bash first because I typically develop on some flavor of Linux machine, but I will also use Python or even Nodejs if the task can be done easier in another language. The point here is that you probably won’t be shipping whatever script you write, you just need it to work reasonably well for the task.

Example

I very recently wanted a way to create a new blog post here instead of manually creating the directory and index.md file by hand each time. It appears that the gatsby cli does not have an option for creating new blog posts, so I wrote a small script to handle this for me. Already it has been a huge help and even a bit motivating. Below is how I approached writing this shell script.

Basically the script changes directories into the content/blog/ directory, creates a new directory that I supply as an argument when I run the script, and copies in a stub index.md that I go in and populate with the actual content for the post itself. The simplest version of the script looks like this:

#!/bin/bash

BLOG_POST_DIR=$1
cd ../content/blog/
mkdir $BLOG_POST_DIR
cp ../../scripts/base-index.md ${BLOG_POST_DIR}/index.md

The script will work fine like this, but it has to be run from a directory one level out from content/. This is an easy thing to forget and would cost me time later to debug, so I added a basic check to ensure it is being run from the scripts directory. Yes, I am aware you can do some clever things with the PATH to ensure it goes to correct directory no matter where you run it from, but I didn’t know how to do that off of the top of my head and I didn’t want to spend a ton of time researching it so I settled on a basic check and moved on. Here is the script now.

#!/bin/bash

BLOG_POST_DIR=$1
WORKING_DIR=$(pwd)

if [[ $WORKING_DIR != *scripts ]]; then
  echo 'Please run this from the scripts dir.'
  exit 0
fi

cd ../content/blog/
mkdir $BLOG_POST_DIR
cp ../../scripts/base-index.md ${BLOG_POST_DIR}/index.md

Another easy thing for me to forget is that I have to supply the name of the directory I want to create as an argument when I run the script. Again, to save me time later I added a quick check for that with a helpful message.

#!/bin/bash

BLOG_POST_DIR=$1
WORKING_DIR=$(pwd)

if [[ $WORKING_DIR != *scripts ]]; then
  echo 'Please run this from the scripts dir.'
  exit 0
fi

if [ -z $BLOG_POST_DIR ]; then
  echo 'Please supply a blog post directory name as the first arg'
  exit 0
else
  cd ../content/blog/
  mkdir $BLOG_POST_DIR
  cp ../../scripts/base-index.md ${BLOG_POST_DIR}/index.md
fi

Finally, the mkdir command will fail if the directory already exists but I might forget why it already exists and have to do some hunting which costs me time. So, I’ll add a line to check for the directory with a helpful message if it already exists. The finished version of the script is below.

#!/bin/bash

BLOG_POST_DIR=$1
WORKING_DIR=$(pwd)

if [[ $WORKING_DIR != *scripts ]]; then
  echo 'Please run this from the scripts dir.'
  exit 0
fi

if [ -z $BLOG_POST_DIR ]; then
  echo 'Please supply a blog post directory name as the first arg'
  exit 0
else
  cd ../content/blog/
  if [ -d $BLOG_POST_DIR ]; then
    echo "Directory $BLOG_POST_DIR already exists. Please delete the directory or choose a different name."
  else
    mkdir $BLOG_POST_DIR
    cp ../../scripts/base-index.md ${BLOG_POST_DIR}/index.md
  fi
fi

And that’s it. The script isn’t pretty and far from perfect, and that is the point. It is just meant to help me move a little bit faster, which it does quite well.