Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import { registerHooks } from "node:module"import path from "node:path"import { fileURLToPath } from "node:url"import { localSource } from "./source.js"import { missingPackageTarget } from "./source.package.js"import { Host } from "./host.js"
let generation = Date.now()
export async function prepareSource(entrypoint: string, track: (file: string, directory?: boolean) => void) { const version = String(++generation) const fresh = (specifier: string) => { const url = new URL(specifier) url.searchParams.set("__opencode_reload", version) return url.href } const hook = registerHooks({ resolve(specifier, context, nextResolve) { if (!context.parentURL || new URL(context.parentURL).searchParams.get("__opencode_reload") !== version) return nextResolve(specifier, context) const local = specifier.startsWith("./") || specifier.startsWith("../") ? new URL(specifier, context.parentURL) : localSource(specifier, path.dirname(fileURLToPath(context.parentURL))) if (!local) { try { return nextResolve(specifier, context) } catch (error) { const target = missingPackageTarget(specifier, fileURLToPath(context.parentURL)) if (target) track(target, true) throw error } } if (fileURLToPath(local).split(path.sep).includes("node_modules")) return nextResolve(specifier, context) const resolved = (() => { try { return nextResolve(specifier, context) } catch (error) { track(path.dirname(fileURLToPath(local)), true) throw error } })() if (!resolved.url.startsWith("file:")) return resolved const file = fileURLToPath(resolved.url) if (file.split(path.sep).includes("node_modules")) return resolved track(file) return { ...resolved, url: fresh(resolved.url) } }, }) const specifier = fresh(entrypoint) return { version: specifier, load: () => Host.load(specifier), dispose: () => hook.deregister() }}