#!/usr/bin/env node
import"./chunk-OBH7XN5N.js";import{a as F}from"./chunk-JBABAK44.js";import{a as $,b as w}from"./chunk-GTNBSMZR.js";import"./chunk-KGOUQS5A.js";var R={name:"du",summary:"estimate file space usage",usage:"du [OPTION]... [FILE]...",options:["-a          write counts for all files, not just directories","-h          print sizes in human readable format","-s          display only a total for each argument","-c          produce a grand total","--max-depth=N  print total for directory only if N or fewer levels deep","    --help  display this help and exit"]},x={allFiles:{short:"a",type:"boolean"},humanReadable:{short:"h",type:"boolean"},summarize:{short:"s",type:"boolean"},grandTotal:{short:"c",type:"boolean"},maxDepth:{long:"max-depth",type:"number"}},H={name:"du",async execute(a,s){if(w(a))return $(R);let n=F("du",a,x);if(!n.ok)return n.error;let r={allFiles:n.result.flags.allFiles,humanReadable:n.result.flags.humanReadable,summarize:n.result.flags.summarize,grandTotal:n.result.flags.grandTotal,maxDepth:n.result.flags.maxDepth??null},c=n.result.positional;c.length===0&&c.push(".");let o="",m="",h=0;for(let l of c){let z=s.fs.resolvePath(s.cwd,l);try{await s.fs.stat(z);let p=await b(s,z,l,r,0);o+=p.output,h+=p.totalSize,m+=p.stderr}catch{m+=`du: cannot access '${l}': No such file or directory
`}}return r.grandTotal&&c.length>0&&(o+=`${D(h,r.humanReadable)}	total
`),{stdout:o,stderr:m,exitCode:m?1:0}}};async function b(a,s,n,r,c){let o={output:"",totalSize:0,stderr:""};try{let m=await a.fs.stat(s);if(!m.isDirectory)return o.totalSize=m.size,(r.allFiles||c===0)&&(o.output=D(m.size,r.humanReadable)+"	"+n+`
`),o;let h=0,l=[];if(a.fs.readdirWithFileTypes){let t=await a.fs.readdirWithFileTypes(s),i=t.filter(e=>e.isFile),f=t.filter(e=>e.isDirectory);for(let e=0;e<i.length;e+=100){let u=i.slice(e,e+100),g=await Promise.all(u.map(async d=>{let T=s==="/"?`/${d.name}`:`${s}/${d.name}`;try{let S=await a.fs.stat(T);return{name:d.name,isDirectory:!1,size:S.size}}catch{return{name:d.name,isDirectory:!1,size:0}}}));l.push(...g)}l.push(...f.map(e=>({name:e.name,isDirectory:!0})))}else{let t=await a.fs.readdir(s);for(let i=0;i<t.length;i+=100){let f=t.slice(i,i+100),e=await Promise.all(f.map(async u=>{let g=s==="/"?`/${u}`:`${s}/${u}`;try{let d=await a.fs.stat(g);return{name:u,isDirectory:d.isDirectory,size:d.isDirectory?void 0:d.size}}catch{return{name:u,isDirectory:!1,size:0}}}));l.push(...e)}}l.sort((t,i)=>t.name.localeCompare(i.name));let z=l.filter(t=>!t.isDirectory);for(let t of z){let i=t.size??0;if(h+=i,r.allFiles&&!r.summarize){let f=n==="."?t.name:`${n}/${t.name}`;o.output+=D(i,r.humanReadable)+"	"+f+`
`}}let p=l.filter(t=>t.isDirectory);for(let t=0;t<p.length;t+=100){let i=p.slice(t,t+100),f=await Promise.all(i.map(async e=>{let u=s==="/"?`/${e.name}`:`${s}/${e.name}`,g=n==="."?e.name:`${n}/${e.name}`;return{name:e.name,result:await b(a,u,g,r,c+1)}}));f.sort((e,u)=>e.name.localeCompare(u.name));for(let{result:e}of f)h+=e.totalSize,r.summarize||(r.maxDepth===null||c+1<=r.maxDepth)&&(o.output+=e.output)}o.totalSize=h,(r.summarize||r.maxDepth===null||c<=r.maxDepth)&&(o.output+=`${D(h,r.humanReadable)}	${n}
`)}catch{o.stderr=`du: cannot read directory '${n}': Permission denied
`}return o}function D(a,s){return s?a<1024?`${a}`:a<1024*1024?`${(a/1024).toFixed(1)}K`:a<1024*1024*1024?`${(a/(1024*1024)).toFixed(1)}M`:`${(a/(1024*1024*1024)).toFixed(1)}G`:String(Math.ceil(a/1024)||1)}export{H as duCommand};
