-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcontent-type-utils.ts
More file actions
51 lines (43 loc) · 1.47 KB
/
content-type-utils.ts
File metadata and controls
51 lines (43 loc) · 1.47 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
import { existsSync, readdirSync, readFileSync } from 'node:fs';
import { resolve as pResolve } from 'node:path';
/**
* Reads all content type schema files from a directory
* @param dirPath - Path to content types directory
* @param ignoredFiles - Files to ignore (defaults to schema.json, .DS_Store, __master.json, __priority.json)
* @returns Array of content type schemas (empty if the path is missing or has no eligible files)
*/
export function readContentTypeSchemas(
dirPath: string,
ignoredFiles: string[] = ['schema.json', '.DS_Store', '__master.json', '__priority.json', 'field_rules_uid.json'],
): Record<string, unknown>[] {
if (!existsSync(dirPath)) {
return [];
}
const files = readdirSync(dirPath);
if (!files || files.length === 0) {
return [];
}
const contentTypes: Record<string, unknown>[] = [];
for (const file of files) {
// Skip if not a JSON file
if (!file.endsWith('.json')) {
continue;
}
// Skip ignored files
if (ignoredFiles.includes(file)) {
continue;
}
try {
const filePath = pResolve(dirPath, file);
const raw = readFileSync(filePath, 'utf8');
const contentType = JSON.parse(raw) as Record<string, unknown>;
if (contentType) {
contentTypes.push(contentType as Record<string, unknown>);
}
} catch (error) {
// Skip files that cannot be parsed
console.warn(`Failed to read content type file ${file}:`, error);
}
}
return contentTypes;
}