mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-22 06:32:00 +00:00
test: harden path resolution test helpers
This commit is contained in:
50
src/cli/command-source.test-helpers.ts
Normal file
50
src/cli/command-source.test-helpers.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
function resolveImportedTypeScriptPath(importerPath: string, target: string): string {
|
||||
const resolvedTarget = path.join(path.dirname(importerPath), target);
|
||||
return resolvedTarget.replace(/\.js$/u, ".ts");
|
||||
}
|
||||
|
||||
async function readModuleSource(modulePath: string, seen: Set<string>): Promise<string> {
|
||||
const resolvedPath = path.resolve(modulePath);
|
||||
if (seen.has(resolvedPath)) {
|
||||
return "";
|
||||
}
|
||||
seen.add(resolvedPath);
|
||||
|
||||
const source = await fs.readFile(resolvedPath, "utf8");
|
||||
if (source.includes("resolveCommandSecretRefsViaGateway")) {
|
||||
return source;
|
||||
}
|
||||
const nestedTargets = new Set<string>();
|
||||
|
||||
for (const match of source.matchAll(/^export \* from "(?<target>[^"]+)";$/gmu)) {
|
||||
const target = match.groups?.target;
|
||||
if (target) {
|
||||
nestedTargets.add(resolveImportedTypeScriptPath(resolvedPath, target));
|
||||
}
|
||||
}
|
||||
|
||||
for (const match of source.matchAll(/import\("(?<target>\.[^"]+\.runtime\.js)"\)/gmu)) {
|
||||
const target = match.groups?.target;
|
||||
if (target) {
|
||||
nestedTargets.add(resolveImportedTypeScriptPath(resolvedPath, target));
|
||||
}
|
||||
}
|
||||
|
||||
const nestedSources = (
|
||||
await Promise.all(
|
||||
[...nestedTargets].map(async (targetPath) => await readModuleSource(targetPath, seen)),
|
||||
)
|
||||
).filter(Boolean);
|
||||
|
||||
return nestedSources.length > 0 ? [source, ...nestedSources].join("\n") : source;
|
||||
}
|
||||
|
||||
export async function readCommandSource(
|
||||
relativePath: string,
|
||||
cwd = process.cwd(),
|
||||
): Promise<string> {
|
||||
return await readModuleSource(path.join(cwd, relativePath), new Set<string>());
|
||||
}
|
||||
Reference in New Issue
Block a user