@@ -25,103 +25,107 @@ function Normalize-Config {
2525 Write-Host " Normalizing config file: $file "
2626 $ext = [System.IO.Path ]::GetExtension($file ).TrimStart(' .' )
2727
28- if ($ext -eq ' xml' ) {
29- Normalize- XmlFile $file
30- return
31- }
32-
3328 switch ($ext ) {
3429 { $_ -in @ (' yaml' , ' yml' ) } {
35- # For YAML files, preserve structure and sort within sections
36- $content = Get-Content $file
37- $output = @ ()
38- $currentSection = " "
39- $sectionContent = @ ()
40-
41- foreach ($line in $content ) {
42- $line = $line.Trim ()
43- if ($line -match ' ^(\w+):$' ) {
44- # If we have a previous section, sort and add its content
45- if ($currentSection -and $sectionContent.Count -gt 0 ) {
46- $output += $currentSection
47- $output += ($sectionContent | Sort-Object )
48- $sectionContent = @ ()
49- }
50- $currentSection = $line
51- }
52- elseif ($line -match ' ^\s*-\s*' ) {
53- $sectionContent += $line
54- }
55- elseif ($line -match ' \S' ) {
56- $output += $line
57- }
58- }
59-
60- # Add the last section
61- if ($currentSection -and $sectionContent.Count -gt 0 ) {
62- $output += $currentSection
63- $output += ($sectionContent | Sort-Object )
64- }
65-
66- # Add empty line at the end if the original had one
67- if ($content [-1 ] -match ' ^\s*$' ) {
68- $output += " "
69- }
70-
71- $output
30+ Normalize- YamlConfig $file
7231 }
7332 { $_ -in @ (' mjs' , ' js' ) } {
74- # For JavaScript config files (like ESLint), sort the rule lines within the rules object
75- $content = Get-Content $file
76- $output = @ ()
77- $inRules = $false
78- $ruleLines = @ ()
79-
80- foreach ($line in $content ) {
81- if ($line -match ' rules: \{' ) {
82- $output += $line
83- $inRules = $true
84- } elseif ($inRules -and $line -match ' ^\s*\}' ) {
85- # Sort collected rule lines and add them
86- $output += ($ruleLines | Sort-Object )
87- $ruleLines = @ ()
88- $inRules = $false
89- $output += $line
90- } elseif ($inRules ) {
91- # Collect rule lines for sorting
92- $ruleLines += $line
93- } else {
94- $output += $line
95- }
96- }
97- $output
33+ Normalize- EslintConfig $file
34+ }
35+ ' toml' {
36+ Normalize- TomlConfig $file
9837 }
99- { $_ -in @ (' rc' , ' conf' , ' ini' , ' toml' , ' xml' ) } {
100- Get-Content $file | ForEach-Object {
101- if ($_ -match ' ^[^#].*=.*\[.*\]' ) {
102- # Handle TOML arrays like: rules = ["a", "b", "c"]
103- $parts = $_ -split ' ='
104- if ($parts [1 ] -match ' \[(.*)\]' ) {
105- $arrayContent = $matches [1 ]
106- $values = $arrayContent -split ' ,\s*' | Sort-Object
107- " $ ( $parts [0 ]) =[$ ( $values -join ' , ' ) ]"
108- } else { $_ }
109- } elseif ($_ -match ' ^[^#].*=.*,' ) {
110- # Handle simple comma-separated values
111- $parts = $_ -split ' ='
112- $values = $parts [1 ] -split ' ,' | Sort-Object
113- " $ ( $parts [0 ]) =$ ( $values -join ' ,' ) "
114- } else { $_ }
115- } | Sort-Object
38+ { $_ -in @ (' rc' , ' conf' , ' ini' ) } {
39+ Normalize- RcConfig $file
40+ }
41+ ' xml' {
42+ Normalize- XmlConfig $file
43+ }
44+ default {
45+ Get-Content $file | Sort-Object
46+ }
47+ }
48+ }
49+
50+ # Normalize YAML configuration files
51+ function Normalize-YamlConfig {
52+ param ([string ]$file )
53+
54+ # For YAML files, try to preserve structure - just return as-is for now
55+ # Complex YAML sorting can break structure, so we keep original order
56+ Get-Content $file
57+ }
58+
59+ # Normalize ESLint configuration files (.mjs/.js)
60+ function Normalize-EslintConfig {
61+ param ([string ]$file )
62+
63+ $content = Get-Content $file
64+ $output = @ ()
65+ $inRules = $false
66+ $ruleLines = @ ()
67+
68+ foreach ($line in $content ) {
69+ if ($line -match ' rules: \{' ) {
70+ $output += $line
71+ $inRules = $true
72+ } elseif ($inRules -and $line -match ' ^\s*\}' ) {
73+ # Sort collected rule lines and add them
74+ $output += ($ruleLines | Sort-Object )
75+ $ruleLines = @ ()
76+ $inRules = $false
77+ $output += $line
78+ } elseif ($inRules ) {
79+ # Collect rule lines for sorting
80+ $ruleLines += $line
81+ } else {
82+ $output += $line
11683 }
117- default { Get-Content $file | Sort-Object }
11884 }
85+ $output
11986}
12087
121- # Helper function to normalize XML files: strip leading spaces and sort <rule ref=.../> lines
122- function Normalize-XmlFile {
123- param ([string ]$Path )
124- $lines = Get-Content $Path
88+ # Normalize TOML configuration files
89+ function Normalize-TomlConfig {
90+ param ([string ]$file )
91+
92+ Get-Content $file | ForEach-Object {
93+ if ($_ -match ' ^[^#].*=.*\[.*\]' ) {
94+ # Handle TOML arrays like: rules = ["a", "b", "c"]
95+ $parts = $_ -split ' ='
96+ if ($parts [1 ] -match ' \[(.*)\]' ) {
97+ $arrayContent = $matches [1 ]
98+ $values = $arrayContent -split ' ,\s*' | Sort-Object
99+ " $ ( $parts [0 ]) =[$ ( $values -join ' , ' ) ]"
100+ } else { $_ }
101+ } elseif ($_ -match ' ^[^#].*=.*,' ) {
102+ # Handle simple comma-separated values
103+ $parts = $_ -split ' ='
104+ $values = $parts [1 ] -split ' ,' | Sort-Object
105+ " $ ( $parts [0 ]) =$ ( $values -join ' ,' ) "
106+ } else { $_ }
107+ } | Sort-Object
108+ }
109+
110+ # Normalize RC/INI configuration files
111+ function Normalize-RcConfig {
112+ param ([string ]$file )
113+
114+ Get-Content $file | ForEach-Object {
115+ if ($_ -match ' ^[^#].*=.*,' ) {
116+ # Handle simple comma-separated values
117+ $parts = $_ -split ' ='
118+ $values = $parts [1 ] -split ' ,' | Sort-Object
119+ " $ ( $parts [0 ]) =$ ( $values -join ' ,' ) "
120+ } else { $_ }
121+ } | Sort-Object
122+ }
123+
124+ # Normalize XML configuration files
125+ function Normalize-XmlConfig {
126+ param ([string ]$file )
127+
128+ $lines = Get-Content $file
125129 $rules = @ ()
126130 $output = @ ()
127131 $endTag = $null
@@ -136,9 +140,12 @@ function Normalize-XmlFile {
136140 $output += $trimmed
137141 }
138142 }
143+
139144 $output + ($rules | Sort-Object ) + $endTag
140145}
141146
147+
148+
142149function Compare-Files {
143150 param (
144151 [string ]$expectedDir ,
0 commit comments