mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 10:50:44 +00:00
perf: replace madge cycle scan
This commit is contained in:
@@ -1584,7 +1584,6 @@
|
||||
"jscpd": "4.0.9",
|
||||
"jsdom": "^29.0.2",
|
||||
"lit": "^3.3.2",
|
||||
"madge": "^8.0.0",
|
||||
"oxfmt": "0.45.0",
|
||||
"oxlint": "^1.60.0",
|
||||
"oxlint-tsgolint": "^0.21.1",
|
||||
|
||||
705
pnpm-lock.yaml
generated
705
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,22 +1,175 @@
|
||||
#!/usr/bin/env node
|
||||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import madge from "madge";
|
||||
import ts from "typescript";
|
||||
|
||||
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const scanRoots = ["src", "extensions", "ui"] as const;
|
||||
const sourceExtensions = [".ts"] as const;
|
||||
const ignoredPathPartPattern =
|
||||
/(^|\/)(node_modules|dist|build|coverage|\.artifacts|\.git|assets)(\/|$)/;
|
||||
|
||||
function normalizeRepoPath(filePath: string): string {
|
||||
return filePath.split(path.sep).join("/");
|
||||
return path.relative(repoRoot, filePath).split(path.sep).join("/");
|
||||
}
|
||||
|
||||
async function main(): Promise<number> {
|
||||
const result = await madge([...scanRoots], {
|
||||
baseDir: repoRoot,
|
||||
fileExtensions: ["ts"],
|
||||
tsConfig: path.join(repoRoot, "tsconfig.json"),
|
||||
});
|
||||
const cycles = result.circular().map((cycle) => cycle.map((file) => normalizeRepoPath(file)));
|
||||
function cycleSignature(files: readonly string[]): string {
|
||||
return files.toSorted((left, right) => left.localeCompare(right)).join("\n");
|
||||
}
|
||||
|
||||
function shouldSkipRepoPath(repoPath: string): boolean {
|
||||
return ignoredPathPartPattern.test(repoPath);
|
||||
}
|
||||
|
||||
function collectSourceFiles(root: string): string[] {
|
||||
const repoPath = normalizeRepoPath(root);
|
||||
if (shouldSkipRepoPath(repoPath)) {
|
||||
return [];
|
||||
}
|
||||
const stats = statSync(root);
|
||||
if (stats.isFile()) {
|
||||
return sourceExtensions.some((extension) => repoPath.endsWith(extension)) ? [repoPath] : [];
|
||||
}
|
||||
if (!stats.isDirectory()) {
|
||||
return [];
|
||||
}
|
||||
return readdirSync(root, { withFileTypes: true })
|
||||
.flatMap((entry) => collectSourceFiles(path.join(root, entry.name)))
|
||||
.toSorted((left, right) => left.localeCompare(right));
|
||||
}
|
||||
|
||||
function loadCompilerOptions(): ts.CompilerOptions {
|
||||
const configPath = path.join(repoRoot, "tsconfig.json");
|
||||
const config = ts.readConfigFile(configPath, (filePath) => ts.sys.readFile(filePath));
|
||||
if (config.error) {
|
||||
throw new Error(ts.flattenDiagnosticMessageText(config.error.messageText, "\n"));
|
||||
}
|
||||
return ts.parseJsonConfigFileContent(config.config, ts.sys, repoRoot).options;
|
||||
}
|
||||
|
||||
function collectStaticModuleSpecifiers(sourceFile: ts.SourceFile): string[] {
|
||||
const specifiers: string[] = [];
|
||||
const visit = (node: ts.Node) => {
|
||||
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
||||
specifiers.push(node.moduleSpecifier.text);
|
||||
} else if (
|
||||
ts.isExportDeclaration(node) &&
|
||||
node.moduleSpecifier &&
|
||||
ts.isStringLiteral(node.moduleSpecifier)
|
||||
) {
|
||||
specifiers.push(node.moduleSpecifier.text);
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(sourceFile);
|
||||
return specifiers;
|
||||
}
|
||||
|
||||
function createImportGraph(files: readonly string[]): Map<string, string[]> {
|
||||
const compilerOptions = loadCompilerOptions();
|
||||
const compilerHost = ts.createCompilerHost(compilerOptions, false);
|
||||
const resolutionCache = ts.createModuleResolutionCache(
|
||||
repoRoot,
|
||||
(value) => value,
|
||||
compilerOptions,
|
||||
);
|
||||
const absoluteToRepoPath = new Map(
|
||||
files.map((file): [string, string] => [path.resolve(repoRoot, file), file]),
|
||||
);
|
||||
const graph = new Map<string, string[]>();
|
||||
|
||||
for (const file of files) {
|
||||
const absoluteFile = path.join(repoRoot, file);
|
||||
const sourceFile = ts.createSourceFile(
|
||||
file,
|
||||
readFileSync(absoluteFile, "utf8"),
|
||||
ts.ScriptTarget.Latest,
|
||||
true,
|
||||
);
|
||||
const imports = collectStaticModuleSpecifiers(sourceFile).flatMap((specifier) => {
|
||||
const resolved = ts.resolveModuleName(
|
||||
specifier,
|
||||
absoluteFile,
|
||||
compilerOptions,
|
||||
compilerHost,
|
||||
resolutionCache,
|
||||
).resolvedModule?.resolvedFileName;
|
||||
if (!resolved) {
|
||||
return [];
|
||||
}
|
||||
const repoPath = absoluteToRepoPath.get(path.resolve(resolved));
|
||||
return repoPath ? [repoPath] : [];
|
||||
});
|
||||
graph.set(
|
||||
file,
|
||||
imports.toSorted((left, right) => left.localeCompare(right)),
|
||||
);
|
||||
}
|
||||
|
||||
return graph;
|
||||
}
|
||||
|
||||
function collectStronglyConnectedComponents(
|
||||
graph: ReadonlyMap<string, readonly string[]>,
|
||||
): string[][] {
|
||||
let nextIndex = 0;
|
||||
const stack: string[] = [];
|
||||
const onStack = new Set<string>();
|
||||
const indexByNode = new Map<string, number>();
|
||||
const lowLinkByNode = new Map<string, number>();
|
||||
const components: string[][] = [];
|
||||
|
||||
const visit = (node: string) => {
|
||||
indexByNode.set(node, nextIndex);
|
||||
lowLinkByNode.set(node, nextIndex);
|
||||
nextIndex += 1;
|
||||
stack.push(node);
|
||||
onStack.add(node);
|
||||
|
||||
for (const next of graph.get(node) ?? []) {
|
||||
if (!indexByNode.has(next)) {
|
||||
visit(next);
|
||||
lowLinkByNode.set(node, Math.min(lowLinkByNode.get(node)!, lowLinkByNode.get(next)!));
|
||||
} else if (onStack.has(next)) {
|
||||
lowLinkByNode.set(node, Math.min(lowLinkByNode.get(node)!, indexByNode.get(next)!));
|
||||
}
|
||||
}
|
||||
|
||||
if (lowLinkByNode.get(node) !== indexByNode.get(node)) {
|
||||
return;
|
||||
}
|
||||
const component: string[] = [];
|
||||
let current: string | undefined;
|
||||
do {
|
||||
current = stack.pop();
|
||||
if (!current) {
|
||||
throw new Error("Import cycle stack underflow");
|
||||
}
|
||||
onStack.delete(current);
|
||||
component.push(current);
|
||||
} while (current !== node);
|
||||
if (component.length > 1 || (graph.get(node) ?? []).includes(node)) {
|
||||
components.push(component.toSorted((left, right) => left.localeCompare(right)));
|
||||
}
|
||||
};
|
||||
|
||||
for (const node of graph.keys()) {
|
||||
if (!indexByNode.has(node)) {
|
||||
visit(node);
|
||||
}
|
||||
}
|
||||
|
||||
return components.toSorted(
|
||||
(left, right) =>
|
||||
right.length - left.length || cycleSignature(left).localeCompare(cycleSignature(right)),
|
||||
);
|
||||
}
|
||||
|
||||
function main(): number {
|
||||
const files = scanRoots.flatMap((root) => collectSourceFiles(path.join(repoRoot, root)));
|
||||
const graph = createImportGraph(files);
|
||||
const cycles = collectStronglyConnectedComponents(graph);
|
||||
|
||||
console.log(`Madge import cycle check: ${cycles.length} cycle(s).`);
|
||||
if (cycles.length === 0) {
|
||||
@@ -34,4 +187,4 @@ async function main(): Promise<number> {
|
||||
return 1;
|
||||
}
|
||||
|
||||
process.exitCode = await main();
|
||||
process.exitCode = main();
|
||||
|
||||
Reference in New Issue
Block a user