properties-comparator 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/DOCUMENTATION.md CHANGED
@@ -1,14 +1,19 @@
1
-
2
- # Node.js Properties File Comparison Utility
1
+ # Node.js YAML and Properties File Comparison Utility
3
2
 
4
3
  ## Overview
5
4
 
6
- This utility provides functionality to parse and compare properties files in the format of key-value pairs. It reads properties files, compares the values for each key across multiple files, and logs the results.
5
+ This utility parses and compares **.properties** and **.yml or .yaml (YAML)** files. It reads each file as key-value pairs, compares the values for each key across multiple files, and produces detailed comparison reports.
7
6
 
8
7
  ### Features:
9
- - Parse properties files into key-value objects.
10
- - Compare key values across multiple properties files.
11
- - Log detailed comparison results, including mismatches.
8
+ - Parse **.properties** files into key-value objects.
9
+ - Parse **.yml or .yaml** (YAML) files into flattened key-value objects (supports nested keys).
10
+ - Compare key values across multiple files (both **.properties** and **.yml or .yaml**).
11
+ - Generate reports in multiple formats:
12
+ - Console output with color-coded highlighting
13
+ - HTML reports with CSS styling
14
+ - Markdown reports
15
+ - Save reports to files or display in console
16
+ - Flexible command-line interface with options
12
17
 
13
18
  ---
14
19
 
@@ -16,7 +21,7 @@ This utility provides functionality to parse and compare properties files in the
16
21
 
17
22
  ### Prerequisites
18
23
  - Node.js installed on your system.
19
- - Properties files in a valid key-value format.
24
+ - Files in valid .properties or .yaml or .yml format.
20
25
 
21
26
  ---
22
27
 
@@ -26,7 +31,26 @@ This utility provides functionality to parse and compare properties files in the
26
31
  Run the script using Node.js with file paths provided as command-line arguments:
27
32
 
28
33
  ```bash
29
- node script.js <filePath1> <filePath2> [<filePath3> ...]
34
+ node compareUtility.js [options] <filePath1> <filePath2> [<filePath3> ...]
35
+ ```
36
+
37
+ ### Command-Line Options
38
+ ```
39
+ Options:
40
+ --format, -f <format> Output format: console, html, or markdown
41
+ --output, -o <file> Output file for html or markdown reports
42
+ ```
43
+
44
+ ### Examples
45
+ ```bash
46
+ # Basic comparison with console output
47
+ node compareUtility.js file1.properties file2.yaml
48
+
49
+ # Generate HTML report and save to output.html
50
+ node compareUtility.js --format html --output output.html file1.properties file2.yaml
51
+
52
+ # Short form options
53
+ node compareUtility.js -f markdown -o report.md file1.properties file2.properties file3.yml
30
54
  ```
31
55
 
32
56
  ### Input Format
@@ -37,6 +61,13 @@ key2=value2
37
61
  # Comments are ignored
38
62
  ```
39
63
 
64
+ YAML files should follow standard YAML format. Nested structures will be flattened with dot notation:
65
+ ```yaml
66
+ key1: value1
67
+ key2:
68
+ nestedKey: nestedValue # Will be accessible as "key2.nestedKey"
69
+ ```
70
+
40
71
  ---
41
72
 
42
73
  ## Functions
@@ -53,113 +84,220 @@ Parses a properties file and returns an object representation of the key-value p
53
84
 
54
85
  #### Example:
55
86
  ```javascript
56
- // Assuming properties file contains:
57
- // key1=value1
58
- // key2=value2
59
-
60
87
  const properties = parsePropertiesFile('/path/to/properties/file');
61
88
  console.log(properties);
62
89
  // Output: { key1: 'value1', key2: 'value2' }
63
90
  ```
64
91
 
65
- #### Implementation:
66
- - Reads the file content using `fs.readFileSync`.
67
- - Splits lines by `
68
- ` or `
69
- `.
70
- - Filters out empty lines and lines starting with `#`.
71
- - Processes valid lines into key-value pairs.
92
+ ---
93
+
94
+ ### `parseYamlFile(filePath)`
95
+
96
+ Parses a .yml or .yaml file into a flat key-value map using dot notation for nested keys.
97
+
98
+ #### Parameters:
99
+ - `filePath` (string): Path to the YAML file.
100
+
101
+ #### Returns:
102
+ - (Object): A flattened object containing key-value pairs from the YAML file.
103
+
104
+ #### Example:
105
+ ```javascript
106
+ const data = parseYamlFile('/path/to/yaml/file');
107
+ console.log(data);
108
+ // Output: { 'key1': 'value1', 'key2.nestedKey': 'nestedValue' }
109
+ ```
110
+
111
+ ---
112
+
113
+ ### `parseFile(filePath)`
114
+
115
+ Detects file extension and parses the file content into an object.
116
+
117
+ #### Parameters:
118
+ - `filePath` (string): Path to the file (.properties, .yml, or .yaml).
119
+
120
+ #### Returns:
121
+ - (Object): Parsed content as a key-value map, or {} if unsupported.
72
122
 
