mirror of
https://github.com/stablyai/orca.git
synced 2026-11-13 09:27:29 +00:00
31 lines
1.4 KiB
JavaScript
31 lines
1.4 KiB
JavaScript
import { copyFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs'
|
|
import { dirname, join, relative } from 'node:path'
|
|
|
|
// Walk/copy relative modules instead of maintaining fixture list: omitted dependencies hide target failures behind resolution errors.
|
|
export function copyScriptWithLocalModules(sourceScriptPath, destinationScriptsDir) {
|
|
mkdirSync(destinationScriptsDir, { recursive: true })
|
|
for (const modulePath of collectScriptModules(sourceScriptPath)) {
|
|
const destination = join(destinationScriptsDir, relative(dirname(sourceScriptPath), modulePath))
|
|
mkdirSync(dirname(destination), { recursive: true })
|
|
copyFileSync(modulePath, destination)
|
|
}
|
|
}
|
|
|
|
function collectScriptModules(scriptPath, seen = new Set()) {
|
|
if (seen.has(scriptPath)) {
|
|
return seen
|
|
}
|
|
seen.add(scriptPath)
|
|
// Follow static/dynamic imports and plain require for CJS gates; exclude projectRequire/requireLocal, which resolve from project root rather than this module.
|
|
const source = readFileSync(scriptPath, 'utf8')
|
|
const specifiers = source.matchAll(
|
|
/(?:\bfrom|\brequire\s*\(|\bimport\s*\(|\bimport)\s*'(\.\.?\/[^']+)'/g
|
|
)
|
|
for (const [, specifier] of specifiers) {
|
|
const modulePath = join(dirname(scriptPath), specifier)
|
|
const resolved = [modulePath, `${modulePath}.ts`].find((file) => existsSync(file)) ?? modulePath
|
|
collectScriptModules(resolved, seen)
|
|
}
|
|
return seen
|
|
}
|