Easier Shell Scripting with ZX (first look)
Ғылым және технология
We've all had to write bash scripts from time to time, but maybe you prefer to write JavaScript. If that's the case, you'll love ZX, a tool from Google that blends the power of shell scripting with the familiarity of JavaScript.
Пікірлер: 31
Really interesting maybe I will pitch this to my team in the new year as we only have maybe 3 people that can actually handle the more complex shell scripts we have.
Another great video. Thank you!
As someone who doesn't know how to do anything complex in bash, this is fantastic. i will try it out.
peter parker in the alternate universe
As a web developer, I want to give Google a kiss. I literally hate Bash's syntax because I always had to Google everything every time I wanted to script something. And when I started using Fish, POSIX compliance was a headache. I was thinking of creating a wrapper module for Node.js exactly like this. Great video btw.
dude, awesome find!
It s a great idea, I also start using rb.
Looks nice.
Check out the deno package called rush. Just a prototype, but similar concept. I think Deno is really good for scripting since you don't have to worry about installing any dependencies.
@andrew-burgess
Жыл бұрын
Thanks, I’ll check it out!
that's dope 👌
babashka is awesome way to script tooo
Supercool +1 sub!
Have you managed to make this work with TypeScript? I would like to rewrite a CLI i made before but with zx. But if I'm not able to do it with TS it's not worth it imo.
@avi7278
Жыл бұрын
Does not seem like the compiler could handle it. Those literals prefixed with $ are not valid syntax, are they?
@loic.bertrand
Жыл бұрын
@@avi7278it is valid JS and TS syntax, it's called a Tagged Templated Literal 🙂 here '$' is tag function which processes the template literal as well as its "dynamic" values (${...} parts in the template)
@SuperQuwertz
4 ай бұрын
You can use bun to run the script without transpiling it
@SuperQuwertz
4 ай бұрын
Or just use JSDocs Or transpile it (which isnt that nice imo)
While this is cool, the utility of bash commands are that they can be run on any system with the same OS. Using ZX will create a dependency on having node installed and this package installed. Now you need to know both bash and JS use this. Why not just use execSync package is JS? But if you are using it for personal project or you are the main mainter of a project this make sense.
@PaulJohnLeonard
Жыл бұрын
I currently use execSync. I will probably switch to zx because it looks like the code will have less boiler plate and be easier to read. I dont mind the one off devop dependancy.
cool ✌🏻
As much as it looks nice, it seems to overly complicate things
@kris10an64
Жыл бұрын
How exactly?
@jaysistar2711
Жыл бұрын
No, I've been doing this sort of thing with Jake, a Make-like task runner with tasks written in JavaScript, and this greatly simplifies things.
are you using neovim?
@andrew-burgess
Жыл бұрын
I am! More about my setup here: shaky.sh/tools/
You kind of look like Linus Torvalds when he was young
nop, if i had to use this might just use python
@andrew-burgess
Жыл бұрын
Fair enough! I don’t know much python, is there a good way to get the output of shell commands, or is there just a better way to do it?
@twothreeoneoneseventwoonefour5
Жыл бұрын
@@andrew-burgess yes there are a lot of utils in python to interact with the shell and it is pretty easy and straightforward. subprocess, shutil is often good enough for most tasks though to get only output iirc(didnt use python for some time) there was subprocess.check_output or something like that. I used it to automate some stuff, getting output from ffprobe and feeding it to ffmpeg for example. subprocess have both sync and async procesd spawn, and as I said above can also get output of a command or a cli program easily
i wrote my own implementation without knowing about this.. thanks for allowing me to discover it now. prior to this i was using the exec function from node:child-process as follows. it very much does the same thing that this is doing, just custom the following is an example of how it was written and how it can be used ```ts import { exec, ExecException } from 'child_process'; class Shell { static async run(command: string = 'ls', after = (stderr: ExecException | null, stdout: string): void => {}): Promise { return await new Promise((resolve): void => { exec(command, (stderr, stdout): void => { after(stderr, stdout); resolve(stdout); }); }); } } await Shell.run(`Echo "Hello World!"`, (_, stdout) => process.stdout.write(stdout)) const fonts = await Shell.run(`ls ./Fonts`) ``` this works fine but it requires some know-how of how promises work and how to wrap exec like this. overall zx makes this easier. thank you again!