mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 19:20:43 +00:00
refactor: dedupe tooling helpers
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import ts from "typescript";
|
||||
import {
|
||||
collectSourceFiles,
|
||||
collectStronglyConnectedComponents,
|
||||
formatCycle,
|
||||
} from "./lib/import-cycle-graph.ts";
|
||||
|
||||
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const scanRoots = ["src", "extensions", "scripts"] as const;
|
||||
@@ -13,14 +18,6 @@ const declarationSourcePattern = /\.d\.[cm]?ts$/;
|
||||
const ignoredPathPartPattern =
|
||||
/(^|\/)(node_modules|dist|build|coverage|\.artifacts|\.git|assets)(\/|$)/;
|
||||
|
||||
function normalizeRepoPath(filePath: string): string {
|
||||
return path.relative(repoRoot, filePath).split(path.sep).join("/");
|
||||
}
|
||||
|
||||
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) ||
|
||||
@@ -30,23 +27,6 @@ function shouldSkipRepoPath(repoPath: string): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
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 createSourceResolver(files: readonly string[]) {
|
||||
const fileSet = new Set(files);
|
||||
const pathMap = new Map<string, string>();
|
||||
@@ -152,106 +132,14 @@ function collectRuntimeStaticImports(
|
||||
return imports.toSorted((left, right) => left.localeCompare(right));
|
||||
}
|
||||
|
||||
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 findCycleWitness(
|
||||
component: readonly string[],
|
||||
graph: ReadonlyMap<string, readonly string[]>,
|
||||
): string[] {
|
||||
const componentSet = new Set(component);
|
||||
const start = component[0];
|
||||
if (!start) {
|
||||
return [];
|
||||
}
|
||||
const activePath: string[] = [];
|
||||
const visited = new Set<string>();
|
||||
const visit = (node: string): string[] | null => {
|
||||
activePath.push(node);
|
||||
visited.add(node);
|
||||
for (const next of graph.get(node) ?? []) {
|
||||
if (!componentSet.has(next)) {
|
||||
continue;
|
||||
}
|
||||
const existingIndex = activePath.indexOf(next);
|
||||
if (existingIndex >= 0) {
|
||||
return [...activePath.slice(existingIndex), next];
|
||||
}
|
||||
if (!visited.has(next)) {
|
||||
const result = visit(next);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
activePath.pop();
|
||||
return null;
|
||||
};
|
||||
return visit(start) ?? component;
|
||||
}
|
||||
|
||||
function formatCycle(
|
||||
component: readonly string[],
|
||||
graph: ReadonlyMap<string, readonly string[]>,
|
||||
): string {
|
||||
const witness = findCycleWitness(component, graph);
|
||||
return witness.map((file, index) => `${index === 0 ? " " : " -> "}${file}`).join("\n");
|
||||
}
|
||||
|
||||
function main(): number {
|
||||
const files = scanRoots.flatMap((root) => collectSourceFiles(path.join(repoRoot, root)));
|
||||
const files = scanRoots.flatMap((root) =>
|
||||
collectSourceFiles(path.join(repoRoot, root), {
|
||||
repoRoot,
|
||||
sourceExtensions,
|
||||
shouldSkipRepoPath,
|
||||
}),
|
||||
);
|
||||
const resolveSource = createSourceResolver(files);
|
||||
const graph = new Map(
|
||||
files.map((file): [string, string[]] => [
|
||||
|
||||
Reference in New Issue
Block a user