eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.24 kB
JavaScript
import{lstat}from"node:fs/promises";import{dirname,join,relative,resolve,sep}from"node:path";import{existsSync}from"node:fs";import{parseTsConfigObject,readTextFileIfExists}from"#internal/application/tsconfig-dependencies.js";async function resolveLocalTsConfigPathTargetRoots(e){let t=await readTextFileIfExists(e.configPath);if(t===void 0)return[];let n=parseTsConfigObject(t),r=isObjectRecord(n?.compilerOptions)?n.compilerOptions:void 0,i=isObjectRecord(r?.paths)?r.paths:void 0;if(r===void 0||i===void 0)return[];let a=typeof r.baseUrl==`string`?resolve(dirname(e.configPath),r.baseUrl):dirname(e.configPath),o=new Set;for(let t of Object.values(i))if(Array.isArray(t))for(let n of t){if(typeof n!=`string`||n.length===0)continue;let t=await resolveLocalTsConfigPathTargetRoot({baseDirectory:a,sourceRoot:e.sourceRoot,target:n});t!==void 0&&o.add(t)}return[...o].sort((e,t)=>e.localeCompare(t))}async function resolveNearestPackageRoot(e,t){let n=resolve(e);try{(await lstat(n)).isDirectory()||(n=dirname(n))}catch{n=dirname(n)}for(;isAuthoredSourcePath(n,t);){if(existsSync(join(n,`package.json`)))return n;let e=dirname(n);if(e===n)return;n=e}}function isAuthoredSourcePath(e,t){return isPathInsideOrEqual(e,t)?!relative(t,e).split(/[\\/]/).includes(`node_modules`):!1}function isPathInsideOrEqual(e,t){let n=resolve(e),r=resolve(t);return n===r||n.startsWith(`${r}${sep}`)}async function resolveLocalTsConfigPathTargetRoot(e){let t=e.target.includes(`*`),n=t?e.target.slice(0,e.target.indexOf(`*`)):e.target;if(n.length===0||n===`.`||n===`./`)return;let r=resolve(e.baseDirectory,n);if(!isAuthoredSourcePath(r,e.sourceRoot))return;let i=await resolveExistingPathOrAncestor({path:r,stopDirectory:e.sourceRoot});if(i===void 0)return;let a=await resolveNearestPackageRoot(i,e.sourceRoot);if(a!==void 0&&a!==e.sourceRoot)return a;if(!t)return i===e.sourceRoot?void 0:i}async function resolveExistingPathOrAncestor(e){let t=resolve(e.path);for(;isAuthoredSourcePath(t,e.stopDirectory);){if(existsSync(t))return t;let e=dirname(t);if(e===t)return;t=e}}function isObjectRecord(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}export{isAuthoredSourcePath,isPathInsideOrEqual,resolveLocalTsConfigPathTargetRoots,resolveNearestPackageRoot};