73
123
  ---
74
124
 
75
- ### `compareProperties(filePaths)`
125
+ ### `compareFileData(filePaths)`
76
126
 
77
- Compares properties across multiple properties files and logs the results.
127
+ Internal helper that compares key-value data from multiple files and returns structured results.
78
128
 
79
129
  #### Parameters:
80
- - `filePaths` (string[]): Array of file paths to be compared.
130
+ - `filePaths` (string[]): Array of file paths.
81
131
 
82
132
  #### Returns:
83
- - None. Outputs comparison results to the console.
133
+ - (Object): An object containing mismatch count and detailed comparison information.
134
+
135
+ ---
136
+
137
+ ### `checkIfAllValuesMatch(filePaths)`
138
+
139
+ Checks if all values match across the provided files.
84
140
 
85
- #### Behavior:
86
- - Parses each properties file into an object.
87
- - Collects all unique keys across files.
88
- - Compares values for each key across all files.
89
- - Logs whether values match or are mismatched. For mismatches, displays values for each file.
141
+ #### Parameters:
142
+ - `filePaths` (string[]): Array of file paths.
143
+
144
+ #### Returns:
145
+ - (boolean): True if all properties match across all files, false otherwise.
146
+
147
+ ---
148
+
149
+ ### `getMismatchFields(filePaths)`
150
+
151
+ Returns a list of fields (keys) that do not match across the provided files.
152
+
153
+ #### Parameters:
154
+ - `filePaths` (string[]): Array of file paths.
155
+
156
+ #### Returns:
157
+ - (string[]): List of mismatched keys.
158
+
159
+ ---
160
+
161
+ ### `generateHtmlReport(filePaths, comparisonData)`
162
+
163
+ Generates an HTML report for the comparison results.
164
+
165
+ #### Parameters:
166
+ - `filePaths` (string[]): Array of file paths that were compared.
167
+ - `comparisonData` (Object): The output from compareFileData function.
168
+
169
+ #### Returns:
170
+ - (string): HTML document as string.
171
+
172
+ ---
173
+
174
+ ### `generateMarkdownReport(filePaths, comparisonData)`
175
+
176
+ Generates a Markdown report for the comparison results.
177
+
178
+ #### Parameters:
179
+ - `filePaths` (string[]): Array of file paths that were compared.
180
+ - `comparisonData` (Object): The output from compareFileData function.
181
+
182
+ #### Returns:
183
+ - (string): Markdown document as string.
184
+
185
+ ---
186
+
187
+ ### `compareFiles(filePaths, options)`
188
+
189
+ Compares properties/keys across multiple files and generates a report based on options.
190
+
191
+ #### Parameters:
192
+ - `filePaths` (string[]): Array of file paths.
193
+ - `options` (Object): Options for comparison output.
194
+ - `format` (string): Output format ('console', 'html', or 'markdown').
195
+ - `outputFile` (string): Path to save the report (for html and markdown).
90
196
 
91
197
  #### Example:
92
- ```bash
93
- node script.js file1.properties file2.properties file3.properties
198
+ ```javascript
199
+ // Compare with console output
200
+ compareFiles(['file1.properties', 'file2.yaml']);
201
+
202
+ // Generate HTML report
203
+ compareFiles(['file1.properties', 'file2.yaml'], {
204
+ format: 'html',
205
+ outputFile: 'report.html'
206
+ });
94
207
  ```
95
208
 
96
- #### Output Example:
97
- ```text
98
- Comparing properties across files:
209
+ ---
99
210
 
