An Alternative to NPM and Yarn Scripts - Bonnie!
Tags: rust, npm, yarn
Published: 2021-04-13 00:19:49

{% github arctic-hen7/bonnie %}

Over the last two weeks, I've been moving away from NodeJS and the JavaScript ecosystem to a great extent, mostly because I think the JavaScript world has blown itself out of proportion. To make a basic, performant, modern app, you now need to spend at least a day on proper boilerplate. The number of frameworks has grown exponentially, and I think the ecosystem is just flooded.

So, I decided to try out [Elm](https://elm-lang.org), a functional programming language with **no runtime errors** (yes, only compile-time errors). It's fantastic! However, Elm is not designed for the backend, so I decided to finally learn [Rust](https://rust-lang.org), something I've been meaning to do for about a year now.

Having adopted these two new languages, I found myself wanting a good system to shorten commands for me - like NPM or Yarn scripts. Make was the most obvious option, but it feels somewhat overkill for essentially an aliasing system. Makefiles also have their own syntax, which, although miniscule, was not something I had any desire to delve into. Instead, I decided my first Rust project would be a replacement for NPM and Yarn scripts - Bonnie!

Bonnie is a super-fast command aliasing tool designed to replace NPM or Yarn scripts entirely. You can define simple commands like so:

```toml
[scripts]
greet = "echo Hello World"
```

And then run them like this:

```
bonnie greet
```

You can mimic the default behavior of NPM and Yarn scripts, to append any given arguments to the end of the command, by adding the double percent sign (`%%`) to the end of the command:

```
[scripts]
greet = "echo Hello %%"
```

And you can add arguments to the end easily when you run it:

```
bonnie greet Donald Knuth in math-land
```

You can even add specific arguments for aliases to take:

```
[scripts]
greet.cmd = "echo Hello %firstname %lastname"
greet.args = [
    "firstname",
    "lastname"
]
```

And those can be run like so:

```
bonnie greet Donald Knuth
```

Bonnie will gracefully tell you when you've done something wrong, like provided too few arguments, or used invalid syntax in your config file. By default, that config is stored at `bonnie.toml`, but you can specify an alternative path by setting the `BONNIE_CONF` environment variable.

This is my first ever CLI, and it's also the first time I've used GitHub Actions to automate building my code on different OSes! Bonnie has binaries available for Linux, MacOS, and Windows on the [releases page](https://github.com/arctic-hen7/bonnie/releases).

I think Bonnie has been a great opportunity for me to dive into a complex language like Rust head-first, because no matter how long you spend poring over the Rust Book (about 7 hours in my case), you can never learn programming without actually building something. In the particular case of Rust, you can never get your head around lifetimes without actually building something!

I'm using Bonnie daily now, and so far it's working really well! I'm not aware of any alternatives to NPM and Yarn scripts that are as simple as Bonnie, and I hope this will help some people!