eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 4.68 kB
JavaScript
import{getDevelopmentEnvironmentFilePaths}from"#cli/dev/environment.js";import{dirname,join,normalize,resolve,sep}from"node:path";import{existsSync}from"node:fs";import{providerSettingsPath}from"#setup/provider-settings.js";import{toErrorMessage}from"#shared/errors.js";import{AUTHORED_ARTIFACTS_UPDATED_LOG_LINE,STRUCTURAL_RELOAD_LOG_LINE,formatChangeDetectedLogLine}from"#internal/nitro/host/dev-watcher-log.js";import{resolveDevelopmentSourceSnapshotWatchPaths}from"#internal/nitro/dev-runtime-source-snapshot.js";import{resolveTsConfigDependencyPaths}from"#internal/application/tsconfig-dependencies.js";import{watch as watch$1}from"#compiled/chokidar/index.js";const WATCHED_LOCKFILE_NAMES=[`pnpm-lock.yaml`,`package-lock.json`,`yarn.lock`,`bun.lock`,`bun.lockb`],WATCH_ROOT_MARKER_NAMES=[`.git`,`pnpm-workspace.yaml`],WATCHER_IGNORED_DIRECTORY_NAMES=new Set([`.devtools`,`.generated`,`.eve`,`.git`,`.next`,`.output`,`.turbo`,`.vercel`,`build`,`dist`,`node_modules`]);async function startAuthoredSourceWatcher(e){let t=e.preparedHost,n=!1,r=Promise.resolve(),i,a=!1,o=new Set,s=new Map,c=new Set,l=await resolveAuthoredWatchPaths(t),u=createWatchPathMap(l),d=watch$1(l,{awaitWriteFinish:{pollInterval:50,stabilityThreshold:160},followSymlinks:!1,ignoreInitial:!0,ignored:e=>shouldIgnoreWatcherPath(e,t.appRoot,t.workspaceExtensions)}),f=waitForWatcherReady(d),rebuild=async(i,a=!1)=>{n||o.size>0||(r=r.then(async()=>{if(n)return;let r=[...s.values()];if(!i&&r.length===0)return;let o=[...c];s.clear(),c.clear();let l=t;!a&&r.length>0&&console.log(formatChangeDetectedLogLine(l.appRoot,r));try{let n=await e.coordinator.rebuild({changedPaths:o});t=n.host,a||(n.kind===`structural`?console.log(STRUCTURAL_RELOAD_LOG_LINE):console.log(AUTHORED_ARTIFACTS_UPDATED_LOG_LINE)),u=syncWatcherPaths({nextWatchPaths:await resolveAuthoredWatchPaths(t),previousWatchPathsByKey:u,watcher:d})}catch(e){console.error(`[eve:dev] rebuild failed: ${toErrorMessage(e)}`)}}).catch(e=>{console.error(`[eve:dev] rebuild queue error: ${toErrorMessage(e)}`)}),await r)},flush=async()=>{i!==void 0&&(clearTimeout(i),i=void 0),await rebuild(!1)},forceRebuild=async(e=!1)=>{i!==void 0&&(clearTimeout(i),i=void 0),await rebuild(!0,e)};return d.on(`all`,(e,t)=>{!n&&a&&e!==`addDir`&&e!==`unlinkDir`&&(s.set(`${e}:${t}`,{event:e,path:t}),c.add(t),i!==void 0&&clearTimeout(i),i=setTimeout(()=>{i=void 0,flush()},120))}),await f,a=!0,{async close(){n=!0,i!==void 0&&(clearTimeout(i),i=void 0),await d.close(),await r},flush,rebuild:forceRebuild,async suspend(e){o.has(e)||(o.add(e),i!==void 0&&(clearTimeout(i),i=void 0),await r)},async resume(e,t){o.delete(e)&&o.size===0&&await forceRebuild(t?.silent)}}}async function waitForWatcherReady(e){await new Promise((t,n)=>{e.on(`ready`,()=>{t()}),e.on(`error`,e=>{n(e)})})}async function resolveAuthoredWatchPaths(e){let t=new Set([e.compileResult.project.agentRoot,join(e.appRoot,`package.json`),join(e.appRoot,`jsconfig.json`),join(e.appRoot,`tsconfig.json`),join(e.appRoot,`tsconfig.*.json`),providerSettingsPath(e.appRoot)]),n=await resolveTsConfigWatchPaths(e.appRoot),r=await resolveDevelopmentSourceSnapshotWatchPaths(e.appRoot);for(let n of e.workspaceExtensions){t.add(n.config.sourceRoot);for(let e of n.buildConfigPaths)t.add(e)}for(let n of getDevelopmentEnvironmentFilePaths(e.appRoot))t.add(n);for(let e of r)t.add(e);for(let e of n)t.add(e);for(let n of resolveLockfileSearchDirectories(e.appRoot))for(let e of WATCHED_LOCKFILE_NAMES)t.add(join(n,e));return[...t].sort((e,t)=>e.localeCompare(t))}function createWatchPathMap(e){let t=new Map;for(let n of e)t.set(toWatchPathKey(n),n);return t}function syncWatcherPaths(e){let t=createWatchPathMap(e.nextWatchPaths),n=[],r=[];for(let[r,i]of t)e.previousWatchPathsByKey.has(r)||n.push(i);for(let[n,i]of e.previousWatchPathsByKey)t.has(n)||r.push(i);return n.length>0&&e.watcher.add(n),r.length>0&&e.watcher.unwatch(r),t}function toWatchPathKey(e){return normalize(resolve(e))}function resolveLockfileSearchDirectories(e){let t=resolve(e),n=[t],r=t;for(;;){if(hasWatchRootMarker(r))return n;let e=dirname(r);if(e===r)return[t];r=e,n.push(r)}}function hasWatchRootMarker(e){return WATCH_ROOT_MARKER_NAMES.some(t=>existsSync(join(e,t)))}async function resolveTsConfigWatchPaths(e){return await resolveTsConfigDependencyPaths(e)}function shouldIgnoreWatcherPath(e,t,n){let r=normalize(e),i=r.split(sep).filter(Boolean);return r!==normalize(providerSettingsPath(t))&&i.some(e=>WATCHER_IGNORED_DIRECTORY_NAMES.has(e))||n.some(t=>isPathInsideOrEqual(e,t.config.outDir))}function isPathInsideOrEqual(e,t){let n=resolve(e),r=resolve(t);return n===r||n.startsWith(`${r}${sep}`)}export{startAuthoredSourceWatcher};