Tuesday, May 18, 2021

Node Shell Scripting

Came across something interesting on the weekend when reading through Hacker Newsletter. It's a command line tool called zx which enables really simple shell scripting in Node.js.

Here is some syntax taken straight from the docs.

#!/usr/bin/env zx

await $`cat package.json | grep name`

let branch = await $`git branch --show-current`
await $`dep deploy --branch=${branch}`

I remember trying to write shell scripts with Node.js in the past and its been a pain in the ass. This basic $command notation simplifies this greatly! The thing is you also just don't get $command you also get:

  1. fetch, make api calls
  2. question, get user input
  3. chalk, colorize text

This seems really powerful and will probably be my go to choice for automation and scripting in the future.