feat: add @shadcn/ui cli (#112)

This commit is contained in:
shadcn
2023-03-08 11:37:15 +04:00
committed by GitHub
parent b043cf7f8c
commit be701cf139
28 changed files with 2833 additions and 70 deletions

View File

@@ -0,0 +1,34 @@
import fetch from "node-fetch"
import * as z from "zod"
const baseUrl =
process.env.NODE_ENV === "production"
? "https://ui.shadcn.com"
: "http://localhost:3000"
const componentSchema = z.object({
name: z.string(),
dependencies: z.array(z.string()).optional(),
files: z.array(
z.object({
name: z.string(),
dir: z.string(),
content: z.string(),
})
),
})
export type Component = z.infer<typeof componentSchema>
const componentsSchema = z.array(componentSchema)
export async function getAvailableComponents() {
try {
const response = await fetch(`${baseUrl}/api/components`)
const components = await response.json()
return componentsSchema.parse(components)
} catch (error) {
throw new Error("Failed to fetch components")
}
}

View File

@@ -0,0 +1,9 @@
import path from "path"
import fs from "fs-extra"
import { type PackageJson } from "type-fest"
export function getPackageInfo() {
const packageJsonPath = path.join("package.json")
return fs.readJSONSync(packageJsonPath) as PackageJson
}

View File

@@ -0,0 +1,17 @@
export function getPackageManager() {
const userAgent = process.env.npm_config_user_agent
if (!userAgent) {
return "npm"
}
if (userAgent.startsWith("yarn")) {
return "yarn"
}
if (userAgent.startsWith("pnpm")) {
return "pnpm"
}
return "npm"
}

View File

@@ -0,0 +1,16 @@
import chalk from "chalk"
export const logger = {
error(...args: unknown[]) {
console.log(chalk.red(...args))
},
warn(...args: unknown[]) {
console.log(chalk.yellow(...args))
},
info(...args: unknown[]) {
console.log(chalk.cyan(...args))
},
success(...args: unknown[]) {
console.log(chalk.green(...args))
},
}