Repository navigation
Expand file tree
/
Copy pathopenapi.ts
More file actions
53 lines (47 loc) · 1.61 KB
/
Copy pathopenapi.ts
File metadata and controls
53 lines (47 loc) · 1.61 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 * as fs from 'fs';
import * as path from 'path';
import type { IndexerPlugin } from '../plugin-api';
import type { IndexerResult } from '../../types';
const SPEC_PATTERNS = [
/openapi\.(ya?ml|json)$/i,
/swagger\.(ya?ml|json)$/i,
/api-docs?\.(ya?ml|json)$/i,
/\.openapi\.(ya?ml|json)$/i,
];
const SPEC_DIRS = ['openapi', 'api', 'spec', 'specs', 'docs/api'];
function isSpecFile(filePath: string): boolean {
const base = path.basename(filePath);
if (SPEC_PATTERNS.some((p) => p.test(base))) return true;
const parts = filePath.split(/[/\\]/);
return parts.some((p) => SPEC_DIRS.includes(p.toLowerCase())) && /\.(ya?ml|json)$/i.test(base);
}
function walk(root: string, skip: Set<string>, match: (p: string) => boolean): string[] {
const found: string[] = [];
function visit(dir: string): void {
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
if (skip.has(entry.name)) continue;
const full = path.join(dir, entry.name);
if (entry.isDirectory()) visit(full);
else if (entry.isFile() && match(full)) found.push(full);
}
}
visit(root);
return found;
}
const SKIP = new Set(['node_modules', '.git', 'dist', 'build', '.codegraphplus', 'vendor']);
export const openApiIndexer: IndexerPlugin = {
name: 'openapi',
discover(root: string): string[] {
return walk(root, SKIP, isSpecFile);
},
indexFile(): IndexerResult {
// Reserved — real OpenAPI parsing not implemented yet
return { assetsAdded: 0, relationsAdded: 0, skipped: true };
},
};