From 7b2d00eb409cefc703ffdaa57eda87fac7f31573 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sat, 15 Feb 2025 18:01:44 +0200 Subject: [PATCH 1/2] fix: build an empty schema when trying to scan a nonexistent directory https://github.com/euberdeveloper/dree/pull/51 --- .../library/src/server/dirtree/index.test.ts | 33 +++++++++++++++++++ packages/library/src/server/dirtree/index.ts | 19 +++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/packages/library/src/server/dirtree/index.test.ts b/packages/library/src/server/dirtree/index.test.ts index 0300a037..27ed1ad8 100644 --- a/packages/library/src/server/dirtree/index.test.ts +++ b/packages/library/src/server/dirtree/index.test.ts @@ -159,4 +159,37 @@ describe("dirtree", () => { export type MyTestDirectoryFile = z.infer" `) }) + + it("creates an empty schema when the directory cannot be read", async () => { + const tree = getDirectoryTree("nonexistent") + const result = await buildSchemaForDirectoryTree(tree, "MyDirectoryTree") + expect(result).toMatchInlineSnapshot(` + " + // Note: This file is autogenerated. Do not edit it directly. + // + // Describes the contents of the test directory, which is a blueprint for + // files and directories. Tests can create a unique, safe environment for + // interacting with the contents of such a directory. + // + // Having strong typing for the test directory contents ensures that tests can + // be written with confidence that the files and directories they expect are + // actually found. Otherwise the tests are brittle and can break easily. + + import { z } from "zod" + + export const MyDirectoryTreeSchema = z.object({ + type: z.literal("directory"), + name: z.literal("root"), + contents: z.object({}), + }) + + export const MyDirectoryTreeContentsSchema = MyDirectoryTreeSchema.shape.contents + export type MyDirectoryTreeContentsSchemaType = z.infer + + export type MyDirectoryTree = MyDirectoryTreeContentsSchemaType["contents"] + + export const testDirectoryFiles = z.enum([]) + export type MyTestDirectoryFile = z.infer" + `) + }) }) diff --git a/packages/library/src/server/dirtree/index.ts b/packages/library/src/server/dirtree/index.ts index 600aeddd..4a75c6fe 100644 --- a/packages/library/src/server/dirtree/index.ts +++ b/packages/library/src/server/dirtree/index.ts @@ -5,7 +5,7 @@ import { format, resolveConfig } from "prettier" import { fileURLToPath } from "url" import { jsonToZod } from "./json-to-zod.js" -type TreeResult = { dree: Dree; allFiles: Dree[] } +type TreeResult = { dree: Dree | undefined; allFiles: Dree[] } /** Convert a directory tree to a TypeScript type. This is useful for testing * as the initial state of the test directory is fully known in tests. */ @@ -25,9 +25,9 @@ export function getDirectoryTree(path: string): TreeResult { dir => { allFiles.push(dir) } - ) + ) as Dree | null // https://github.com/euberdeveloper/dree/pull/51 - return { dree: result, allFiles } + return { dree: result ?? undefined, allFiles } } type FileNode = { @@ -63,10 +63,15 @@ export function convertDree(root: Dree): TreeNode { } export async function buildSchemaForDirectoryTree(result: TreeResult, name: string): Promise { - const root = result.dree - assert(root.type === Type.DIRECTORY) - const node = convertDree(root) - const schema = (await jsonToZod(node, `${name}Schema`)).split("\n") + let root: TreeNode + if (result.dree) { + assert(result.dree.type === Type.DIRECTORY) + root = convertDree(result.dree) + } else { + // directory does not exist, or some other problem scanning it + root = { type: Type.DIRECTORY, name: "root", contents: {} } + } + const schema = (await jsonToZod(root, `${name}Schema`)).split("\n") const lines = ` // Note: This file is autogenerated. Do not edit it directly. From d8819e45ca5bbe8457d684b60b94e3b6c44602f0 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sat, 15 Feb 2025 18:17:16 +0200 Subject: [PATCH 2/2] fixup! fix: build an empty schema when trying to scan a nonexistent directory --- packages/library/src/server/dirtree/index.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/library/src/server/dirtree/index.ts b/packages/library/src/server/dirtree/index.ts index 4a75c6fe..f0e0c7c4 100644 --- a/packages/library/src/server/dirtree/index.ts +++ b/packages/library/src/server/dirtree/index.ts @@ -1,4 +1,3 @@ -import assert from "assert" import type { Dree } from "dree" import { scan, Type } from "dree" import { format, resolveConfig } from "prettier" @@ -42,7 +41,11 @@ type DirectoryNode = { type TreeNode = FileNode | DirectoryNode -export function convertDree(root: Dree): TreeNode { +export function convertDree(root: Dree | undefined): TreeNode { + if (!root) { + return { type: Type.DIRECTORY, name: "root", contents: {} } + } + if (root.type === Type.FILE) { return { name: root.name, @@ -63,14 +66,8 @@ export function convertDree(root: Dree): TreeNode { } export async function buildSchemaForDirectoryTree(result: TreeResult, name: string): Promise { - let root: TreeNode - if (result.dree) { - assert(result.dree.type === Type.DIRECTORY) - root = convertDree(result.dree) - } else { - // directory does not exist, or some other problem scanning it - root = { type: Type.DIRECTORY, name: "root", contents: {} } - } + const root = convertDree(result.dree) + const schema = (await jsonToZod(root, `${name}Schema`)).split("\n") const lines = `