DEV Community

Cover image for Meet zx: A Better Way to Write Scripts with Node.js

Meet zx: A Better Way to Write Scripts with Node.js

Alex Lakatos 🥑 on May 11, 2021

Bash is great and all, but it’s not something I’ll pick up in a day. I was looking for something a little bit more convenient to write scripts in. ...
Collapse
 
vonheikemen profile image
Heiker

I saw that the other day, I must say I like a lot because it looks similar to a thing I did a while ago. But there is a "missed oportunity" in this tool. The implementation of $ is not really cross-platform, which is quite a shame. Also, it would be nice if they provide an argument parser like minimist or arg.

Collapse
 
lakatos88 profile image
Alex Lakatos 🥑

Yeah, I think it's still very new, and might mature in the future. I have high hopes for it.

I don't usually think about shell scripts as accepting arguments, I usually build CLIs for that use case, with things like oclif or js-fire. But when I was trying to use arguments in zx even if for the sake of my example, I did miss a better way to parse arguments than process.argv. Hopefully it's something they'll add in the near future.

Collapse
 
vonheikemen profile image
Heiker

Looking throught the issues it does appear like the maintainer wants to keep things as simple as possible. Doesn't look too eager to change things. Anyway, is still a good project.

You know I just remembered something: one can actually install this tool directly from github. So, people could fork this project, tweak a little bit and then install the fork like this.

npm install --global github:<some-user-name>/zx 
Enter fullscreen mode Exit fullscreen mode

That would be an interesting use of this npm install feature.

Thread Thread
 
lakatos88 profile image
Alex Lakatos 🥑

Oh, that's a really good idea. Especially since the work required would be minimal, for example using minimist for arguments would imply adding a few lines for importing the package and exposing it within the scope of zx.

Collapse
 
ubuntupunk profile image
ubuntupunk

from zx api docs:

minimist

const argv = minimist(process.argv.slice(2), {})

Collapse
 
frondor profile image
Federico Vázquez

Sorry I don't get it, but how is that different from executing node ./my-script-file.js ?

Collapse
 
lakatos88 profile image
Alex Lakatos 🥑

That means I have to have a package.json next to my-script-file.js, and install dependencies. It also doesn't have access to $(unix-command), I'd have to run exec() in my script file, and then worry about sanitizing those commands.

zx has a bunch of defaults and modules out of the box, so it conveniently lets me make my scripts portable and easier to write without having to worry about a bunch of the boilerplate and legwork I would need if I did node ./my-script-file.js. It's the same argument I guess of using any library over writing it from scratch yourself instead. Convenience. It also comes with the same drawbacks of using libraries, mostly trust issues.

Collapse
 
siddharthshyniben profile image
Siddharth

This was a funny one! Especially the math part 😂

Collapse
 
lakatos88 profile image
Alex Lakatos 🥑

Aww, thank you! I do what I can 😅

Collapse
 
siddharthshyniben profile image
Siddharth

I don't think I could ever write like that 😃

Collapse
 
tusharsadhwani profile image
Tushar Sadhwani

If you're interested, I wrote a python version that's much cleaner and easier to use (imo) 😉 Available here