Nice way to run a dev env

Nice. A couple of suggestions.

The shebang should be #!/usr/bin/env sh, which is the most portable. The script doesn’t use any bash’isms so it should work in any Bourne shell compatible shell, not just bash.

After the shebang add set -e. This will cause the script to exit if any of the commands fail. Otherwise it will happily continue executing commands and failing later on because something like bundle install failed, and the error output is now gone from the user’s screen.

Now the plugin git pull is a bit more problematic as errors written to STDERR and a failing git execution will not be properly handled. So I pushed STDERR also to STDOUT and kept track of the exit code

No need for a bunch of cd commands, or setting the PLUGIN_DIR variable.

#!/usr/bin/env sh
set -e

cd ~/discourse
git pull

for folder in plugins/*; do
  if [ -d "$folder/.git" ]; then
    exitcode=0
    output=$(git -C "$folder" pull 2>&1) || exitcode=$?
    if echo "$output" | grep -q -v "Already up to date."; then
      echo "[$(basename "$folder")]"
      echo "$output"
      [ "$exitcode" -ne 0 ] && exit "$exitcode"
    fi
  fi
done

bundle install
pnpm install
bin/rails db:migrate
DISCOURSE_HOSTNAME=localhost UNICORN_LISTENER=localhost:3000 bin/ember-cli -u

I already had a similar script, except for the git pulling as I don’t want that to happen every time.

1 Like