Home >Web Front-end >JS Tutorial >Execa vs using child_process to execute CLI commands programmatically.

Execa vs using child_process to execute CLI commands programmatically.

Linda Hamilton
Linda HamiltonOriginal
2024-12-01 00:17:12805browse

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.

Execa vs using child_process to execute CLI commands programmatically.

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 vs using child_process to execute CLI commands programmatically.

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

Execa vs using child_process to execute CLI commands programmatically.

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

Execa vs using child_process to execute CLI commands programmatically.

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.

Execa vs using child_process to execute CLI commands programmatically.

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:shadcn-ui/ui execa&type=code

The above is the detailed content of Execa vs using child_process to execute CLI commands programmatically.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn