path-utils.js 3.04 KB
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,
};