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 me:
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.
I am open to work on an interesting project. Send me an email at ramu.narasinga@gmail.com
My Github - https://github.com/ramu-narasinga
My website - https://ramunarasinga.com
My Youtube channel - https://www.youtube.com/@thinkthroo
Learning platform - https://thinkthroo.com
Codebase Architecture - https://app.thinkthroo.com/architecture
Best practices - https://app.thinkthroo.com/best-practices
Production-grade projects - https://app.thinkthroo.com/production-grade-projects
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
Top comments (0)