Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-app-index-missing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": patch
---

Skip the app index update when `app/page.tsx` is missing instead of throwing `ENOENT`.
90 changes: 90 additions & 0 deletions packages/shadcn/src/utils/update-app-index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import os from "os"
import path from "path"
import { getRegistryItems } from "@/src/registry/api"
import { Config } from "@/src/utils/get-config"
import fs from "fs-extra"
import { afterEach, describe, expect, it, vi } from "vitest"

import { updateAppIndex } from "./update-app-index"

vi.mock("@/src/registry/api", () => ({
getRegistryItems: vi.fn(),
}))

const tempDirs: string[] = []

async function createTempCwd() {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "shadcn-app-index-"))
tempDirs.push(dir)

return dir
}

function createConfig(cwd: string) {
return { resolvedPaths: { cwd } } as Config
}

afterEach(async () => {
vi.clearAllMocks()
await Promise.all(tempDirs.splice(0).map((dir) => fs.remove(dir)))
})

describe("updateAppIndex", () => {
it("should not throw when app/page.tsx does not exist", async () => {
const cwd = await createTempCwd()

await expect(
updateAppIndex("component", createConfig(cwd))
).resolves.toBeUndefined()
expect(getRegistryItems).not.toHaveBeenCalled()
})

it("should not throw when app/page.tsx is a directory", async () => {
const cwd = await createTempCwd()
await fs.ensureDir(path.join(cwd, "app/page.tsx"))

await expect(
updateAppIndex("component", createConfig(cwd))
).resolves.toBeUndefined()
expect(getRegistryItems).not.toHaveBeenCalled()
})

it("should overwrite app/page.tsx with the component import", async () => {
const cwd = await createTempCwd()
const indexPath = path.join(cwd, "app/page.tsx")
await fs.outputFile(indexPath, "export default function Page() {}\n")

vi.mocked(getRegistryItems).mockResolvedValue([
{
name: "component",
type: "registry:component",
meta: {
importSpecifier: "ComponentExample",
moduleSpecifier: "@/components/component-example",
},
},
] as Awaited<ReturnType<typeof getRegistryItems>>)

await updateAppIndex("component", createConfig(cwd))

expect(await fs.readFile(indexPath, "utf8")).toBe(
'import { ComponentExample } from "@/components/component-example"\n\nexport default function Page() {\n return <ComponentExample />\n}'
)
})

it("should leave app/page.tsx untouched when the item has no meta", async () => {
const cwd = await createTempCwd()
const indexPath = path.join(cwd, "app/page.tsx")
await fs.outputFile(indexPath, "export default function Page() {}\n")

vi.mocked(getRegistryItems).mockResolvedValue([
{ name: "component", type: "registry:component" },
] as Awaited<ReturnType<typeof getRegistryItems>>)

await updateAppIndex("component", createConfig(cwd))

expect(await fs.readFile(indexPath, "utf8")).toBe(
"export default function Page() {}\n"
)
})
})
4 changes: 3 additions & 1 deletion packages/shadcn/src/utils/update-app-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { Config } from "@/src/utils/get-config"
export async function updateAppIndex(component: string, config: Config) {
const indexPath = path.join(config.resolvedPaths.cwd, "app/page.tsx")

if (!(await fs.stat(indexPath)).isFile()) {
const stat = await fs.stat(indexPath).catch(() => null)

if (!stat?.isFile()) {
return
}

Expand Down
Loading