Handy way to save and run project-specific commands.

Handy way to save and run project-specific commands.

Problem Faced:

As a developer, i usually want to run some set of commands to execute/run the application. Like building and running docker container or setting some environment variables before running application or setting up the development environment like activating envs, running db's, opening vs_code like that.

We can solve these using, alias command; but i need something more friendly with a text file.

Solution

While attending one of the weekend https://ilugc.in/ talks, Sibi K was discussing on one open source tool. JUST.

Just is a handy way to save and run project-specific commands. Commands are stored in a file called justfile or Justfile with syntax inspired by make:

How it works:

  1. We just need to create a file named justfile or JustFile.

  2. Then we need to add command(s) in a groupwise manner under a name, as given below.

hello: # <- Group Name
    # List of commands to be executed in order.
    echo "Hello !"
    echo "This will be executed next"
  1. Execute the file using just.

image.png

Some cool features

1. We can have a default group, which would be executed when we aren't specifying the group name.

default:
    echo "Default !!"

hello: # <- Group Name
    # List of commands to be executed in order.
    echo "Hello !"
    echo "This will be executed next"

image.png

2. Each group is called as recipe's. We can have multiple recipe's name in the default recipe to execute them in order.

default: hello greet


hello: # <- Group Name
    # List of commands to be executed in order.
    echo "Hello !"
    echo "This will be excuted next"

greet:
    echo "Hi How are you"

image.png

3. We can also use dotenv files with justfile.

.env

# a comment, will be ignored
POSTGRES=localhost:6379
PORT=5004

justfile

set dotenv-load

serve:
    echo "Starting the server with database $POSTGRES on port $PORT"

image.png

4. Usually when an error happens in a sequence of commands, the next commands will not run. But with justfile we can ignore the errors using hypen as a prefix (-cat).

foo:
    -cat foo
    echo "Done"

image.png

5. We can also use conditional expressions with just. 6. Changing variable values from command line itself. 7. Remember that each line will be executed in a fresh shell, so if you are changing directory in a line, it won't affect the other line. 8. By using @ symbol it won't echo the commands that we wrote.

hello:
    @echo "Done"

image.png

Did you find this article valuable?

Support Parotta Salna by becoming a sponsor. Any amount is appreciated!