Logo

dev-resources.site

for different kinds of informations.

shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 1.1

Published at
6/25/2024
Categories
javascript
opensource
nextjs
shadcnui
Author
ramunarasinga
Author
13 person written this
ramunarasinga
open
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 1.1

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 1.1, we will look at a code snippet picked from packages/cli/src/index.ts.

In part 1.0, I discussed the imports used and getPackageInfo utility function. Let’s continue where we left off in part 1.0 by picking a code snippet that is in continuation to part 1.0

The below code snippet is picked from ui/packages/cli/src/index.ts

async function main() {
  const packageInfo = await getPackageInfo()

  const program = new Command()
    .name("shadcn-ui")
    .description("add components and dependencies to your project")
    .version(
      packageInfo.version || "1.0.0",
      "-v, --version",
      "display the version number"
    )

  program.addCommand(init).addCommand(add).addCommand(diff)

  program.parse()
}

main()
Enter fullscreen mode Exit fullscreen mode

new Command()

Commander package is used and is configured with name, description and version.

program.addCommand

After a new command is initialised, init, add and diff commands are added to the program variable using addCommand. You can specify (sub)commands using .command() or .addCommand(). There are two ways these can be implemented: using an action handler attached to the command, or as a stand-alone executable file.

program.parse

Parse function should be called with no parameters to parse process.argv via the CLI.

Conclusion:

In this article, I discussed a code snippet that deals with initialising command and adds custom commands such as init, add and diff that are used in shadcn-ui/ui CLI. I found out that program.parse() call with no arguments is important to parse the process.argv passed in via your CLI.

Get free courses inspired by the best practices used in open source.

About me:

Website: https://ramunarasinga.com/

Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/

Github: https://github.com/Ramu-Narasinga

Email: [email protected]

Learn the best practices used in open source.

References:

  1. https://www.npmjs.com/package/commander#parse-and-parseasync
  2. https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/index.ts
shadcnui Article's
30 articles in total
Favicon
Color Highlighting for Tailwind CSS Variables in VS Code
Favicon
Shadcn UI Theme generator, with OKLCH colors and ancient sacred geometry.
Favicon
Next.js Starter Kit: Build Fast & Secure Apps with Auth, Payments & More!
Favicon
Comparing the copyToClipboard implementations in Shadcn-ui/ui and Codehike.
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 3.1
Favicon
Comparison of file and component structures among Shadcn-ui, Plane.so and Gitroom.
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 3.0
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.15
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.14
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.13
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.12
Favicon
Create File Upload UI in Next.js with Shadcn UI
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.11
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.10
Favicon
How to Use Icons in Shadcn UI with Next.js
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.9
Favicon
Next.js with Shadcn UI Progress Bar Example
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.7
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.5
Favicon
How to Use Scroll Area in Next.js with Shadcn UI
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.4
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.3
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.2
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.0
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.1
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 1.1
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 1.0
Favicon
shadcn-ui/ui codebase analysis: How is β€œBlocks” page built β€” Part 5
Favicon
Next.js Image File Upload and Preview with shadcn/ui
Favicon
shadcn-ui/ui codebase analysis: How is β€œBlocks” page built β€” Part 3

Featured ones: