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): Promise { 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(); for (const match of source.matchAll(/^export \* from "(?[^"]+)";$/gmu)) { const target = match.groups?.target; if (target) { nestedTargets.add(resolveImportedTypeScriptPath(resolvedPath, target)); } } for (const match of source.matchAll(/import\("(?\.[^"]+\.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 { return await readModuleSource(path.join(cwd, relativePath), new Set()); }