DEV Community

Cover image for Justfile make your life easier
Cheulong Sear
Cheulong Sear

Posted on

Justfile make your life easier

What is Just?

just is a handy way to save and run project-specific commands.
Commands, called recipes, are stored in a file called justfile with syntax inspired by make.

Why use Just?

just gives you cleaner, safer, more portable project commands than ad-hoc shell scripts or long README copy-paste commands.

just has a ton of useful features, and many improvements over make:

  • just is a command runner, not a build system, so it avoids much of make's complexity and idiosyncrasies. No need for .PHONY recipes!

  • Linux, MacOS, Windows, and other reasonable unices are supported with no additional dependencies. (Although if your system doesn't have an sh, you'll need to choose a different shell.)

  • Errors are specific and informative, and syntax errors are reported along with their source context.

  • Recipes can accept command line arguments.

  • Wherever possible, errors are resolved statically. Unknown recipes and circular dependencies are reported before anything runs.

  • just loads .env files, making it easy to populate environment variables.

  • Recipes can be listed from the command line.

  • Command line completion scripts are available for most popular shells.

  • Recipes can be written in arbitrary languages, like Python or NodeJS.

And much more!

Installation

just should run on any system with a reasonable sh, including Linux, MacOS, and the BSDs.

For Ubuntu:

sudo apt install just
Enter fullscreen mode Exit fullscreen mode

Example

Here is the example of just with vitest in bun:

# sum.ts

console.log('environment is', process.env.NODE_ENV);

export const sum = (a: number, b: number) => {
    return a + b;
}

console.log(sum(1, 2));
Enter fullscreen mode Exit fullscreen mode
# sum.test.ts

import { expect, test } from 'vitest'
import { sum } from './sum.ts'

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3)
})
Enter fullscreen mode Exit fullscreen mode
  "scripts": {
    "start": "bun run sum.ts",
    "test": "vitest"
  }
Enter fullscreen mode Exit fullscreen mode

Create justfile in the root directory of the project:

default:
    just --list --unsorted

start:
    bun run start

test:
    vitest

Enter fullscreen mode Exit fullscreen mode

Run just:

just
Enter fullscreen mode Exit fullscreen mode
just start
Enter fullscreen mode Exit fullscreen mode
just test
Enter fullscreen mode Exit fullscreen mode

Other Arguments

Env

For example, if your .env file contains:

DATABASE_ADDRESS=localhost:6379
SERVER_PORT=1337
Enter fullscreen mode Exit fullscreen mode

And your justfile contains:

set dotenv-load

serve:
  @echo "Starting server with database $DATABASE_ADDRESS on port $SERVER_PORT…"
  ./server --database $DATABASE_ADDRESS --port $SERVER_PORT
Enter fullscreen mode Exit fullscreen mode

just serve will output:

$ just serve
Starting server with database localhost:6379 on port 1337…
./server --database $DATABASE_ADDRESS --port $SERVER_PORT
Enter fullscreen mode Exit fullscreen mode

### Export

The export setting causes all just variables to be exported as environment variables. Defaults to false.

set export

a := "hello"

@foo b:
  echo $a
  echo $b
Enter fullscreen mode Exit fullscreen mode
$ just foo goodbye
hello
goodbye
Enter fullscreen mode Exit fullscreen mode

Positional Arguments

If positional-arguments is true, recipe arguments will be passed as positional arguments to commands. For linewise recipes, argument $0 will be the name of the recipe.

For example, running this recipe:

set positional-arguments

@foo bar:
  echo $0
  echo $1
Enter fullscreen mode Exit fullscreen mode

Will produce the following output:

$ just foo hello
foo
hello
Enter fullscreen mode Exit fullscreen mode

=== Done ===

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Top comments (0)