100
- Key: key1 - Values match across all files.
101
- Key: key2 - Mismatched values:
102
- File 1: value1
103
- File 2: value2
104
- File 3: value3
105
- ```
211
+ ### `run()`
212
+
213
+ CLI entry point. Parses command-line arguments and runs the comparison.
106
214
 
107
215
  ---
108
216
 
109
- ### Runtime Input Handling
217
+ ## Output Formats
218
+
219
+ ### Console Output
220
+ The default output format provides:
221
+ - A table showing all keys and their values across files
222
+ - Highlighted mismatched rows for easy identification
223
+ - A summary of mismatched keys
224
+
225
+ ### HTML Report
226
+ Generates a visually appealing HTML report with:
227
+ - CSS styling
228
+ - Color-coded cells for matches and mismatches
229
+ - Summary section with quick statistics
230
+ - Responsive design
110
231
 
111
- The script expects file paths as command-line arguments:
112
- - Extracted from `process.argv`.
113
- - If no file paths are provided, it displays an error and exits:
114
- ```
115
- Please provide the paths to the properties files as command-line arguments.
116
- ```
117
- - Verifies the existence of all specified files. If any file is missing:
118
- ```
119
- One or more properties files are missing. Ensure all specified properties files exist.
120
- ```
232
+ ### Markdown Report
233
+ Creates a Markdown document with:
234
+ - File list with paths
235
+ - Comparison table
236
+ - Summary section with mismatched keys highlighted
121
237
 
122
238
  ---
123
239
 
124
240
  ## Error Handling
125
241
 
126
242
  - **No File Paths Provided**: Logs an error and exits.
243
+ - **Only One File Provided**: Logs an error about needing at least two files and exits.
127
244
  - **Missing Files**: Logs missing files and exits.
128
- - **Empty or Malformed Properties Files**: Skips invalid lines.
245
+ - **Unsupported File Extensions**: Logs a warning and treats as empty file.
246
+ - **Invalid YAML**: Logs error details and treats as empty file.
247
+ - **Invalid Format Option**: Falls back to console output with warning.
129
248
 
130
249
  ---
131
250
 
132
- ## Example Properties Files
251
+ ## Example Scenarios
133
252
 
134
- ### File 1 (`file1.properties`):
253
+ ### Basic Comparison
254
+
255
+ #### Command:
256
+ ```bash
257
+ node compareUtility.js file1.properties file2.properties
135
258
  ```
136
- key1=value1
137
- key2=value2
259
+
260
+ #### Output:
138
261
  ```
262
+ Comparing properties/keys across files:
139
263
 
140
- ### File 2 (`file2.properties`):
264
+ ┌─────────┬─────┬────────────┬────────────┐
265
+ │ (index) │ Key │ Matched │ ... │
266
+ ├─────────┼─────┼────────────┼────────────┤
267
+ │ 0 │ ... │ 'Yes' │ ... │
268
+ │ 1 │ ... │ 'No' │ ... │
269
+ └─────────┴─────┴────────────┴────────────┘
270
+
271
+ === Highlighted Mismatched Rows ===
272
+ Key: key2 | File 1: value2 | File 2: value3
273
+
274
+ === Summary ===
275
+ 1 key(s) have mismatched values.
276
+ Mismatched keys: key2
141
277
  ```
142
- key1=value1
143
- key2=value3
278
+
279
+ ### HTML Report Generation
280
+
281
+ #### Command:
282
+ ```bash
283
+ node compareUtility.js -f html -o report.html file1.properties file2.yaml
144
284
  ```
145
285
 
146
- ---
286
+ #### Output:
287
+ ```
288
+ HTML report saved to: report.html
289
+ ```
147
290
 
148
- ## Sample Execution
291
+ ### Markdown Report Generation
149
292
 
150
- ### Command:
293
+ #### Command:
151
294
  ```bash
152
- node script.js file1.properties file2.properties
295
+ node compareUtility.js -f markdown -o report.md file1.properties file2.properties
153
296
  ```
154
297
 
155
- ### Output:
156
- ```text
157
- Comparing properties across files:
158
-
159
- Key: key1 - Values match across all files.
160
- Key: key2 - Mismatched values:
161
- File 1: value2
162
- File 2: value3
298
+ #### Output:
299
+ ```
300
+ Markdown report saved to: report.md
163
301
  ```
164
302
 
165
303
  ---
@@ -167,20 +305,23 @@ Key: key2 - Mismatched values:
167
305
  ## Dependencies
168
306
 
169
307
  - `fs` module (Node.js File System)
308
+ - `path` module (Node.js Path)
309
+ - `js-yaml` module (YAML parsing)
310
+ - `chalk` module (Terminal styling)
170
311
 
171
312
  ---
172
313
 
173
314
  ## Limitations
174
315
 
175
- - Assumes properties are simple key-value pairs separated by `=`.
176
- - Ignores keys without a corresponding value.
177
- - Does not support multi-line properties.
316
+ - Assumes properties are simple key-value pairs separated by `=`
317
+ - Does not support multi-line properties in .properties files
318
+ - Flattens all nested YAML structures to dot notation
178
319
 
179
320
  ---
180
321
 
181
322
  ## License
182
323
 
183
- This script is licensed under the MIT License.
324
+ This utility is licensed under the MIT License.
184
325
 
