mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-07-02 17:08:39 +00:00
feat: add @shadcn/ui cli (#112)
This commit is contained in:
34
packages/cli/src/utils/get-components.ts
Normal file
34
packages/cli/src/utils/get-components.ts
Normal 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")
|
||||
}
|
||||
}
|
||||
9
packages/cli/src/utils/get-package-info.ts
Normal file
9
packages/cli/src/utils/get-package-info.ts
Normal 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
|
||||
}
|
||||
17
packages/cli/src/utils/get-package-manager.ts
Normal file
17
packages/cli/src/utils/get-package-manager.ts
Normal 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"
|
||||
}
|
||||
16
packages/cli/src/utils/logger.ts
Normal file
16
packages/cli/src/utils/logger.ts
Normal 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))
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user