import{createRequire} from"node:module";const require=createRequire(import.meta.url); import{a as x}from"./chunk-QAJKDPNS.js";import{a as D,b as ee}from"./chunk-KSBHIW2H.js";import{c as K}from"./chunk-XEKXHOPO.js";import{a as V,b as Y,c as z}from"./chunk-QAYAQNCG.js";var le={name:"grep",summary:"print lines that match patterns",usage:"grep [OPTION]... PATTERN [FILE]...",options:["-E, --extended-regexp PATTERN is an extended regular expression","-P, --perl-regexp PATTERN is a Perl regular expression","-F, --fixed-strings PATTERN is a set of newline-separated strings","-i, --ignore-case ignore case distinctions","-v, --invert-match select non-matching lines","-w, --word-regexp match only whole words","-x, --line-regexp match only whole lines","-c, --count print only a count of matching lines","-l, --files-with-matches print only names of files with matches","-L, --files-without-match print names of files with no matches","-m NUM, --max-count=NUM stop after NUM matches","-n, --line-number print line number with output lines","-h, --no-filename suppress the file name prefix on output","-o, --only-matching show only nonempty parts of lines that match","-q, --quiet, --silent suppress all normal output","-r, -R, --recursive search directories recursively","-A NUM print NUM lines of trailing context","-B NUM print NUM lines of leading context","-C NUM print NUM lines of context","-e PATTERN use PATTERN for matching"," --include=GLOB search only files matching GLOB"," --exclude=GLOB skip files matching GLOB"," --exclude-dir=DIR skip directories matching DIR"," --help display this help and exit"]},te={name:"grep",async execute(t,s){if(Y(t))return V(le);let l=!1,f=!1,u=!1,p=!1,m=!1,a=!1,n=!1,c=!1,y=!1,w=!1,h=!1,d=!1,F=!1,C=!1,I=!1,W=0,P=0,T=0,A=[],E=[],U=[],R=null,H=[];for(let r=0;r1||n)&&!C,j=50;for(let r=0;r{let g=o.path,e=g.split("/").pop()||g;if(E.length>0&&!n&&E.some(N=>x(e,N,{stripQuotes:!0}))||A.length>0&&!n&&!A.some(N=>x(e,N,{stripQuotes:!0})))return null;try{let N=s.fs.resolvePath(s.cwd,g),G=!1;if(o.isFile===void 0?G=(await s.fs.stat(N)).isDirectory:G=!o.isFile,G)return n?null:{error:`grep: ${g}: Is a directory `};let S=await s.fs.readFile(N);if($&&!u){let J=$.ignoreCase?S.toLowerCase():S;if(!$.needles.some(q=>J.includes(q))){if(p){let q=Z?`${g}:0`:"0";return{file:g,result:{output:`${q} `,matched:!1,matchCount:0}}}return{file:g,result:{output:"",matched:!1,matchCount:0}}}}let re=D(S,O,{invertMatch:u,showLineNumbers:f,countOnly:p,filename:Z?g:"",onlyMatching:F,beforeContext:P,afterContext:T,maxCount:W,kResetGroup:Q,preFilter:$});return{file:g,result:re}}catch{return{error:`grep: ${g}: No such file or directory `}}}));for(let o of b){if(o===null)continue;if("error"in o&&o.error){k+=o.error,o.error.includes("Is a directory")||(X=!0);continue}if(!("file"in o)||!o.result)continue;let{file:g,result:e}=o;if(e.matched){if(_=!0,I)return{stdout:"",stderr:"",exitCode:0};m?M+=`${g} `:a||(M+=e.output)}else a?M+=`${g} `:p&&!m&&(M+=e.output)}}let B;return X?B=2:a?B=M.length>0?0:1:B=_?0:1,I?{stdout:"",stderr:"",exitCode:B}:{stdout:M,stderr:k,exitCode:B}}},se=256;async function ne(t,s,l,f,u=0){if(u>=se)return;let p=l.fs.resolvePath(l.cwd,t);try{if(!(await l.fs.stat(p)).isDirectory){let n=t.split("/").pop()||"";if(s){let c=s.replace(/^\//,"");x(n,c,{stripQuotes:!0})&&f.push(t)}return}let a=await l.fs.readdir(p);for(let n of a){let c=t==="."?n:`${t}/${n}`,y=l.fs.resolvePath(l.cwd,c);if((await l.fs.stat(y)).isDirectory)await ne(c,s,l,f,u+1);else if(s){let h=s.replace(/^\//,"");x(n,h,{stripQuotes:!0})&&f.push(c)}}}catch{}}async function oe(t,s){let l=[],f=t.lastIndexOf("/"),u,p;if(f===-1?(u=s.cwd,p=t):(u=t.slice(0,f)||"/",p=t.slice(f+1)),t.includes("**")){let a=[],n=t.split("**"),c=n[0].replace(/\/$/,"")||".",y=n[1]||"";return await ne(c,y,s,a),a.map(w=>({path:w}))}let m=s.fs.resolvePath(s.cwd,u);try{if(s.fs.readdirWithFileTypes){let a=await s.fs.readdirWithFileTypes(m);for(let n of a)if(x(n.name,p,{stripQuotes:!0})){let c=f===-1?n.name:`${u}/${n.name}`;l.push({path:c,isFile:n.isFile})}}else{let a=await s.fs.readdir(m);for(let n of a)if(x(n,p,{stripQuotes:!0})){let c=f===-1?n:`${u}/${n}`;l.push({path:c})}}}catch{}return l.sort((a,n)=>a.path.localeCompare(n.path))}async function L(t,s,l=[],f=[],u=[],p,m=0){if(m>=se)return[];let a=s.fs.resolvePath(s.cwd,t),n=[];try{let c,y;if(p!==void 0)c=p,y=!p;else{let h=await s.fs.stat(a);c=h.isFile,y=h.isDirectory}if(c){let h=t.split("/").pop()||t;return f.length>0&&f.some(d=>x(h,d,{stripQuotes:!0}))?[]:l.length>0&&!l.some(d=>x(h,d,{stripQuotes:!0}))?[]:[{path:t,isFile:!0}]}if(!y)return[];let w=t.split("/").pop()||t;if(u.length>0&&u.some(h=>x(w,h,{stripQuotes:!0})))return[];if(s.fs.readdirWithFileTypes){let h=await s.fs.readdirWithFileTypes(a);for(let d of h){if(d.name.startsWith("."))continue;let F=t==="."?d.name:`${t}/${d.name}`,C=await L(F,s,l,f,u,d.isFile,m+1);n.push(...C)}}else{let h=await s.fs.readdir(a);for(let d of h){if(d.startsWith("."))continue;let F=t==="."?d:`${t}/${d}`,C=await L(F,s,l,f,u,void 0,m+1);n.push(...C)}}}catch{}return n}var pe={name:"fgrep",async execute(t,s){return te.execute(["-F",...t],s)}},he={name:"egrep",async execute(t,s){return te.execute(["-E",...t],s)}},de={name:"grep",flags:[{flag:"-E",type:"boolean"},{flag:"-F",type:"boolean"},{flag:"-P",type:"boolean"},{flag:"-i",type:"boolean"},{flag:"-v",type:"boolean"},{flag:"-w",type:"boolean"},{flag:"-x",type:"boolean"},{flag:"-c",type:"boolean"},{flag:"-l",type:"boolean"},{flag:"-L",type:"boolean"},{flag:"-n",type:"boolean"},{flag:"-h",type:"boolean"},{flag:"-o",type:"boolean"},{flag:"-q",type:"boolean"},{flag:"-r",type:"boolean"},{flag:"-m",type:"value",valueHint:"number"},{flag:"-A",type:"value",valueHint:"number"},{flag:"-B",type:"value",valueHint:"number"},{flag:"-C",type:"value",valueHint:"number"},{flag:"-e",type:"value",valueHint:"pattern"}],stdinType:"text",needsArgs:!0},ge={name:"fgrep",flags:[],stdinType:"text",needsArgs:!0},me={name:"egrep",flags:[],stdinType:"text",needsArgs:!0};export{te as a,pe as b,he as c,de as d,ge as e,me as f};