-
-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: shorter $.sync #581
Comments
const s = $.sync;
// Good!
s`foo`;
s`bar`;
s`baz`; |
Hi @sindresorhus, thanks for the quick reply. As I said in the original post, I know that I can do this for my own projects by wrapping |
Hi @Zamiell, I agree with @sindresorhus that, since |
@ehmicky Thanks for the quick reply. I'm sad to hear that you don't like the idea, as I think having
I get a red squiggly line underneath. So now, I have to go manually open up one of my other scripts, find the line that says import { $ } from "execa";
const s = $.sync;
s`npx tsx`;
s`npx eslint .`; Overall, this is not a great user experience. But if import { s } from "execa";
s`npx tsx`;
s`npx eslint .`; Much easier, and no manual copy-pasting involved! Because auto-importing is doing all of the work. |
By the way, I think another great feature would be Consider the following code: import { $ } from "execa";
const branch = $.sync`git branch --show-current`.stdout;
if (branch === "main") {
// Do something.
} This is kind of ugly. I think it's a really common pattern to run a command and then put the output into a variable. So that pattern could be greatly simplified by import { o } from "execa";
const branch = o`git branch --show-current`;
if (branch === "main") {
// Do something.
} What do you think? |
Thanks for going into more details over this. API surfaceI think one risk that you highlight in your second message is that there might be a temptation to add more and more exports. For example, if getting While convenience methods improve the developer experience by providing with shortcuts for common tasks, they also extend the API surface, making the documentation terser for new users. It's a trade-off. Parallel commandsAnother thing is that synchronous methods can be an anti-pattern that we might want to discourage. One would probably argue that in a typical script that runs commands one after the other, holding the CPU is not a problem at all, which is true. However, running commands in parallel can provide with a big performance improvement, and it can only be done asynchronously. In my experience, most typical scripts can run at least some of their commands in parallel, and it is often overlooked by developers used to sync methods. One could then probably argue that users can always use both the synchronous and asynchronous methods. However, I have seen in practice that it can be hard to introduce async calls in more complex scripts, with many levels of nested functions. For example, let's assume a script calls a function Some scripts also use logic that runs in the background (such as Due to this, preferring async calls over sync can be seen a practice we might want to encourage. ShortcutsThat being said, I do agree with you that one of the design goals of the Retrieving stdoutAlso, I agree that retrieving Execa does provide with a convenient Also, Bash provides with built-in syntax for this with So, I think there are quite a few pros and cons to consider. @sindresorhus What do you think about this? |
You said "wrapping". I presented aliasing, which is not the same thing. |
I'm warming up to the idea, but I will need some time to think about it. If we do this, we would only do |
These are the possible alternatives: s`dep deploy`;
_`dep deploy`;
$_`dep deploy`;
_$`dep deploy`;
$$`dep deploy`; |
One alternative is this, but it also makes scripts using import {$} from 'execa/sync';
const branch = $`git branch --show-current`.stdout;
if (branch === "main") {
// Do something.
} |
Thank you ehmicky for the detailed and thoughtful reply! I agree with you that there are pros and cons.
Yeah, I don't think that approach is very good, because it prevents using both sync and async at the same time. I have found that in my scripts, I want to use both. (e.g. I want to use sync if I am at the beginning of the script for "prep" work, and I want to use async at the end of the script for tasks that are not order-dependent.) |
Yes, it seems that the only two symbols that are allowed in JavaScript/TypeScript as functions are I think using Another alternative you didn't mention is: s$`dep deploy`
$s`dep deploy` Personally, I think is a bit more clear than packing on more symbols. However, I'll note that I still prefer a one-letter solution like By the way, I think that if we all converge on s`npx tsc`
a`npx eslint .`
a`npx prettier .` versus: s`npx tsc`
$`npx eslint .`
$`npx prettier .` Here, I think the former snippet with the two different letters is more easily understandable. |
I don't think that's needed. The s`npx tsc`
await $`npx eslint .`
await $`npx prettier .` I don't really want to export more aliases. |
One problem with |
I like |
I also think I also agree that import {$} from 'execa';
import {$ as $s} from 'execa/sync'; |
Ok. Let's go with |
I'm a little late to the party, but curious what folks think about just including a shorter alias on e.g. Would make the code updates trivial and not require adding a new entry point e.g.
Also, |
Thanks for chiming in @aaronccasanova. For memories, @aaronccasanova suggested and implemented the I would personally favor Another question would be: should |
I'm in favor of |
I would keep |
Done in #594. |
One thing to consider for anyone using Overall, it seems to me that synchronous invocations are useful only in very specific contexts (such as in a Overall, we probably should discourage using |
This feature has been just released in Execa 9.0.0. Please see (and share!) the release post and the changelog. |
Hello, and thanks for the library.
I love the
$.sync
command, but it is too long. I want to make scripts that are nice and short.e.g.
I propose a default alias for
$.sync
ass
.I know that I can do this for my own projects by wrapping
$.sync
and exporting it, but I think this feature should be for all users!The text was updated successfully, but these errors were encountered: