DEV Community

Cover image for Day 3: JS Scripts 🚀
Valeria
Valeria

Posted on

Day 3: JS Scripts 🚀

I like JavaScript and I don't like bash! And today I'd like to share with you a tool that allows me to do less of the latter: zx.

There are several ways to install it, with Deno you could do it like so:

deno install --global -A npm:zx
Enter fullscreen mode Exit fullscreen mode

Create a script, e.g. list.ts:

#!/usr/bin/env zx
import { $ } from 'npm:zx'

const list = await $`ls`
console.log(list)
Enter fullscreen mode Exit fullscreen mode

Allow execution as a script:

chmod +x list.ts
Enter fullscreen mode Exit fullscreen mode

And, finally, run it:

./list.ts
Enter fullscreen mode Exit fullscreen mode

Process output with files list

Isn't it awesome?! You can use any command available on your machine, check this example out:

#!/usr/bin/env zx

await $`cat package.json | grep name`

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

await Promise.all([
  $`sleep 1; echo 1`,
  $`sleep 2; echo 2`,
  $`sleep 3; echo 3`,
])

const name = 'foo bar'
await $`mkdir /tmp/${name}`
Enter fullscreen mode Exit fullscreen mode

And, as any other package, you could use zx as a library. Imagine all the cool things you could build!

Liked the content and would love to have more of it all year long?

Buy Me A Coffee

Top comments (0)