I predict that most uses of zx in the wild are people rewriting their Bash scripts into JavaScript/TypeScript.
For example, I imagine a common script might look something like this:
It strikes me that this could be simplified. zx could automatically await every $ invocation. If it did that, then the code could be simplified to this:
$`npx tsc`;
$`npx prettier .`;
$`npx eslint .`;
Much better! However, I presume the downside to this approach is that we lose the ability to do background tasks. For example, the previous code might want to be written like:
I chose the & symbol because that is the background execution operator in bash. In this setup, we get the best of both worlds.
Of course, this would be a breaking change and might disrupt the ecosystem. So, another idea is to add an option:
$.alwaysAwait = true;
Yet another idea/approach would be to use a separate symbol other than a dollar sign as an indication of "run a command like $, but always await it automatically". For example:
_`npx tsc`;
_`npx prettier .`;
_`npx eslint .`;
(Underscore and dollar sign are the only symbols that are valid as function names, so here I'll somewhat-arbitrarily choose underscore since dollar sign is taken.)
const foo = $(await $`foo`).toString();
const bar = $(await $`bar`).toString();
It strikes me that this could be abstracted further. Similar to my previous suggestion, it could be possible to add a symbol that is similar to $, but combines the await and toString parts. As an example, I'll arbitrarily choose the o letter, which is short for "output":
const foo = o`foo`;
const bar = o`bar`;
Another option is to use an underscore instead of o. Regardless, this would be a nice abstraction.
Question 1
I predict that most uses of
zx
in the wild are people rewriting their Bash scripts into JavaScript/TypeScript. For example, I imagine a common script might look something like this:It strikes me that this could be simplified.
zx
could automaticallyawait
every$
invocation. If it did that, then the code could be simplified to this:Much better! However, I presume the downside to this approach is that we lose the ability to do background tasks. For example, the previous code might want to be written like:
So that's a problem. One idea is to parse the command for the
&
symbol, which would disable the awaiting behavior, like this:I chose the
&
symbol because that is the background execution operator in bash. In this setup, we get the best of both worlds. Of course, this would be a breaking change and might disrupt the ecosystem. So, another idea is to add an option:Yet another idea/approach would be to use a separate symbol other than a dollar sign as an indication of "run a command like $, but always await it automatically". For example:
(Underscore and dollar sign are the only symbols that are valid as function names, so here I'll somewhat-arbitrarily choose underscore since dollar sign is taken.)
Question 2
It looks like this is a common pattern:
It strikes me that this could be abstracted further. Similar to my previous suggestion, it could be possible to add a symbol that is similar to
$
, but combines the await and toString parts. As an example, I'll arbitrarily choose theo
letter, which is short for "output":Another option is to use an underscore instead of
o
. Regardless, this would be a nice abstraction.