config-js-parser 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Krishnamurthy G B
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # config-parser
2
+ config-parser parses config to json and json to config text
3
+
4
+ ```
5
+
6
+ const {
7
+ convertConfigToJson,
8
+ convertJsonToConfig
9
+ } = require("config-js-parser")
10
+
11
+ // --- Execution Example ---
12
+
13
+ const sourceConfig = 'App.config';
14
+ const outputJson = 'settings.json';
15
+ const reconstructedConfig = 'App.updated.config';
16
+
17
+ // Example usage:
18
+ async function runConversion() {
19
+ // 1. Convert Config -> JSON
20
+ await convertConfigToJson(sourceConfig, outputJson);
21
+
22
+ // 2. Convert JSON -> Config
23
+ await convertJsonToConfig(outputJson, reconstructedConfig);
24
+ }
25
+
26
+ runConversion();
27
+
28
+ ```
@@ -0,0 +1,20 @@
1
+ const {
2
+ convertConfigToJson,
3
+ convertJsonToConfig
4
+ } = require("../index")
5
+ // --- Execution Example ---
6
+
7
+ const sourceConfig = 'App.config';
8
+ const outputJson = 'settings.json';
9
+ const reconstructedConfig = 'App.updated.config';
10
+
11
+ // Example usage:
12
+ async function runConversion() {
13
+ // 1. Convert Config -> JSON
14
+ await convertConfigToJson(sourceConfig, outputJson);
15
+
16
+ // 2. Convert JSON -> Config
17
+ await convertJsonToConfig(outputJson, reconstructedConfig);
18
+ }
19
+
20
+ runConversion();
package/index.js ADDED
@@ -0,0 +1,52 @@
1
+ const fs = require('fs');
2
+ const xml2js = require('xml2js');
3
+
4
+ /**
5
+ * Converts a .config (XML) file to a JSON file.
6
+ * @param {string} configFilePath - Path to the source .config file.
7
+ * @param {string} jsonDestinationPath - Path where the JSON should be saved.
8
+ */
9
+ async function convertConfigToJson(configFilePath, jsonDestinationPath) {
10
+ try {
11
+ // Read the .config file (XML format)
12
+ const configFileContent = fs.readFileSync(configFilePath, 'utf-8');
13
+
14
+ // explicitArray: false prevents single elements from being wrapped in arrays
15
+ const parser = new xml2js.Parser({ explicitArray: false });
16
+
17
+ const result = await parser.parseStringPromise(configFileContent);
18
+ const jsonContent = JSON.stringify(result, null, 4);
19
+
20
+ fs.writeFileSync(jsonDestinationPath, jsonContent, 'utf-8');
21
+ console.log(`Successfully converted ${configFilePath} to ${jsonDestinationPath}`);
22
+ } catch (error) {
23
+ console.error('Error converting Config to JSON:', error);
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Converts a JSON file back to a .config (XML) file.
29
+ * @param {string} jsonFilePath - Path to the source JSON file.
30
+ * @param {string} configDestinationPath - Path where the .config should be saved.
31
+ */
32
+ async function convertJsonToConfig(jsonFilePath, configDestinationPath) {
33
+ try {
34
+ // Read the JSON file
35
+ const jsonFileContent = fs.readFileSync(jsonFilePath, 'utf-8');
36
+ const jsonObject = JSON.parse(jsonFileContent);
37
+
38
+ // Build XML string from the JSON object
39
+ const builder = new xml2js.Builder();
40
+ const xmlContent = builder.buildObject(jsonObject);
41
+
42
+ fs.writeFileSync(configDestinationPath, xmlContent, 'utf-8');
43
+ console.log(`Successfully converted ${jsonFilePath} to ${configDestinationPath}`);
44
+ } catch (error) {
45
+ console.error('Error converting JSON to Config:', error);
46
+ }
47
+ }
48
+
49
+ module.exports = {
50
+ convertConfigToJson,
51
+ convertJsonToConfig
52
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "config-js-parser",
3
+ "version": "1.0.0",
4
+ "description": "config-parser parses config to json and json to config text",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "mocha --reporter spec --recursive --timeout 60000"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/ganeshkbhat/config-js-parser.git"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "type": "commonjs",
17
+ "bugs": {
18
+ "url": "https://github.com/ganeshkbhat/config-js-parser/issues"
19
+ },
20
+ "homepage": "https://github.com/ganeshkbhat/config-js-parser#readme",
21
+ "devDependencies": {
22
+ "chai": "^6.2.2",
23
+ "mocha": "^11.7.5"
24
+ },
25
+ "dependencies": {
26
+ "xml2js": "^0.6.2"
27
+ }
28
+ }
@@ -0,0 +1,79 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { expect } = require('chai');
4
+
5
+ // Assuming the conversion functions are exported from 'converter.js'
6
+ const { convertConfigToJson, convertJsonToConfig } = require('../index');
7
+
8
+ describe('Config and JSON Converter Tests', () => {
9
+ const testConfigPath = path.join(__dirname, 'test.config');
10
+ const testJsonPath = path.join(__dirname, 'test.json');
11
+ const finalConfigPath = path.join(__dirname, 'final.config');
12
+
13
+ const sampleXml =
14
+ `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
15
+ <configuration>
16
+ <appSettings>
17
+ <add key="Environment" value="Production"/>
18
+ </appSettings>
19
+ </configuration>`;
20
+
21
+ // Cleanup files before and after tests
22
+ const cleanup = () => {
23
+ [testConfigPath, testJsonPath, finalConfigPath].forEach(file => {
24
+ if (fs.existsSync(file)) fs.unlinkSync(file);
25
+ });
26
+ };
27
+
28
+ before(() => {
29
+ cleanup();
30
+ // Create initial config file for testing
31
+ fs.writeFileSync(testConfigPath, sampleXml, 'utf-8');
32
+ });
33
+
34
+ after(() => {
35
+ cleanup();
36
+ });
37
+
38
+ describe('convertConfigToJson()', () => {
39
+ it('should create a JSON file and contain the correct values', async () => {
40
+ await convertConfigToJson(testConfigPath, testJsonPath);
41
+
42
+ // Check if file exists
43
+ expect(fs.existsSync(testJsonPath)).to.be.true;
44
+
45
+ // Verify content
46
+ const jsonContent = JSON.parse(fs.readFileSync(testJsonPath, 'utf-8'));
47
+ expect(jsonContent).to.have.property('configuration');
48
+ expect(jsonContent.configuration.appSettings.add.$.key).to.equal('Environment');
49
+ expect(jsonContent.configuration.appSettings.add.$.value).to.equal('Production');
50
+ });
51
+ });
52
+
53
+ describe('convertJsonToConfig()', () => {
54
+ it('should convert JSON back to a valid .config XML file', async () => {
55
+ await convertJsonToConfig(testJsonPath, finalConfigPath);
56
+
57
+ // Check if file exists
58
+ expect(fs.existsSync(finalConfigPath)).to.be.true;
59
+
60
+ // Verify content contains XML tags
61
+ const configContent = fs.readFileSync(finalConfigPath, 'utf-8');
62
+ expect(configContent).to.contain('<configuration>');
63
+ expect(configContent).to.contain('key="Environment"');
64
+ expect(configContent).to.contain('value="Production"');
65
+ });
66
+ });
67
+
68
+ describe('Error Handling', () => {
69
+ it('should log an error if the source file does not exist', async () => {
70
+ // We expect this not to throw a hard crash because of the try-catch in the source code
71
+ // but we can verify it doesn't create an output file
72
+ const nonExistentPath = 'ghost.config';
73
+ const outputPath = 'ghost.json';
74
+
75
+ await convertConfigToJson(nonExistentPath, outputPath);
76
+ expect(fs.existsSync(outputPath)).to.be.false;
77
+ });
78
+ });
79
+ });