DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Edited on • Originally published at thinkthroo.com

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]})
}
Enter fullscreen mode Exit fullscreen mode

Just to provide a bit more context, this run gets called inside a function called install in CodeMirror source code.

Image description

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
Enter fullscreen mode Exit fullscreen mode

install is parsed using process.argv

Image description

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

Image description

Check out these search results for Execa usage in Shadcn/ui repository

Image description

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

  1. https://www.npmjs.com/package/execa

  2. https://nodejs.org/api/child_process.html#child_processexecfilesyncfile-args-options

  3. https://nodejs.org/api/child_process.html#child_processexecfilefile-args-options-callback

  4. https://github.com/codemirror/dev/blob/main/bin/cm.js#L14

  5. https://github.com/codemirror/dev?tab=readme-ov-file#codemirror

  6. https://github.com/search?q=repo%3Ashadcn-ui%2Fui%20execa&type=code

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Retry later