Skip to content

Commit

Permalink
feat(shadcn): add new registry:file type (#6377)
Browse files Browse the repository at this point in the history
* feat(shadcn): add new registry:file type

* chore: add changeset

* fix: file target
  • Loading branch information
shadcn authored Jan 16, 2025
1 parent f07c7ad commit 5f7957a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-eyes-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": minor
---

new registry:file type
2 changes: 1 addition & 1 deletion apps/www/lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function getFileTarget(file: z.infer<typeof registryItemFileSchema>) {
}
}

return target
return target ?? ""
}

async function createTempSourceFile(filename: string) {
Expand Down
3 changes: 2 additions & 1 deletion apps/www/public/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"registry:ui",
"registry:hook",
"registry:theme",
"registry:page"
"registry:page",
"registry:file"
]
},
"description": {
Expand Down
20 changes: 17 additions & 3 deletions apps/www/public/schema/registry-item.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"registry:ui",
"registry:hook",
"registry:theme",
"registry:page"
"registry:page",
"registry:file"
]
},
"description": {
Expand Down Expand Up @@ -64,14 +65,27 @@
"registry:ui",
"registry:hook",
"registry:theme",
"registry:page"
"registry:page",
"registry:file"
]
},
"target": {
"type": "string"
}
},
"required": ["path", "type"]
"if": {
"properties": {
"type": {
"enum": ["registry:file", "registry:page"]
}
}
},
"then": {
"required": ["path", "type", "target"]
},
"else": {
"required": ["path", "type"]
}
}
},
"tailwind": {
Expand Down
24 changes: 17 additions & 7 deletions packages/shadcn/src/registry/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@ export const registryItemTypeSchema = z.enum([
"registry:component",
"registry:ui",
"registry:hook",
"registry:theme",
"registry:page",
"registry:file",

// Internal use only
"registry:theme",
"registry:example",
"registry:style",
"registry:internal",
])

export const registryItemFileSchema = z.object({
path: z.string(),
content: z.string().optional(),
type: registryItemTypeSchema,
target: z.string().optional(),
})
export const registryItemFileSchema = z.discriminatedUnion("type", [
// Target is required for registry:file and registry:page
z.object({
path: z.string(),
content: z.string().optional(),
type: z.enum(["registry:file", "registry:page"]),
target: z.string(),
}),
z.object({
path: z.string(),
content: z.string().optional(),
type: registryItemTypeSchema.exclude(["registry:file", "registry:page"]),
target: z.string().optional(),
}),
])

export const registryItemTailwindSchema = z.object({
config: z
Expand Down
22 changes: 21 additions & 1 deletion packages/shadcn/test/utils/updaters/update-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("resolveFilePath", () => {
description: "should NOT use src directory for root files",
file: {
path: "hello-world/.env",
type: "registry:lib",
type: "registry:file",
target: "~/.env",
},
resolvedPath: "/foo/bar/.env",
Expand Down Expand Up @@ -347,6 +347,26 @@ describe("resolveFilePath", () => {
).toBe("/foo/bar/src/hooks/use-foo.ts")
})

test("should resolve registry:file file types", () => {
expect(
resolveFilePath(
{
path: "hello-world/.env",
type: "registry:file",
target: "~/baz/.env",
},
{
resolvedPaths: {
cwd: "/foo/bar",
},
},
{
isSrcDir: false,
}
)
).toBe("/foo/bar/baz/.env")
})

test("should resolve nested files", () => {
expect(
resolveFilePath(
Expand Down

0 comments on commit 5f7957a

Please sign in to comment.