#!/usr/bin/env node
import{a as y}from"./chunk-GIFF636B.js";import{a as S,b as z}from"./chunk-G43H2WGH.js";import"./chunk-SE4C7FJY.js";import{a as _,b as j,c as G}from"./chunk-GTNBSMZR.js";import"./chunk-KGOUQS5A.js";var te={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"]},J={name:"grep",async execute(t,s){if(j(t))return _(te);let o=!1,f=!1,c=!1,h=!1,m=!1,l=!1,i=!1,d=!1,w=!1,u=!1,p=!1,T=!1,x=!1,q=!1,B=!1,M=0,R=0,F=0,W=[],A=[],Q=[],N=null,I=[];for(let r=0;r<t.length;r++){let n=t[r];if(n.startsWith("-")&&n!=="-"){if(n==="-e"&&r+1<t.length){N=t[++r];continue}if(n.startsWith("--include=")){W.push(n.slice(10));continue}if(n.startsWith("--exclude=")){A.push(n.slice(10));continue}if(n.startsWith("--exclude-dir=")){Q.push(n.slice(14));continue}if(n.startsWith("--max-count=")){M=parseInt(n.slice(12),10);continue}let C=n.match(/^-m(\d+)$/);if(C){M=parseInt(C[1],10);continue}if(n==="-m"&&r+1<t.length){M=parseInt(t[++r],10);continue}let a=n.match(/^-([ABC])(\d+)$/);if(a){let e=parseInt(a[2],10);a[1]==="A"?F=e:a[1]==="B"?R=e:a[1]==="C"&&(R=e,F=e);continue}if((n==="-A"||n==="-B"||n==="-C")&&r+1<t.length){let e=parseInt(t[++r],10);n==="-A"?F=e:n==="-B"?R=e:(R=e,F=e);continue}let g=n.startsWith("--")?[n]:n.slice(1).split("");for(let e of g)if(e==="i"||e==="--ignore-case")o=!0;else if(e==="n"||e==="--line-number")f=!0;else if(e==="v"||e==="--invert-match")c=!0;else if(e==="c"||e==="--count")h=!0;else if(e==="l"||e==="--files-with-matches")m=!0;else if(e==="L"||e==="--files-without-match")l=!0;else if(e==="r"||e==="R"||e==="--recursive")i=!0;else if(e==="w"||e==="--word-regexp")d=!0;else if(e==="x"||e==="--line-regexp")w=!0;else if(e==="E"||e==="--extended-regexp")u=!0;else if(e==="P"||e==="--perl-regexp")p=!0;else if(e==="F"||e==="--fixed-strings")T=!0;else if(e==="o"||e==="--only-matching")x=!0;else if(e==="h"||e==="--no-filename")q=!0;else if(e==="q"||e==="--quiet"||e==="--silent")B=!0;else{if(e.startsWith("--"))return G("grep",e);if(e.length===1)return G("grep",`-${e}`)}}else N===null?N=n:I.push(n)}if(N===null)return{stdout:"",stderr:`grep: missing pattern
`,exitCode:2};let V=T?"fixed":u?"extended":p?"perl":"basic",U,b;try{let r=z(N,{mode:V,ignoreCase:o,wholeWord:d,lineRegexp:w});U=r.regex,b=r.kResetGroup}catch{return{stdout:"",stderr:`grep: invalid regular expression: ${N}
`,exitCode:2}}if(I.length===0&&s.stdin!==void 0){let r=S(s.stdin,U,{invertMatch:c,showLineNumbers:f,countOnly:h,filename:"",onlyMatching:x,beforeContext:R,afterContext:F,maxCount:M,kResetGroup:b});return B?{stdout:"",stderr:"",exitCode:r.matched?0:1}:{stdout:r.output,stderr:"",exitCode:r.matched?0:1}}if(I.length===0)return{stdout:"",stderr:`grep: no input files
`,exitCode:2};let $="",D="",H=!1,k=!1,P=[];for(let r of I)if(r.includes("*")||r.includes("?")||r.includes("[")){let n=await se(r,s);if(i)for(let C of n){let a=await O(C.path,s,W,A,Q,C.isFile);P.push(...a)}else P.push(...n)}else if(i){let n=await O(r,s,W,A,Q);P.push(...n)}else P.push({path:r});let X=(P.length>1||i)&&!q,Z=50;for(let r=0;r<P.length;r+=Z){let n=P.slice(r,r+Z),C=await Promise.all(n.map(async a=>{let g=a.path,e=g.split("/").pop()||g;if(A.length>0&&!i&&A.some(v=>y(e,v,{stripQuotes:!0}))||W.length>0&&!i&&!W.some(v=>y(e,v,{stripQuotes:!0})))return null;try{let v=s.fs.resolvePath(s.cwd,g),L=!1;if(a.isFile===void 0?L=(await s.fs.stat(v)).isDirectory:L=!a.isFile,L)return i?null:{error:`grep: ${g}: Is a directory
`};let Y=await s.fs.readFile(v),ee=S(Y,U,{invertMatch:c,showLineNumbers:f,countOnly:h,filename:X?g:"",onlyMatching:x,beforeContext:R,afterContext:F,maxCount:M,kResetGroup:b});return{file:g,result:ee}}catch{return{error:`grep: ${g}: No such file or directory
`}}}));for(let a of C){if(a===null)continue;if("error"in a&&a.error){D+=a.error,a.error.includes("Is a directory")||(k=!0);continue}if(!("file"in a)||!a.result)continue;let{file:g,result:e}=a;if(e.matched){if(H=!0,B)return{stdout:"",stderr:"",exitCode:0};m?$+=`${g}
`:l||($+=e.output)}else l?$+=`${g}
`:h&&!m&&($+=e.output)}}let E;return k?E=2:l?E=$.length>0?0:1:E=H?0:1,B?{stdout:"",stderr:"",exitCode:E}:{stdout:$,stderr:D,exitCode:E}}};async function K(t,s,o,f){let c=o.fs.resolvePath(o.cwd,t);try{if(!(await o.fs.stat(c)).isDirectory){let l=t.split("/").pop()||"";if(s){let i=s.replace(/^\//,"");y(l,i,{stripQuotes:!0})&&f.push(t)}return}let m=await o.fs.readdir(c);for(let l of m){let i=t==="."?l:`${t}/${l}`,d=o.fs.resolvePath(o.cwd,i);if((await o.fs.stat(d)).isDirectory)await K(i,s,o,f);else if(s){let u=s.replace(/^\//,"");y(l,u,{stripQuotes:!0})&&f.push(i)}}}catch{}}async function se(t,s){let o=[],f=t.lastIndexOf("/"),c,h;if(f===-1?(c=s.cwd,h=t):(c=t.slice(0,f)||"/",h=t.slice(f+1)),t.includes("**")){let l=[],i=t.split("**"),d=i[0].replace(/\/$/,"")||".",w=i[1]||"";return await K(d,w,s,l),l.map(u=>({path:u}))}let m=s.fs.resolvePath(s.cwd,c);try{if(s.fs.readdirWithFileTypes){let l=await s.fs.readdirWithFileTypes(m);for(let i of l)if(y(i.name,h,{stripQuotes:!0})){let d=f===-1?i.name:`${c}/${i.name}`;o.push({path:d,isFile:i.isFile})}}else{let l=await s.fs.readdir(m);for(let i of l)if(y(i,h,{stripQuotes:!0})){let d=f===-1?i:`${c}/${i}`;o.push({path:d})}}}catch{}return o.sort((l,i)=>l.path.localeCompare(i.path))}async function O(t,s,o=[],f=[],c=[],h){let m=s.fs.resolvePath(s.cwd,t),l=[];try{let i,d;if(h!==void 0)i=h,d=!h;else{let u=await s.fs.stat(m);i=u.isFile,d=u.isDirectory}if(i){let u=t.split("/").pop()||t;return f.length>0&&f.some(p=>y(u,p,{stripQuotes:!0}))?[]:o.length>0&&!o.some(p=>y(u,p,{stripQuotes:!0}))?[]:[{path:t,isFile:!0}]}if(!d)return[];let w=t.split("/").pop()||t;if(c.length>0&&c.some(u=>y(w,u,{stripQuotes:!0})))return[];if(s.fs.readdirWithFileTypes){let u=await s.fs.readdirWithFileTypes(m);for(let p of u){if(p.name.startsWith("."))continue;let T=t==="."?p.name:`${t}/${p.name}`,x=await O(T,s,o,f,c,p.isFile);l.push(...x)}}else{let u=await s.fs.readdir(m);for(let p of u){if(p.startsWith("."))continue;let T=t==="."?p:`${t}/${p}`,x=await O(T,s,o,f,c);l.push(...x)}}}catch{}return l}var oe={name:"fgrep",async execute(t,s){return J.execute(["-F",...t],s)}},ae={name:"egrep",async execute(t,s){return J.execute(["-E",...t],s)}};export{ae as egrepCommand,oe as fgrepCommand,J as grepCommand};