185
326
  ---
186
327
 
package/PUBLISH.MD CHANGED
@@ -4,13 +4,15 @@
4
4
  ### Test Locally
5
5
  `npm link`
6
6
 
7
+ `npm unlink -g`
8
+
9
+ `npm link properties-comparator`
7
10
 
8
11
  ## Now you can run the script globally using:
9
12
 
10
13
  ```properties-comparator <filePath1> <filePath2>```
11
14
 
12
15
 
13
-
14
16
  ## Publish the Package
15
17
  ### Login to npm (if you haven't already):
16
18
  `npm login`
package/README.md CHANGED
@@ -1,16 +1,91 @@
1
1
  # properties-comparator
2
- This utility provides functionality to parse and compare properties files in the format of key-value pairs. It reads properties files, compares the values for each key across multiple files, and logs the results.
3
2
 
4
- Utility is available as NPM Package [https://www.npmjs.com/package/properties-comparator](https://www.npmjs.com/package/properties-comparator)
3
+ A powerful utility for parsing and comparing **.properties** and **.yml/.yaml** files. This tool reads files as key-value pairs, compares values across multiple files, and generates detailed comparison reports in various formats.
5
4
 
5
+ [![NPM Package](https://img.shields.io/npm/v/properties-comparator.svg)](https://www.npmjs.com/package/properties-comparator)
6
6
 
7
- ### Install the Package
8
- `npm install -g properties-comparator`
7
+ ## Features
9
8
 
9
+ - **Multi-format Support**: Parse both **.properties** files and **.yml/.yaml** (YAML) files
10
+ - **Nested Structure Handling**: Flatten nested YAML structures into key-value pairs
11
+ - **Comprehensive Comparison**: Compare values across multiple files simultaneously
12
+ - **Multiple Report Formats**:
13
+ - Console output with color-coded highlighting
14
+ - HTML reports with CSS styling
15
+ - Markdown reports
16
+ - **Flexible Output Options**: Save reports to files or display in console
17
+ - **User-friendly CLI**: Simple command-line interface with intuitive options
10
18
 
11
- ## Now you can run the script globally using:
19
+ ## Installation
12
20
 
13
- ```properties-comparator <filePath1> <filePath2>```
21
+ ```bash
22
+ # Install globally
23
+ npm install -g properties-comparator
14
24
 
25
+ # To uninstall
26
+ npm uninstall -g properties-comparator
27
+ ```
15
28
 
16
- ### Check [Documentation](DOCUMENTATION.md) for more details
29
+ ## Usage
30
+
31
+ ### Basic Usage
32
+
33
+ Compare two or more files:
34
+
35
+ ```bash
36
+ properties-comparator <filePath1> <filePath2> [<filePath3> ...]
37
+ ```
38
+
39
+ ### Examples
40
+
41
+ Compare multiple files:
42
+ ```bash
43
+ properties-comparator ./config1.properties ./config2.yml ./config3.properties
44
+ ```
45
+
46
+ Generate HTML report:
47
+ ```bash
48
+ properties-comparator -f html -o report.html ./config1.properties ./config2.yml
49
+ ```
50
+
51
+ Generate Markdown report:
52
+ ```bash
53
+ properties-comparator -f markdown -o report.md ./config1.properties ./config2.yml
54
+ ```
55
+
56
+ ### Command Options
57
+
58
+ - `-f, --format <type>` - Report format (console, html, markdown)
59
+ - `-o, --output <file>` - Output file for the report
60
+ - `-h, --help` - Display help information
61
+
62
+ ## Report Examples
63
+
64
+ ### Terminal View
65
+ ![Terminal Report](./screenshots/TerminalTable.png)
66
+
67
+ ### HTML View
68
+ ![HTML Report](./screenshots/HtmlReport.png)
69
+
70
+ ### Markdown View
71
+ ![Markdown Report](./screenshots/MarkDownReport.png)
72
+
73
+ ## Quality Assurance
74
+
75
+ This package is thoroughly tested with over 90% code coverage to ensure reliability.
76
+
77
+ ![Test Coverage](./screenshots/TestCoverage.png)
78
+
79
+ ## Compatibility
80
+
81
+ Developed and tested with:
82
+ - npm v11.1.0
83
+ - Node.js v22.13.0
84
+
85
+ ## Documentation
86
+
87
+ For more detailed information, please check the [Documentation](DOCUMENTATION.md).
88
+
89
+ ## License
90
+
91
+ [MIT](LICENSE)
package/TEST.md ADDED
@@ -0,0 +1,62 @@
1
+ # Automated testing with Jest
2
+
3
+
4
+ ## Run Your Tests
5
+ `npm init -y # if you haven't created a package.json yet`
6
+ `npm install --save-dev jest`
7
+
8
+ `npm test`
9
+ or
10
+ `npx jest`
11
+
12
+
13
+ ### Check jest.config.js for testing configuration
14
+
15
+ `npm test -- --coverage`
16
+
17
+
18
+ ### Test Comparisons
19
+
20
+ `node index.js ./test/config1.properties ./test/config1.yaml`
21
+
22
+ `node index.js ./test/config1.properties ./test/config2.yml`
23
+
24
+ `node index.js ./test/config1.properties ./test/config2.properties`
25
+
26
+ `node index.js ./test/config2.properties ./test/config2.yml`
27
+
28
+ `node index.js ./test/config3.txt ./test/config3.yml2`
29
+
30
+ `node index.js --format html --output report.html ./test/config1.properties ./test/config2.yml`
31
+
32
+ `node index.js -f markdown -o report.md ./test/config1.properties ./test/config2.yml`
33
+
34
+ ### NPM LINK Test
35
+
36
+ `properties-comparator ./test/config1.properties ./test/config1.yaml`
37
+
38
+ `properties-comparator ./test/config1.properties ./test/config2.yml`
39
+
40
+ `properties-comparator ./test/config1.properties ./test/config2.properties`
41
+
42
+ `properties-comparator ./test/config2.properties ./test/config2.yml`
43
+
44
+ `properties-comparator ./test/config3.txt ./test/config3.yml2`
45
+
46
+ `properties-comparator --format html --output report.html ./test/config1.properties ./test/config2.yml`
47
+
48
+ `properties-comparator -f markdown -o report.md ./test/config1.properties ./test/config2.yml`
49
+
50
+
51
+ `properties-comparator ./test/config1.properties ./test/config2.yml ./test/config3.properties`
52
+
53
+ `properties-comparator -v ./test/config1.properties ./test/config2.yml ./test/config3.properties`
54
+
55
+
56
+ `properties-comparator -f html -o report.html ./test/config1.properties ./test/config1.yaml`
57
+
58
+ `properties-comparator -f html -o report.html ./test/config1.properties ./test/config2.yml`
59
+
60
+ `properties-comparator -f html -o report.html ./test/config1.properties ./test/config2.yml ./test/config3.properties`
61
+
62
+ `properties-comparator -f markdown -o report.md ./test/config1.properties ./test/config2.yml ./test/config3.properties`
@@ -0,0 +1,3 @@
1
+ export default {
2
+ presets: ['@babel/preset-env'],
3
+ };
package/cli.js ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { program } from 'commander';
4
+ import { compareFiles } from './index.js';
5
+ import { readFileSync } from 'fs';
6
+ import path from 'path';
7
+
8
+ // Get version from package.json
9
+ let version = '1.0.0';
10
+
11
+ try {
12
+ const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'));
13
+ version = packageJson.version;
14
+ } catch (err) {
15
+ // Use default version if package.json can't be read
16
+ }
17
+
18
+ // Check if no arguments were provided
19
+ if (process.argv.length <= 2) {
20
+ console.log(`properties-comparator v${version}`);
21
+ console.log('Usage: properties-comparator [options] <file1> <file2> [file3...]');
22
+ process.exit(0);
23
+ }
24
+
25
+ program
26
+ .version(version)
27
+ .description('Compare properties between multiple files')
28
+ .option('-f, --format <format>', 'Output format: console, html, or markdown (default: console)')
29
+ .option('-o, --output <path>', 'Output file path for results')
30
+ .option('-v, --verbose', 'Show verbose output')
31
+ .arguments('<files...>')
32
+ .usage('[options] <file1> <file2> [file3...]')
33
+ .action((files, options) => {
34
+ if (files.length < 2) {
35
+ console.error('Error: At least two files are required for comparison');
36
+ program.help();
37
+ process.exit(1);
38
+ }
39
+
40
+ try {
41
+ // Resolve all file paths
42
+ const resolvedPaths = files.map(file => path.resolve(file));
43
+
44
+ // Prepare options for compareFiles
45
+ const comparisonOptions = {
46
+ format: options.format || 'console',
47
+ outputFile: options.output,
48
+ verbose: options.verbose // Pass verbose option to compareFiles
49
+ };
50
+
51
+ // Run the comparison
52
+ compareFiles(resolvedPaths, comparisonOptions);
53
+
54
+ } catch (error) {
55
+ console.error('Error:', error.message);
56
+ process.exit(1);
57
+ }
58
+ });
59
+
60
+ program.parse(process.argv);