-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-json.ts
More file actions
53 lines (45 loc) · 1.37 KB
/
Copy pathvalidate-json.ts
File metadata and controls
53 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { join } from "node:path";
const glob = new Bun.Glob("src/content/*.json");
const files: string[] = [];
for await (const file of glob.scan({ cwd: process.cwd() })) {
files.push(file);
}
files.sort();
if (files.length === 0) {
console.error("❌ No JSON files found in src/content.");
process.exit(1);
}
let hasError = false;
for (const file of files) {
try {
const filePath = join(process.cwd(), file);
const content = await Bun.file(filePath).text();
JSON.parse(content);
console.log(`✅ ${file} is valid JSON`);
} catch (e) {
hasError = true;
console.error(
`❌ ${file} is INVALID JSON: ${e instanceof Error ? e.message : String(e)}`,
);
// Output the part where it failed if possible
if (e instanceof Error) {
const match = e.message.match(/position (\d+)/);
if (match) {
const pos = parseInt(match[1]);
try {
const filePath = join(process.cwd(), file);
const content = await Bun.file(filePath).text();
const start = Math.max(0, pos - 50);
const end = Math.min(content.length, pos + 50);
console.error("Context:", content.substring(start, end));
console.error(" ".repeat(Math.min(50, pos)) + "^");
} catch (err) {
// Ignore errors reading file for context
}
}
}
}
}
if (hasError) {
process.exit(1);
}