import"./chunk-NUFRM6SI.js";import{a as w}from"./chunk-HWKDQ44K.js";import{a as D,b as $}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";var g={name:"tree",summary:"list contents of directories in a tree-like format",usage:"tree [OPTION]... [DIRECTORY]...",options:["-a          include hidden files","-d          list directories only","-L LEVEL    limit depth of directory tree","-f          print full path prefix for each file","    --help  display this help and exit"]},T={showHidden:{short:"a",type:"boolean"},directoriesOnly:{short:"d",type:"boolean"},fullPath:{short:"f",type:"boolean"},maxDepth:{short:"L",type:"number"}},E={name:"tree",async execute(i,o){if($(i))return D(g);let s=w("tree",i,T);if(!s.ok)return s.error;let f={showHidden:s.result.flags.showHidden,directoriesOnly:s.result.flags.directoriesOnly,maxDepth:s.result.flags.maxDepth??null,fullPath:s.result.flags.fullPath},d=s.result.positional;d.length===0&&d.push(".");let t="",l="",a=0,n=0;for(let e of d){let r=await b(o,e,f,"",0);t+=r.output,l+=r.stderr,a+=r.dirCount,n+=r.fileCount}return t+=`
${a} director${a===1?"y":"ies"}`,f.directoriesOnly||(t+=`, ${n} file${n===1?"":"s"}`),t+=`
`,{stdout:t,stderr:l,exitCode:l?1:0}}};async function b(i,o,s,f,d){let t={output:"",stderr:"",dirCount:0,fileCount:0},l=i.fs.resolvePath(i.cwd,o);try{if(!(await i.fs.stat(l)).isDirectory)return t.output=`${o}
`,t.fileCount=1,t}catch{return t.stderr=`tree: ${o}: No such file or directory
`,t}if(t.output=`${o}
`,s.maxDepth!==null&&d>=s.maxDepth)return t;try{let a=[];if(i.fs.readdirWithFileTypes)a=(await i.fs.readdirWithFileTypes(l)).map(r=>({name:r.name,isDirectory:r.isDirectory}));else{let e=await i.fs.readdir(l);for(let r=0;r<e.length;r+=100){let h=e.slice(r,r+100),u=await Promise.all(h.map(async c=>{let m=l==="/"?`/${c}`:`${l}/${c}`;try{let y=await i.fs.stat(m);return{name:c,isDirectory:y.isDirectory}}catch{return null}}));a.push(...u.filter(c=>c!==null))}}let n=a.filter(e=>!(!s.showHidden&&e.name.startsWith(".")||s.directoriesOnly&&!e.isDirectory));n.sort((e,r)=>e.name.localeCompare(r.name));for(let e=0;e<n.length;e++){let r=n[e],h=l==="/"?`/${r.name}`:`${l}/${r.name}`,u=e===n.length-1,c=u?"`-- ":"|-- ",m=f+(u?"    ":"|   ");if(r.isDirectory){t.dirCount++;let y=s.fullPath?h:r.name;if(t.output+=`${f+c+y}
`,s.maxDepth===null||d+1<s.maxDepth){let C=await P(i,h,s,m,d+1);t.output+=C.output,t.dirCount+=C.dirCount,t.fileCount+=C.fileCount}}else{t.fileCount++;let y=s.fullPath?h:r.name;t.output+=`${f+c+y}
`}}}catch{t.stderr=`tree: ${o}: Permission denied
`}return t}async function P(i,o,s,f,d){let t={output:"",stderr:"",dirCount:0,fileCount:0};if(s.maxDepth!==null&&d>=s.maxDepth)return t;try{let l=[];if(i.fs.readdirWithFileTypes)l=(await i.fs.readdirWithFileTypes(o)).map(e=>({name:e.name,isDirectory:e.isDirectory}));else{let n=await i.fs.readdir(o);for(let e=0;e<n.length;e+=100){let r=n.slice(e,e+100),h=await Promise.all(r.map(async u=>{let c=o==="/"?`/${u}`:`${o}/${u}`;try{let m=await i.fs.stat(c);return{name:u,isDirectory:m.isDirectory}}catch{return null}}));l.push(...h.filter(u=>u!==null))}}let a=l.filter(n=>!(!s.showHidden&&n.name.startsWith(".")||s.directoriesOnly&&!n.isDirectory));a.sort((n,e)=>n.name.localeCompare(e.name));for(let n=0;n<a.length;n++){let e=a[n],r=o==="/"?`/${e.name}`:`${o}/${e.name}`,h=n===a.length-1,u=h?"`-- ":"|-- ",c=f+(h?"    ":"|   ");if(e.isDirectory){t.dirCount++;let m=s.fullPath?r:e.name;t.output+=`${f+u+m}
`;let y=await P(i,r,s,c,d+1);t.output+=y.output,t.dirCount+=y.dirCount,t.fileCount+=y.fileCount}else{t.fileCount++;let m=s.fullPath?r:e.name;t.output+=`${f+u+m}
`}}}catch{}return t}export{E as treeCommand};
