import{resolveInstalledPackageInfo}from"#internal/application/package.js";import{constants}from"node:fs";import{delimiter,join,relative,sep}from"node:path";import{access,readdir,stat,writeFile}from"node:fs/promises";import{LOCAL_WORKFLOW_WORLD_DATA_DIRECTORY_RELATIVE_PATH}from"#internal/workflow/local-world-data-directory.js";import{captureVercel,resolveVercelInvocation}from"#setup/primitives/run-vercel.js";function createDevDiagnosticDump(e,t,n={}){let r=t.replace(/\.log$/,`.dump`),o=n.now??(()=>new Date),s=o().getTime(),c,l,u=Promise.resolve(),d=!1,f=!1,write=()=>{if(f||d)return;let e=formatDump({at:o(),durationMs:o().getTime()-s,environment:c,stats:l});u=u.then(()=>writeFile(r,e,{encoding:`utf8`,mode:384})).catch(()=>{d=!0})};return write(),(n.environment??(()=>collectDevEnvironmentInfo(e)))().then(e=>{c=e,write()}).catch(()=>{}),{path:r,displayPath:relative(e,r).split(sep).join(`/`),updateSessionStats(e){l=e,write()},async close(){f||(write(),f=!0,await u)}}}function formatDump(e){let t={updatedAt:e.at.toISOString(),durationMs:e.durationMs,environment:e.environment??null,session:e.stats??null};return`${JSON.stringify(t,null,2)}\n`}async function collectDevEnvironmentInfo(t){let[n,r]=await Promise.all([detectVercelCli(t),measureSessionsDirectory(t)]),i={eveVersion:resolveInstalledPackageInfo().version,nodeVersion:process.version,platform:`${process.platform} ${process.arch}`,...n};return r!==void 0&&(i.sessionsDirectory=r),i}async function detectVercelCli(e){let t=resolveVercelInvocation(e),[n,r]=await Promise.all([captureVercel([`--version`],{cwd:e,nonInteractive:!0,timeoutMs:5e3}),t.command===`vercel`?findOnPath(`vercel`):Promise.resolve(t.command)]),i=n.ok?n.stdout:`${n.failure.stdout}\n${n.failure.stderr}`,a=/(\d+\.\d+\.\d+\S*)/.exec(i)?.[1],o={};return a!==void 0&&(o.vercelCliVersion=a),r!==void 0&&(o.vercelCliPath=r),o}async function findOnPath(e){let i=(process.env.PATH??``).split(delimiter).filter(e=>e.length>0);for(let n of i){let i=join(n,e);try{return await access(i,constants.X_OK),i}catch{}}}async function measureSessionsDirectory(e){let t=join(e,LOCAL_WORKFLOW_WORLD_DATA_DIRECTORY_RELATIVE_PATH),n;try{n=await readdir(t,{recursive:!0,withFileTypes:!0})}catch{return}let i=0,a=0;for(let e of n)if(e.isFile()){i+=1;try{a+=(await stat(join(e.parentPath,e.name))).size}catch{}}return{path:LOCAL_WORKFLOW_WORLD_DATA_DIRECTORY_RELATIVE_PATH,files:i,bytes:a}}export{collectDevEnvironmentInfo,createDevDiagnosticDump,measureSessionsDirectory};