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

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

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

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay