DEV Community

Cover image for Day 3: JS Scripts 🚀
Valeria
Valeria

Posted on

3

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay