|
| 1 | +import { readFile } from "node:fs/promises" |
| 2 | +import { join } from "node:path" |
| 3 | +import { DocClient } from "../lib/doc-client" |
| 4 | +import { DocFileSystem } from "../lib/doc-file-system" |
| 5 | +import { DocFileSystemJsonRead } from "../lib/doc-file-system-json-read" |
| 6 | +import { DocFileSystemWrite } from "../lib/doc-file-system-write" |
| 7 | + |
| 8 | +/** |
| 9 | + * Debug script for JSON file system |
| 10 | + */ |
| 11 | + |
| 12 | +console.log("JSON File System Debug") |
| 13 | +console.log("======================\n") |
| 14 | + |
| 15 | +// Load JSON data |
| 16 | +const jsonPath = join(import.meta.dir, "sample-docs-data.json") |
| 17 | +console.log("Loading:", jsonPath) |
| 18 | + |
| 19 | +let jsonData: unknown |
| 20 | +try { |
| 21 | + const jsonContent = await readFile(jsonPath, "utf-8") |
| 22 | + jsonData = JSON.parse(jsonContent) |
| 23 | + console.log("✅ Loaded\n") |
| 24 | +} catch (error) { |
| 25 | + console.error("❌ Failed:", error) |
| 26 | + process.exit(1) |
| 27 | +} |
| 28 | + |
| 29 | +// Setup JSON reader |
| 30 | +const jsonReader = new DocFileSystemJsonRead({ |
| 31 | + data: jsonData, |
| 32 | + basePath: "json://docs-sample", |
| 33 | +}) |
| 34 | + |
| 35 | +// Setup local writer (not used) |
| 36 | +const localWriter = new DocFileSystemWrite({ |
| 37 | + basePath: "/tmp/docs-json-output", |
| 38 | +}) |
| 39 | + |
| 40 | +// Create file system |
| 41 | +const fileSystem = new DocFileSystem({ |
| 42 | + basePath: "json://docs-sample", |
| 43 | + reader: jsonReader, |
| 44 | + writer: localWriter, |
| 45 | +}) |
| 46 | + |
| 47 | +const client = new DocClient({ |
| 48 | + fileSystem, |
| 49 | +}) |
| 50 | + |
| 51 | +console.log("Configuration:") |
| 52 | +console.log("- Reader: JSON (sample-docs-data.json)") |
| 53 | +console.log("- Writer: Local (/tmp/docs-json-output)") |
| 54 | +console.log("- Base path:", client.basePath()) |
| 55 | + |
| 56 | +// JSON data overview |
| 57 | +console.log("\n--- Data Overview ---") |
| 58 | +const allPaths = jsonReader.getAllFilePaths() |
| 59 | +console.log(`Total files: ${allPaths.length}`) |
| 60 | +console.log("\nFiles:") |
| 61 | +allPaths.forEach((path) => { |
| 62 | + const ext = path.split(".").pop() |
| 63 | + const icon = |
| 64 | + ext === "md" ? "📝" : ext === "json" ? "📦" : ext === "ts" ? "📘" : "📄" |
| 65 | + console.log(` ${icon} ${path}`) |
| 66 | +}) |
| 67 | + |
| 68 | +// Root directory |
| 69 | +console.log("\n--- Root Directory ---") |
| 70 | +const rootDir = client.directory("") |
| 71 | +const rootDirs = await rootDir.directoryNames() |
| 72 | +const rootFiles = await rootDir.fileNames() |
| 73 | + |
| 74 | +if (!(rootDirs instanceof Error)) { |
| 75 | + console.log("Directories:", rootDirs.join(", ")) |
| 76 | +} |
| 77 | + |
| 78 | +if (!(rootFiles instanceof Error)) { |
| 79 | + console.log("Files:", rootFiles.join(", ")) |
| 80 | +} |
| 81 | + |
| 82 | +// Docs directory |
| 83 | +console.log("\n--- docs/ ---") |
| 84 | +const docsDir = client.directory("docs") |
| 85 | +const docsDirs = await docsDir.directoryNames() |
| 86 | +const docsFiles = await docsDir.fileNames() |
| 87 | + |
| 88 | +if (!(docsDirs instanceof Error)) { |
| 89 | + console.log("Subdirectories:", docsDirs.join(", ")) |
| 90 | +} |
| 91 | + |
| 92 | +if (!(docsFiles instanceof Error)) { |
| 93 | + console.log("Files:", docsFiles.join(", ")) |
| 94 | +} |
| 95 | + |
| 96 | +// Read README.md |
| 97 | +console.log("\n--- README.md ---") |
| 98 | +const readmeContent = await jsonReader.readFile("README.md") |
| 99 | +if (typeof readmeContent === "string") { |
| 100 | + console.log(`First 200 chars: ${readmeContent.slice(0, 200)}...`) |
| 101 | + const size = await jsonReader.getFileSize("README.md") |
| 102 | + console.log(`Size: ${size} bytes`) |
| 103 | +} |
| 104 | + |
| 105 | +// Read and parse docs/index.md |
| 106 | +console.log("\n--- docs/index.md ---") |
| 107 | +const indexFile = await client.file("docs/index.md").read() |
| 108 | +if (!(indexFile instanceof Error)) { |
| 109 | + console.log("Title:", indexFile.content.title) |
| 110 | + console.log("Description:", indexFile.content.description) |
| 111 | + const meta = indexFile.content.meta() |
| 112 | + console.log("Meta keys:", Object.keys(meta)) |
| 113 | +} |
| 114 | + |
| 115 | +// Read docs/getting-started/installation.md |
| 116 | +console.log("\n--- docs/getting-started/installation.md ---") |
| 117 | +const installFile = await client |
| 118 | + .file("docs/getting-started/installation.md") |
| 119 | + .read() |
| 120 | +if (!(installFile instanceof Error)) { |
| 121 | + console.log("Title:", installFile.content.title) |
| 122 | + const meta = installFile.content.meta() |
| 123 | + console.log("Meta keys:", Object.keys(meta)) |
| 124 | +} |
| 125 | + |
| 126 | +// Path validation |
| 127 | +console.log("\n--- Path Validation ---") |
| 128 | + |
| 129 | +console.log("File existence:") |
| 130 | +const filesToCheck = ["README.md", "docs/index.md", "non-existent.md"] |
| 131 | +for (const file of filesToCheck) { |
| 132 | + const exists = await jsonReader.fileExists(file) |
| 133 | + console.log(` ${file}: ${exists ? "✓" : "✗"}`) |
| 134 | +} |
| 135 | + |
| 136 | +console.log("\nDirectory existence:") |
| 137 | +const dirsToCheck = ["docs", "docs/getting-started", "non-existent"] |
| 138 | +for (const dir of dirsToCheck) { |
| 139 | + const exists = await jsonReader.directoryExists(dir) |
| 140 | + console.log(` ${dir}/: ${exists ? "✓" : "✗"}`) |
| 141 | +} |
| 142 | + |
| 143 | +// Nested directories |
| 144 | +console.log("\n--- docs/getting-started/ ---") |
| 145 | +const gettingStartedDir = client.directory("docs/getting-started") |
| 146 | +const gsFiles = await gettingStartedDir.fileNames() |
| 147 | + |
| 148 | +if (!(gsFiles instanceof Error)) { |
| 149 | + console.log("Files:", gsFiles.join(", ")) |
| 150 | +} |
| 151 | + |
| 152 | +console.log("\n--- docs/guides/ ---") |
| 153 | +const guidesDir = client.directory("docs/guides") |
| 154 | +const guidesFiles = await guidesDir.fileNames() |
| 155 | + |
| 156 | +if (!(guidesFiles instanceof Error)) { |
| 157 | + console.log("Files:", guidesFiles.join(", ")) |
| 158 | +} |
| 159 | + |
| 160 | +// Error cases |
| 161 | +console.log("\n--- Error Cases ---") |
| 162 | + |
| 163 | +const nonExistent = await jsonReader.readFile("non-existent.md") |
| 164 | +console.log("Non-existent file:", nonExistent === null ? "null ✓" : "error") |
| 165 | + |
| 166 | +const modTime = await jsonReader.getFileModifiedTime("README.md") |
| 167 | +console.log( |
| 168 | + "Modified time:", |
| 169 | + modTime instanceof Error |
| 170 | + ? `Error: ${modTime.message.slice(0, 50)}...` |
| 171 | + : "unexpected", |
| 172 | +) |
| 173 | + |
| 174 | +console.log("\n✅ Complete") |
0 commit comments