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: sync method #681
Comments
It's a valid concern, and there can be scenarios where having a synchronous version of a command execution library like If you'd like to contribute by adding a synchronous version, here's how you might approach it in JavaScript/TypeScript. Note that this is a simplified example, and you should consider adding error handling and handling more complex use cases based on your specific needs: import shell from 'shelljs';
/**
* Synchronously checks if a command is available.
* @param {string} bin - The command to check.
* @returns {boolean} - True if the command is available; otherwise, false.
*/
export function isAvailableSync(bin) {
const result = shell.exec(`command -v ${bin}`, { silent: true });
return result.code === 0;
}
// Usage
if (isAvailableSync('ffmpeg')) {
// ...
} In this example, we use the Remember that synchronous code execution can block the event loop, so use this approach cautiously and only when necessary for specific use cases where synchronous execution is more appropriate. |
I'd also like to echo the call for some sync methods. One of the situations where it would have been useful is in a module which does a tiny bit of processing when it first initialises, along these lines:
The program as a whole isn't run through zx, so top-level Requiring everything to be async sometimes introduces complexities like this that are solvable, but are just...annoying...when there's no benefit. |
I understand that a sync version of
$
might not provide the same flexibility than the current async version, for example printing the ouput of a long running command before it ends.But it might be beneficial for some short running commands that we usually wrap into a utility function, which do not blend in a regular control flow, where
await
everywhere becomes a pain.An example:
Execa, who took inspiration from zx for its own
$
utility, provides a sync method: https://github.com/sindresorhus/execa/tree/main#synccommandThe text was updated successfully, but these errors were encountered: