Skip to content

Commit 7b2d00e

Browse files
committed
fix: build an empty schema when trying to scan a nonexistent directory
euberdeveloper/dree#51
1 parent 264ac6a commit 7b2d00e

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

packages/library/src/server/dirtree/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,37 @@ describe("dirtree", () => {
159159
export type MyTestDirectoryFile = z.infer<typeof testDirectoryFiles>"
160160
`)
161161
})
162+
163+
it("creates an empty schema when the directory cannot be read", async () => {
164+
const tree = getDirectoryTree("nonexistent")
165+
const result = await buildSchemaForDirectoryTree(tree, "MyDirectoryTree")
166+
expect(result).toMatchInlineSnapshot(`
167+
"
168+
// Note: This file is autogenerated. Do not edit it directly.
169+
//
170+
// Describes the contents of the test directory, which is a blueprint for
171+
// files and directories. Tests can create a unique, safe environment for
172+
// interacting with the contents of such a directory.
173+
//
174+
// Having strong typing for the test directory contents ensures that tests can
175+
// be written with confidence that the files and directories they expect are
176+
// actually found. Otherwise the tests are brittle and can break easily.
177+
178+
import { z } from "zod"
179+
180+
export const MyDirectoryTreeSchema = z.object({
181+
type: z.literal("directory"),
182+
name: z.literal("root"),
183+
contents: z.object({}),
184+
})
185+
186+
export const MyDirectoryTreeContentsSchema = MyDirectoryTreeSchema.shape.contents
187+
export type MyDirectoryTreeContentsSchemaType = z.infer<typeof MyDirectoryTreeSchema>
188+
189+
export type MyDirectoryTree = MyDirectoryTreeContentsSchemaType["contents"]
190+
191+
export const testDirectoryFiles = z.enum([])
192+
export type MyTestDirectoryFile = z.infer<typeof testDirectoryFiles>"
193+
`)
194+
})
162195
})

packages/library/src/server/dirtree/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { format, resolveConfig } from "prettier"
55
import { fileURLToPath } from "url"
66
import { jsonToZod } from "./json-to-zod.js"
77

8-
type TreeResult = { dree: Dree; allFiles: Dree[] }
8+
type TreeResult = { dree: Dree | undefined; allFiles: Dree[] }
99

1010
/** Convert a directory tree to a TypeScript type. This is useful for testing
1111
* as the initial state of the test directory is fully known in tests. */
@@ -25,9 +25,9 @@ export function getDirectoryTree(path: string): TreeResult {
2525
dir => {
2626
allFiles.push(dir)
2727
}
28-
)
28+
) as Dree | null // https://github.com/euberdeveloper/dree/pull/51
2929

30-
return { dree: result, allFiles }
30+
return { dree: result ?? undefined, allFiles }
3131
}
3232

3333
type FileNode = {
@@ -63,10 +63,15 @@ export function convertDree(root: Dree): TreeNode {
6363
}
6464

6565
export async function buildSchemaForDirectoryTree(result: TreeResult, name: string): Promise<string> {
66-
const root = result.dree
67-
assert(root.type === Type.DIRECTORY)
68-
const node = convertDree(root)
69-
const schema = (await jsonToZod(node, `${name}Schema`)).split("\n")
66+
let root: TreeNode
67+
if (result.dree) {
68+
assert(result.dree.type === Type.DIRECTORY)
69+
root = convertDree(result.dree)
70+
} else {
71+
// directory does not exist, or some other problem scanning it
72+
root = { type: Type.DIRECTORY, name: "root", contents: {} }
73+
}
74+
const schema = (await jsonToZod(root, `${name}Schema`)).split("\n")
7075

7176
const lines = `
7277
// Note: This file is autogenerated. Do not edit it directly.

0 commit comments

Comments
 (0)