Skip to content

Commit 226b93c

Browse files
committed
fix(docs): only list component examples that loaded
`listComponentExamples()` serialized the scanned names even when a file could not be read, so it could advertise an example `getComponentExample()` returns null for. Track the names that actually loaded, and only swallow ENOENT: malformed JSON now fails the build instead of silently shipping an incomplete set.
1 parent 5c773e3 commit 226b93c

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

docs/modules/component-example.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,30 @@ export default defineNuxtModule({
139139
// is every request the MCP `get-example` tool makes, since its
140140
// internal `$fetch` reaches the handler instead of the CDN).
141141
const examples: Record<string, unknown> = {}
142+
// Only the examples that actually loaded are listed, so
143+
// `listComponentExamples()` never advertises a name that
144+
// `getComponentExample()` cannot return.
145+
const availableNames: string[] = []
146+
142147
for (const name of names) {
148+
let contents: string
143149
try {
144-
examples[name] = JSON.parse(readFileSync(join(outputDir, `${name}.json`), 'utf-8'))
145-
} catch {
146-
// A missing file means the example was removed between the scan
147-
// and codegen; leave it out rather than failing the build.
150+
contents = readFileSync(join(outputDir, `${name}.json`), 'utf-8')
151+
} catch (error) {
152+
// The example was removed between the scan and codegen. Anything
153+
// else (a permission error, unreadable JSON below) is a real
154+
// problem and should fail the build.
155+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
156+
continue
157+
}
158+
throw error
148159
}
160+
161+
examples[name] = JSON.parse(contents)
162+
availableNames.push(name)
149163
}
150164

151-
return `const names = ${JSON.stringify(names)}
165+
return `const names = ${JSON.stringify(availableNames)}
152166
const examples = ${JSON.stringify(examples)}
153167
154168
function _load(name) {

0 commit comments

Comments
 (0)