Task organisation for dev projects,
based on a pure shell script.
Download
(Latest version of task runner)
chmod +x run
mv run /usr/local/bin/run
Needs Bash 3.2+, tested on Linux and macOS.
Place a task file called run.sh
in your project root and make it executable.
Say, for a NodeJS project, it could be like:
# Start web server
run::server
() {
PORT=8000
\
CONFIG=/app/config.yml
\
node
src/server.js
}
# Install dependencies
run::install
() {
npm
install
\
--strict-peer-deps
}
A run.sh
task file is a plain regular shell script.
There is no special magic to it, it just adheres to the following convention:
The task definitions are shell commands that carry a run::
prefix.
Tasks can optionally be preceded by a descriptive comment.
The first comment line is interpreted as title,
and the following ones as additional info text.
These notation rules allow the run
CLI tool to pick up the tasks, e.g.:
run server
would execute the above “Start web server” task.
run --list
lists all available tasks.
When the task runner runs a task, it evaluates the run.sh
task file from top to bottom – so when
using third-party projects, please make sure it’s trustworthy.
Without the run
CLI tool available (e.g. when in a CI or production environment), you can still
access your tasks by sourcing the
run.sh
task file and then directly calling the task commands by their original names.
That’s all there is.
Here are a few handy shell scripting snippets.
PORT=${PORT:-8080}
PORT=$([[ "$IS_PROD" ]] && echo 443 || echo 8080)
run::hello() {
echo "$1" # First arg
echo "$2" # Second arg
}
run::print() {
echo "$@"
}
run::hello() {
run::other-task
}
[[ -f file.txt ]] # … file exists
[[ -d folder/ ]] # … directory exists
[[ -z "$VAR" ]] # … var is empty/unset (“zero”)
[[ -n "$VAR" ]] # … var is not empty
CONTENTS=$(<file.txt)
source otherfile.sh
THIS_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
source .env
# You can also do that conditionally, e.g.:
[[ -f .env ]] && source .env
[[ -f .env ]] && source .env
|| source .env.devset -o errexit