Execa vs using child_process to execute CLI commands programmatically.
In this article, we analyse two ways to execute CLI commands programmatically found in Shadcn/ui and CodeMirror.
Have you ever wondered if it is possible to run CLI commands programmatically? We are going to look at how Shadcn/ui uses execa and how CodeMirror uses child_process to execute CLI programmaticaly.
child_process usage in CodeMirror
CodeMirror is found to be using a Node.js API called child_process.execFileSync
. Read more
function run(cmd, args, wd = root, { shell = false } = {}) {
return child.execFileSync(cmd, args, {shell, cwd: wd, encoding: "utf8", stdio: ["ignore", "pipe", process.stderr]})
}
Just to provide a bit more context, this run gets called inside a function called install in CodeMirror source code.
Check the CodeMirror v6 Readme to understand what this install is for.
One of the first steps to set up the development environment for CodeMirror is to execute the below command, this is mentioned in CodeMirror’s Readme.md.
node bin/cm.js install
install is parsed using process.argv
Execa usage in Shadcn/ui
Execa runs commands in your script, application or library. Unlike shells, it is optimized for programmatic usage. Built on top of the child_process
core module.
Shadcn/ui’s add command is found to be execa to install packages
Check out these search results for Execa usage in Shadcn/ui repository
About us:
At Thinkthroo, we study large open source projects and provide architectural guides. We have developed reusable Components, built with tailwind, that you can use in your project. We offer Next.js, React and Node development services.
Book a meeting with us to discuss your project.
References
-
https://nodejs.org/api/child_process.html#child_processexecfilesyncfile-args-options
-
https://nodejs.org/api/child_process.html#child_processexecfilefile-args-options-callback
-
https://github.com/codemirror/dev?tab=readme-ov-file#codemirror
-
https://github.com/search?q=repo%3Ashadcn-ui%2Fui%20execa&type=code