# === CONFIG === $RootPath = "C:\sources\Project\Angular" $DryRun = $false # true = report; false = actually modify files Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' # === REGEXS (literal '...') === $reComponent = [regex]'@Component\s*\(\s*\{[\s\S]*?\}\s*\)' $reImports = [regex]'imports\s*:\s*\[(?[\s\S]*?)\]' $reTemplateUrl = [regex]'templateUrl\s*:\s*(["''])(?[\s\S]*?)(? $($mTpl.Groups['url'].Value)"; $skTplNF++; continue } $tpl = Get-Content -LiteralPath $tplPath -Raw $counts = Get-DirectiveUsageCounts -TemplateContent $tpl -Directives $present $logParts = @() foreach ($n in $present) { $logParts += "$n=$($counts[$n])" } Write-Host ("[INFO] {0} -> {1} | template: {2}" -f $f.FullName, ($logParts -join ', '), $tplPath) -ForegroundColor DarkGray $unused = @($present | Where-Object { ($counts.ContainsKey($_) -and $counts[$_] -eq 0) }) if ($unused.Count -eq 0) { Write-Host ("[IGNORED] {0}" -f $f.FullName) -ForegroundColor DarkYellow continue } $newArr = Prune-ImportsArray -arr $importsRaw -drop $unused $newBlock = $reImports.Replace($block, { param($mm) "imports: [`n $newArr`n]" }) $text = $text.Remove($m.Index, $m.Length).Insert($m.Index, $newBlock) $fileChanged = $true Write-Host ("[CLEANED] {0} -> {1}" -f $f.FullName, ($unused -join ', ')) -ForegroundColor Green } if ($fileChanged) { $modified++ if (-not $DryRun) { Set-Content -LiteralPath $f.FullName -Value $text -NoNewline } } } "`nModified components : $modified" "Skipped (no templateUrl) : $skNoTpl" "Skipped (template not found) : $skTplNF" "Skipped (no imports[]) : $skNoImports`n" if (-not $DryRun) { Write-Host "Running: npm run clean ..."; Set-Location $RootPath; npm run clean } Pause