feat: fix preset handling and templates

This commit is contained in:
shadcn
2026-02-10 10:34:38 +04:00
parent ab54e7b7bd
commit d602ccc224
2 changed files with 68 additions and 5 deletions

View File

@@ -234,7 +234,7 @@ export const init = new Command()
value: "create",
},
{
title: "No. Use defaults.",
title: "No. Continue without a preset.",
value: "defaults",
},
],
@@ -252,10 +252,7 @@ export const init = new Command()
process.exit(0)
}
// User chose defaults — continue with default init flow.
opts.defaults = true
opts.template = opts.template || "next"
opts.baseColor = opts.baseColor || "neutral"
// User chose no preset — continue with normal init flow.
}
const options = initOptionsSchema.parse({

View File

@@ -177,4 +177,70 @@ describe("handlePresetOption", () => {
expect(result).toBeNull()
})
it("should exit with error when preset name is empty string", async () => {
vi.mocked(getPreset).mockResolvedValue(null)
mockExit = vi
.spyOn(process, "exit")
.mockImplementation(() => undefined as never)
await handlePresetOption("", false)
expect(mockExit).toHaveBeenCalledWith(1)
})
it("should list available presets in error when not found", async () => {
const { logger } = await import("@/src/utils/logger")
const secondPreset = { ...mockPreset, name: "minimal", title: "Minimal" }
vi.mocked(getPreset).mockResolvedValue(null)
vi.mocked(getPresets).mockResolvedValue([mockPreset, secondPreset])
mockExit = vi
.spyOn(process, "exit")
.mockImplementation(() => undefined as never)
await handlePresetOption("nonexistent", false)
expect(logger.error).toHaveBeenCalledWith(
expect.stringContaining("default, minimal")
)
})
it("should pass rtl to create url when custom is selected", async () => {
vi.mocked(prompts).mockResolvedValue({ selectedPreset: "custom" })
await handlePresetOption(true, true)
expect(open).toHaveBeenCalledWith(expect.stringContaining("rtl=true"))
})
it("should select correct preset from multiple options", async () => {
const secondPreset = {
...mockPreset,
name: "minimal",
title: "Minimal",
baseColor: "zinc",
}
vi.mocked(getPresets).mockResolvedValue([mockPreset, secondPreset])
vi.mocked(prompts).mockResolvedValue({ selectedPreset: "minimal" })
const result = await handlePresetOption(true, false)
expect(result).toEqual(secondPreset)
})
it("should propagate error when getPresets fails", async () => {
vi.mocked(getPresets).mockRejectedValue(new Error("Network error"))
await expect(handlePresetOption(true, false)).rejects.toThrow(
"Network error"
)
})
it("should propagate error when getPreset fails", async () => {
vi.mocked(getPreset).mockRejectedValue(new Error("Network error"))
await expect(handlePresetOption("default", false)).rejects.toThrow(
"Network error"
)
})
})