eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.41 kB
JavaScript
import{serializeHeadlessSetupEvent}from"./setup-headless.js";import{readFile,unlink,writeFile}from"node:fs/promises";import{isAbsolute,relative,resolve}from"node:path";const PROJECT_INSTALL_FILES=[`package.json`,`pnpm-lock.yaml`,`pnpm-workspace.yaml`,`package-lock.json`,`npm-shrinkwrap.json`,`yarn.lock`,`bun.lock`,`bun.lockb`,`.env.example`,`.env.local`];function registryTargets(e,t){if(typeof t!=`object`||!t||!(`files`in t))return[];let n=t.files;return Array.isArray(n)?n.flatMap(t=>{if(typeof t!=`object`||!t||!(`target`in t))return[];let n=t.target;if(typeof n!=`string`||n.length===0)return[];let r=resolve(e,n),i=relative(e,r);return isAbsolute(i)||i.startsWith(`..`)?[]:[i]}):[]}async function snapshotFile(e){try{return{path:e,contents:await readFile(e)}}catch(t){if(t.code===`ENOENT`)return{path:e};throw t}}async function snapshotRegistryInstall(e,t){let n=[...new Set([...PROJECT_INSTALL_FILES,...registryTargets(e,t)])];return await Promise.all(n.map(t=>snapshotFile(resolve(e,t))))}async function rollbackRegistryInstall(e,t){let n=[];for(let r of t)try{r.contents===void 0?await unlink(r.path).catch(e=>{if(e.code!==`ENOENT`)throw e}):await writeFile(r.path,r.contents)}catch{n.push(relative(e,r.path))}return{restored:n.length===0,changed:n}}function registryInstallFailureCode(e){let t=e instanceof Error?e.message:String(e);return t.includes(`ERR_PNPM_IGNORED_BUILDS`)||t.includes(`Ignored build scripts`)?`pnpm_build_policy`:`dependency_install`}function registryInstallFailureMessage(e){return e===`pnpm_build_policy`?"Dependency installation stopped because pnpm requires build-script decisions. Run `pnpm approve-builds`, then retry the eve add command.":`Dependency installation failed. Retry the eve add command in a terminal for details.`}async function installRegistryItemTransaction(e){let t=await snapshotRegistryInstall(e.appRoot,e.registryItem);try{await e.install()}catch(n){let r=await rollbackRegistryInstall(e.appRoot,t),i=registryInstallFailureCode(n),a=registryInstallFailureMessage(i);if(e.nonInteractive){let t={version:1,type:`failed`,item:e.item,completedItems:[],message:a,failureCode:i,rolledBack:r.restored};r.changed.length>0&&(t.changed=r.changed),e.logger.log(serializeHeadlessSetupEvent(t))}throw Error(a,{cause:n})}}export{installRegistryItemTransaction,registryInstallFailureCode,registryInstallFailureMessage,rollbackRegistryInstall,snapshotRegistryInstall};