diff --git a/packages/shadcn/src/registry/schema.ts b/packages/shadcn/src/registry/schema.ts index 01fafada9cc..30f9608bd94 100644 --- a/packages/shadcn/src/registry/schema.ts +++ b/packages/shadcn/src/registry/schema.ts @@ -36,6 +36,8 @@ export const registryItemFileSchema = z.discriminatedUnion("type", [ }), ]) +export type RegistryItemFile = z.infer + export const registryItemTailwindSchema = z.object({ config: z .object({ diff --git a/packages/shadcn/src/registry/utils.ts b/packages/shadcn/src/registry/utils.ts index 84fd4cdc62c..187006222f9 100644 --- a/packages/shadcn/src/registry/utils.ts +++ b/packages/shadcn/src/registry/utils.ts @@ -8,6 +8,8 @@ import { } from "@/src/schema" import { Config } from "@/src/utils/get-config" import { ProjectInfo, getProjectInfo } from "@/src/utils/get-project-info" +import { createRegistryFile } from "@/src/utils/registry/create-registry-file" +import { determineFileType } from "@/src/utils/registry/determine-file-type" import { resolveImport } from "@/src/utils/resolve-import" import { findCommonRoot, @@ -106,11 +108,7 @@ export async function recursivelyResolveFileImports( // Add the original file first const fileType = determineFileType(filePath) - const originalFile = { - path: relativeRegistryFilePath, - type: fileType, - target: "", - } + const originalFile = createRegistryFile(relativeRegistryFilePath, fileType) files.push(originalFile) // 1. Find all import statements in the file. @@ -227,30 +225,6 @@ async function createTempSourceFile(filename: string) { return path.join(dir, filename) } -// This is a bit tricky to accurately determine. -// For now we'll use the module specifier to determine the type. -function determineFileType( - moduleSpecifier: string -): z.infer["type"] { - if (moduleSpecifier.includes("/ui/")) { - return "registry:ui" - } - - if (moduleSpecifier.includes("/lib/")) { - return "registry:lib" - } - - if (moduleSpecifier.includes("/hooks/")) { - return "registry:hook" - } - - if (moduleSpecifier.includes("/components/")) { - return "registry:component" - } - - return "registry:component" -} - // Additional utility functions for local file support export function isUrl(path: string) { try { diff --git a/packages/shadcn/src/utils/registry/create-registry-file.ts b/packages/shadcn/src/utils/registry/create-registry-file.ts new file mode 100644 index 00000000000..33b4b1740f9 --- /dev/null +++ b/packages/shadcn/src/utils/registry/create-registry-file.ts @@ -0,0 +1,16 @@ +import { RegistryItemFile } from "@/src/schema" + +const createRegistryFile = ( + filePath: string, + fileType: RegistryItemFile["type"], + target: string = "" +): RegistryItemFile => { + // + return { + path: filePath, + type: fileType, + target: target, + } +} + +export { createRegistryFile } diff --git a/packages/shadcn/src/utils/registry/determine-file-type.ts b/packages/shadcn/src/utils/registry/determine-file-type.ts new file mode 100644 index 00000000000..021c4e0d9ee --- /dev/null +++ b/packages/shadcn/src/utils/registry/determine-file-type.ts @@ -0,0 +1,25 @@ +import type { RegistryItem } from "@/src/registry/schema" + +// This is a bit tricky to accurately determine. +// For now we'll use the module specifier to determine the type. +const determineFileType = (moduleSpecifier: string): RegistryItem["type"] => { + if (moduleSpecifier.includes("/ui/")) { + return "registry:ui" + } + + if (moduleSpecifier.includes("/lib/")) { + return "registry:lib" + } + + if (moduleSpecifier.includes("/hooks/")) { + return "registry:hook" + } + + if (moduleSpecifier.includes("/components/")) { + return "registry:component" + } + + return "registry:component" +} + +export { determineFileType }