path-utils.js
3.04 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const fs = require("node:fs");
const path = require("node:path");
const SOURCE_EXTENSIONS = [".js", ".vue", ".json"];
const IGNORED_DIRECTORIES = new Set([
"node_modules",
"dist",
"coverage",
".git",
".cache",
"copyDirTarget",
]);
function toPosix(filePath) {
return filePath.replace(/\\/g, "/");
}
function relativePath(rootDir, filePath) {
return toPosix(path.relative(rootDir, filePath));
}
function isTestFile(filePath) {
return /(?:^|\/)(?:tests?\/.*|.*\.(?:spec|test)\.js$)/.test(toPosix(filePath));
}
function isDocumentFile(filePath) {
const normalized = toPosix(filePath);
return normalized.endsWith(".md")
|| /src\/api\/adapters\/.*\/__contracts__\/.*\.contract\.json$/.test(normalized);
}
function shouldIncludeFile(relativeFile) {
const normalized = toPosix(relativeFile);
if (normalized.startsWith("scripts/knowledge-graph/__fixtures__/")) {
return false;
}
if (normalized.startsWith(".ai/generated/")) {
return false;
}
if (normalized.endsWith(".vue") || normalized.endsWith(".js")) {
return true;
}
if (isDocumentFile(normalized)) {
return true;
}
return normalized === ".ai/knowledge/domain-catalog.json"
|| normalized === ".ai/knowledge/known-gaps.json"
|| /src\/api\/adapters\/.*\/__contracts__\/.*\.contract\.json$/.test(normalized);
}
function collectFiles(rootDir) {
const files = [];
function walk(currentDir) {
for (const entry of fs.readdirSync(currentDir, { withFileTypes: true })) {
if (entry.isDirectory() && IGNORED_DIRECTORIES.has(entry.name)) {
continue;
}
const absolutePath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
walk(absolutePath);
continue;
}
const relativeFile = relativePath(rootDir, absolutePath);
if (shouldIncludeFile(relativeFile)) {
files.push(relativeFile);
}
}
}
walk(rootDir);
return files.sort();
}
function resolveWithCandidates(absoluteBase) {
const candidates = [absoluteBase];
SOURCE_EXTENSIONS.forEach(extension => candidates.push(`${absoluteBase}${extension}`));
SOURCE_EXTENSIONS.forEach(extension => candidates.push(path.join(absoluteBase, `index${extension}`)));
return candidates.find(candidate => fs.existsSync(candidate) && fs.statSync(candidate).isFile()) || null;
}
function resolveModule(rootDir, fromFile, specifier) {
if (typeof specifier !== "string" || !specifier) {
return null;
}
let absoluteBase;
if (specifier.startsWith("@/")) {
absoluteBase = path.join(rootDir, "src", specifier.slice(2));
} else if (specifier.startsWith("@api/")) {
absoluteBase = path.join(rootDir, "src/api", specifier.slice(5));
} else if (specifier.startsWith(".")) {
absoluteBase = path.resolve(rootDir, path.dirname(fromFile), specifier);
} else {
return null;
}
const resolved = resolveWithCandidates(absoluteBase);
return resolved ? relativePath(rootDir, resolved) : null;
}
function resolveView(rootDir, importPath) {
return resolveWithCandidates(path.join(rootDir, "src/views", importPath));
}
module.exports = {
collectFiles,
isDocumentFile,
isTestFile,
relativePath,
resolveModule,
resolveView,
toPosix,
};