-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind-InProject.ps1
More file actions
229 lines (194 loc) · 6.22 KB
/
Copy pathFind-InProject.ps1
File metadata and controls
229 lines (194 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<#
.SYNOPSIS
Search across project files (respects common ignore patterns).
.DESCRIPTION
Searches for patterns in project files, automatically excluding
node_modules, vendor, .git, and other common directories.
.PARAMETER Pattern
The search pattern (regex supported).
.PARAMETER Type
File type filter: php, js, ts, css, html, json, perl, py, etc.
Can specify multiple types separated by comma.
.PARAMETER Path
Directory to search in (defaults to current directory).
.PARAMETER CaseSensitive
Make search case-sensitive.
.PARAMETER AsJson
Output as JSON for MCP tools.
.PARAMETER Context
Number of context lines to show around matches.
.EXAMPLE
search "function login" # Search all files
search "TODO" -Type php # Only PHP files
search "import" -Type js,ts # JS/TS files
search "pattern" -AsJson # JSON output for AI
#>
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true)]
[string]$Pattern,
[Parameter(Position = 1)]
[string]$Type,
[string]$Path = '.',
[switch]$CaseSensitive,
[switch]$AsJson
)
# Resolve path
$searchPath = Resolve-Path $Path -ErrorAction SilentlyContinue
if (-not $searchPath) {
Write-Host "Path not found: $Path" -ForegroundColor Red
exit 1
}
# Directories to exclude
$excludeDirs = @(
'node_modules', 'vendor', '.git', '__pycache__', '.idea', '.vscode',
'venv', '.venv', 'dist', 'build', 'coverage', '.next', '.nuxt',
'storage/framework', 'bootstrap/cache', 'target', 'bin', 'obj'
)
# File extensions by type
$typeExtensions = @{
'php' = @('*.php')
'js' = @('*.js', '*.jsx', '*.mjs')
'ts' = @('*.ts', '*.tsx')
'css' = @('*.css', '*.scss', '*.sass', '*.less')
'html' = @('*.html', '*.htm', '*.blade.php', '*.twig')
'json' = @('*.json')
'perl' = @('*.pl', '*.pm', '*.t')
'py' = @('*.py')
'python' = @('*.py')
'md' = @('*.md', '*.markdown')
'sql' = @('*.sql')
'yaml' = @('*.yaml', '*.yml')
'xml' = @('*.xml')
'config' = @('*.json', '*.yaml', '*.yml', '*.xml', '*.ini', '*.env*')
}
# Build include patterns
$includePatterns = @()
if ($Type) {
$types = $Type.ToLower() -split '[,\s]+'
foreach ($t in $types) {
if ($typeExtensions.ContainsKey($t)) {
$includePatterns += $typeExtensions[$t]
} else {
$includePatterns += "*.$t"
}
}
}
# Get files to search
$files = Get-ChildItem -Path $searchPath -Recurse -File -ErrorAction SilentlyContinue | Where-Object {
$filePath = $_.FullName
# Exclude directories
foreach ($excludeDir in $excludeDirs) {
if ($filePath -match "[\\/]$excludeDir[\\/]") {
return $false
}
}
# Include by type if specified
if ($includePatterns.Count -gt 0) {
$matched = $false
foreach ($incPattern in $includePatterns) {
if ($_.Name -like $incPattern) {
$matched = $true
break
}
}
return $matched
}
# Exclude binary/large files
$excludeExtensions = @('.exe', '.dll', '.zip', '.tar', '.gz', '.png', '.jpg', '.gif', '.ico', '.woff', '.woff2', '.ttf', '.eot', '.mp3', '.mp4', '.pdf')
if ($excludeExtensions -contains $_.Extension.ToLower()) {
return $false
}
return $true
}
# Search in files
$results = @()
$totalMatches = 0
foreach ($file in $files) {
try {
$content = Get-Content $file.FullName -ErrorAction Stop
$lineNum = 0
$fileMatches = @()
foreach ($line in $content) {
$lineNum++
$isMatch = if ($CaseSensitive) {
$line -cmatch $Pattern
} else {
$line -match $Pattern
}
if ($isMatch) {
$fileMatches += [ordered]@{
line = $lineNum
content = $line.Trim()
}
$totalMatches++
}
}
if ($fileMatches.Count -gt 0) {
$relativePath = $file.FullName.Replace($searchPath.Path, '').TrimStart('\', '/')
$results += [ordered]@{
file = $relativePath
matches = $fileMatches
count = $fileMatches.Count
}
}
} catch {
# Skip files that can't be read
}
}
# JSON output
if ($AsJson) {
@{
pattern = $Pattern
totalMatches = $totalMatches
fileCount = $results.Count
results = $results
} | ConvertTo-Json -Depth 10
exit 0
}
# Pretty output
Write-Host ""
Write-Host "Search: " -NoNewline -ForegroundColor Cyan
Write-Host $Pattern -ForegroundColor Yellow
if ($Type) {
Write-Host "Type: " -NoNewline -ForegroundColor Cyan
Write-Host $Type -ForegroundColor Yellow
}
Write-Host ""
if ($results.Count -eq 0) {
Write-Host "No matches found." -ForegroundColor DarkGray
Write-Host ""
exit 0
}
foreach ($result in $results) {
Write-Host $result.file -ForegroundColor Green
foreach ($match in $result.matches | Select-Object -First 5) {
Write-Host " " -NoNewline
Write-Host "$($match.line.ToString().PadLeft(4)):" -NoNewline -ForegroundColor DarkGray
# Highlight match in line
$line = $match.content
if ($line -match "($Pattern)") {
$parts = $line -split "($Pattern)"
foreach ($part in $parts) {
if ($part -match "^$Pattern$") {
Write-Host $part -NoNewline -ForegroundColor Black -BackgroundColor Yellow
} else {
Write-Host $part -NoNewline -ForegroundColor White
}
}
Write-Host ""
} else {
Write-Host " $line" -ForegroundColor White
}
}
if ($result.matches.Count -gt 5) {
Write-Host " ... $($result.matches.Count - 5) more matches" -ForegroundColor DarkGray
}
Write-Host ""
}
Write-Host "Found " -NoNewline -ForegroundColor Cyan
Write-Host $totalMatches -NoNewline -ForegroundColor Yellow
Write-Host " matches in " -NoNewline -ForegroundColor Cyan
Write-Host $results.Count -NoNewline -ForegroundColor Yellow
Write-Host " files" -ForegroundColor Cyan
Write-Host ""