Skip to content

Commit d8485b7

Browse files
Use glob pattern runner instead of listing filenames
Replace hardcoded filename list with run-examples.ts that auto-discovers all .ts files in src/ Benefits: - Add new example → automatically included in 'bun examples' - Rename example → no package.json update needed - Impossible for package.json to reference non-existent files
1 parent ba6e0d9 commit d8485b7

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

typescript/ai-sdk-v5/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"examples": "bun run src/prompt-caching/anthropic-user-message-cache.ts && bun run src/prompt-caching/anthropic-multi-message-cache.ts && bun run src/prompt-caching/anthropic-no-cache-control.ts",
7+
"examples": "bun run run-examples.ts",
88
"typecheck": "tsc --noEmit"
99
},
1010
"dependencies": {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bun
2+
/**
3+
* Run all example files in the src/ directory
4+
* Each example is run in a separate process to handle process.exit() calls
5+
*/
6+
7+
import { readdirSync, statSync } from 'fs';
8+
import { join } from 'path';
9+
import { $ } from 'bun';
10+
11+
const srcDir = join(import.meta.dir, 'src');
12+
13+
// Recursively find all .ts files in src/
14+
function findExamples(dir: string): string[] {
15+
const entries = readdirSync(dir);
16+
const files: string[] = [];
17+
18+
for (const entry of entries) {
19+
const fullPath = join(dir, entry);
20+
const stat = statSync(fullPath);
21+
22+
if (stat.isDirectory()) {
23+
files.push(...findExamples(fullPath));
24+
} else if (entry.endsWith('.ts')) {
25+
files.push(fullPath);
26+
}
27+
}
28+
29+
return files.sort();
30+
}
31+
32+
const examples = findExamples(srcDir);
33+
console.log(`Found ${examples.length} example(s)\n`);
34+
35+
let failed = 0;
36+
for (const example of examples) {
37+
const relativePath = example.replace(import.meta.dir + '/', '');
38+
console.log(`\n${'='.repeat(80)}`);
39+
console.log(`Running: ${relativePath}`);
40+
console.log('='.repeat(80));
41+
42+
try {
43+
await $`bun run ${example}`.quiet();
44+
console.log(`✅ ${relativePath} completed successfully`);
45+
} catch (error) {
46+
console.error(`❌ ${relativePath} failed`);
47+
failed++;
48+
}
49+
}
50+
51+
console.log(`\n${'='.repeat(80)}`);
52+
console.log(`Results: ${examples.length - failed}/${examples.length} passed`);
53+
console.log('='.repeat(80));
54+
55+
if (failed > 0) {
56+
process.exit(1);
57+
}

0 commit comments

Comments
 (0)