| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import { promises as fs } from "node:fs";
- import path from "node:path";
- import ts from "typescript";
- import { parse as parseYaml } from "yaml";
- const SOURCE_EXTENSIONS = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"];
- export function posix(value) {
- return value.split(path.sep).join("/");
- }
- function isSourceFile(file) {
- return SOURCE_EXTENSIONS.some((extension) => file.endsWith(extension));
- }
- async function walk(root, files) {
- let entries;
- try {
- entries = await fs.readdir(root, { withFileTypes: true });
- } catch {
- return;
- }
- for (const entry of entries) {
- if (["node_modules", "dist", "out", "coverage", ".git"].includes(entry.name)) continue;
- const target = path.join(root, entry.name);
- if (entry.isDirectory()) await walk(target, files);
- else if (entry.isFile() && isSourceFile(target)) files.push(target);
- }
- }
- function validatePolicy(raw, cwd) {
- if (!raw || raw.version !== 1 || !Array.isArray(raw.modules)) {
- throw new Error("architecture-policy.yaml 必须包含 version: 1 和 modules 数组");
- }
- const ids = new Set();
- const modules = raw.modules.map((module) => {
- if (!module?.id || ids.has(module.id))
- throw new Error(`模块 id 重复或为空: ${module?.id ?? ""}`);
- ids.add(module.id);
- if (!Array.isArray(module.roots) || module.roots.length === 0) {
- throw new Error(`模块 ${module.id} 必须声明 roots`);
- }
- return {
- id: module.id,
- roots: module.roots.map((root) => path.resolve(cwd, root)),
- managed: module.managed === true,
- requires: [...new Set(module.requires ?? [])],
- publicEntrypoints: (module.publicEntrypoints ?? []).map((entry) => posix(entry)),
- layers: module.layers ?? {},
- layerOrder: module.layerOrder ?? Object.keys(module.layers ?? {}),
- owner: module.owner ?? null,
- };
- });
- const moduleIds = new Set(modules.map((module) => module.id));
- for (const module of modules) {
- for (const dependency of module.requires) {
- if (!moduleIds.has(dependency))
- throw new Error(`模块 ${module.id} 依赖未知模块 ${dependency}`);
- }
- }
- const global = {
- maxFileLines: raw.global?.maxFileLines ?? 400,
- maxContractLines: raw.global?.maxContractLines ?? 300,
- forbidCycles: raw.global?.forbidCycles !== false,
- forbidDeepImports: raw.global?.forbidDeepImports !== false,
- managedOnly: raw.global?.managedOnly !== false,
- maxPublicMethods: raw.global?.maxPublicMethods ?? 12,
- };
- const exceptions = (raw.exceptions ?? []).map((exception) => ({
- ...exception,
- paths: exception.paths ?? [],
- }));
- return { version: 1, modules, global, exceptions };
- }
- export function layerForFile(file, module) {
- const matches = module.roots
- .flatMap((root) =>
- Object.entries(module.layers).map(([name, directory]) => ({
- name,
- directory: path.resolve(root, directory),
- })),
- )
- .filter(({ directory }) => file === directory || file.startsWith(`${directory}${path.sep}`))
- .sort((a, b) => b.directory.length - a.directory.length);
- return matches[0]?.name ?? null;
- }
- export function publicEntrypointMatches(target, targetModule, cwd) {
- return targetModule.publicEntrypoints.some((entry) => {
- const candidates = [
- path.resolve(cwd, entry),
- ...targetModule.roots.map((root) => path.resolve(root, entry)),
- ];
- return candidates.includes(target);
- });
- }
- export function manifestRequires(source) {
- const match = source.match(/requires\s*:\s*\[([^\]]*)\]/s);
- if (!match) return null;
- return [...match[1].matchAll(/["']([^"']+)["']/g)].map((item) => item[1]);
- }
- export function countPublicMethods(source) {
- const sourceFile = ts.createSourceFile("contract.ts", source, ts.ScriptTarget.Latest, true);
- let count = 0;
- function visit(node) {
- if (ts.isInterfaceDeclaration(node) || ts.isClassDeclaration(node)) {
- for (const member of node.members) {
- if (
- ts.isMethodSignature(member) ||
- ts.isMethodDeclaration(member) ||
- ts.isCallSignatureDeclaration(member)
- )
- count += 1;
- }
- }
- ts.forEachChild(node, visit);
- }
- visit(sourceFile);
- return count;
- }
- export async function loadPolicy(cwd = process.cwd()) {
- const filename = path.join(cwd, "architecture-policy.yaml");
- const content = await fs.readFile(filename, "utf8");
- return validatePolicy(parseYaml(content), cwd);
- }
- export function moduleForFile(file, policy) {
- const candidates = policy.modules
- .filter((module) =>
- module.roots.some((root) => file === root || file.startsWith(`${root}${path.sep}`)),
- )
- .sort(
- (a, b) =>
- Math.max(...b.roots.map((root) => root.length)) -
- Math.max(...a.roots.map((root) => root.length)),
- );
- return candidates[0] ?? null;
- }
- export async function discoverFiles(policy) {
- const files = [];
- for (const module of policy.modules) {
- for (const root of module.roots) await walk(root, files);
- }
- return [...new Set(files)].sort();
- }
- export function resolveImport(from, specifier, knownFiles) {
- if (!specifier.startsWith(".")) return null;
- let base = path.resolve(path.dirname(from), specifier);
- if (SOURCE_EXTENSIONS.some((extension) => base.endsWith(extension))) {
- base = base.slice(0, base.lastIndexOf("."));
- }
- const candidates = [];
- for (const extension of SOURCE_EXTENSIONS) candidates.push(`${base}${extension}`);
- for (const extension of SOURCE_EXTENSIONS) candidates.push(path.join(base, `index${extension}`));
- return candidates.find((candidate) => knownFiles.has(candidate)) ?? null;
- }
- export function importsOf(file, source) {
- const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true);
- const imports = [];
- function visit(node) {
- if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && node.moduleSpecifier) {
- imports.push(node.moduleSpecifier.text);
- }
- if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
- if (ts.isStringLiteral(node.moduleReference.expression))
- imports.push(node.moduleReference.expression.text);
- }
- ts.forEachChild(node, visit);
- }
- visit(sourceFile);
- return imports;
- }
- export function isException(policy, rule, file, cwd) {
- const relative = posix(path.relative(cwd, file));
- return policy.exceptions.some(
- (exception) =>
- exception.rule === rule &&
- exception.paths.some(
- (pattern) => relative === pattern || relative.startsWith(pattern.replace(/\*\*$/, "")),
- ),
- );
- }
|