


shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? — Part 5
I wanted to find out how shadcn-ui CLI works. In this article, I discuss the code used to build the shadcn-ui/ui CLI.
In part 2.11, we looked at runInit function and how shadcn-ui/ui ensures directories provided in resolvedPaths in config exist.
The following operations are performed in runInit function:
- Ensure all resolved paths directories exist.
- Write tailwind config.
- Write css file.
- Write cn file.
- Install dependencies.
1, 2, 3, 4 from the above are covered in part 2.12, 2.13 and 2.14, let’s find out how “Install dependencies” operation is done.
Install dependencies
The below code snippet is picked from cli/src/commands/init.ts
// Install dependencies. const dependenciesSpinner = ora(\`Installing dependencies...\`)?.start() const packageManager = await getPackageManager(cwd) // TODO: add support for other icon libraries. const deps = \[ ...PROJECT\_DEPENDENCIES, config.style === "new-york" ? "@radix-ui/react-icons" : "lucide-react", \] await execa( packageManager, \[packageManager === "npm" ? "install" : "add", ...deps\], { cwd, } ) dependenciesSpinner?.succeed()
ora is an elegant terminal spinner and is used to show the message “Installing dependencies…” when you run the npx shadcn init command.
getPackageManager
getPackageManager is imported packages/cli/src/utils/get-package-manager.ts
import { detect } from "@antfu/ni" export async function getPackageManager( targetDir: string ): Promise<"yarn" | "pnpm" | "bun" | "npm"> { const packageManager = await detect({ programmatic: true, cwd: targetDir }) if (packageManager === "yarn@berry") return "yarn" if (packageManager === "pnpm@6") return "pnpm" if (packageManager === "bun") return "bun" return packageManager ?? "npm" }
Have you ever used npm in a pnpm project? I have and often times, it fails to install the package because you are using npm in a pnpm project.
@antfu/ni lets you use the right package manager and detect is a function that gives the packageManager used in a given project based on cwd.
I cannot find anything mentioned about detect method in the @antfu/ni Github readme. How would you know such a method exists unless you read it in some open source codebase?
execa
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. This is built by the legend, Sindre Sorhus
shadcn-ui/ui CLI uses execa to install the neccessary dependencies as part of npx shadcn-ui init command.
Conclusion
shadcn-ui CLI uses execa, built by the legend, Sindre Sorhu. Execa is used to install the necessary dependencies in a script file. We all are familiar with executing installation commands but if you want to install some packages in a script programatically, execa can be used.
shadcn-ui CLI detects the package manager used in your project using “detect” method from @antfu/ni.
This article marks my quest to study and learn what happens under the hood when you run npx shadcn-ui init command as completed.
I am moving on to study the add command.
Want to learn how to build shadcn-ui/ui from scratch? Check out build-from-scratch
About me:
Website: https://ramunarasinga.com/
Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/
Github: https://github.com/Ramu-Narasinga
Email: ramu.narasinga@gmail.com
Build shadcn-ui/ui from scratch
References:
- https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/commands/init.ts#L382
- https://www.npmjs.com/package/ora
- https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/get-package-manager.ts#L3
- https://www.npmjs.com/package/@antfu/ni
- https://github.com/antfu-collective/ni#readme
- https://github.com/search?q=import+%7B+detect+%7D+from+%22%40antfu%2Fni%22&type=code
- https://www.npmjs.com/package/execa
- https://github.com/sindresorhus
The above is the detailed content of shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? — Part 5. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
