diff --git a/package-lock.json b/package-lock.json index f585ed8..42cab79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "boycott-israeli-tech-companies-dataset", "version": "1.0.0", "license": "ISC", + "dependencies": { + "js-yaml": "^4.1.0" + }, "devDependencies": { "ajv": "^8.12.0", "husky": "^8.0.0", @@ -18,6 +21,7 @@ "version": "8.12.0", "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.12.0.tgz", "integrity": "sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==", + "dev": true, "dependencies": { "fast-deep-equal": "^3.1.1", "json-schema-traverse": "^1.0.0", @@ -29,10 +33,16 @@ "url": "https://github.com/sponsors/epoberezkin" } }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", - "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "dev": true }, "node_modules/husky": { "version": "8.0.3", @@ -49,15 +59,28 @@ "url": "https://github.com/sponsors/typicode" } }, + "node_modules/js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dependencies": { + "argparse": "^2.0.1" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/json-schema-traverse": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", - "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "dev": true }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "dev": true, "engines": { "node": ">=6" } @@ -66,6 +89,7 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "dev": true, "engines": { "node": ">=0.10.0" } @@ -74,6 +98,7 @@ "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", + "dev": true, "dependencies": { "punycode": "^2.1.0" } @@ -82,6 +107,7 @@ "version": "2.3.4", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.4.tgz", "integrity": "sha512-8aAvwVUSHpfEqTQ4w/KMlf3HcRdt50E5ODIQJBw1fQ5RL34xabzxtUlzTXVqc4rkZsPbvrXKWnABCD7kWSmocA==", + "dev": true, "engines": { "node": ">= 14" } diff --git a/package.json b/package.json index 4dfe59f..4faa836 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,7 @@ "husky": "^8.0.0", "yaml": "^2.3.4" }, - "dependencies": {} + "dependencies": { + "js-yaml": "^4.1.0" + } } diff --git a/yaml_to_json_converter.js b/yaml_to_json_converter.js new file mode 100644 index 0000000..f927031 --- /dev/null +++ b/yaml_to_json_converter.js @@ -0,0 +1,66 @@ +const fs = require('fs'); +const path = require('path'); +const yaml = require('js-yaml'); + +function convertYamlToJson(yamlFilePath) { + const yamlData = fs.readFileSync(yamlFilePath, 'utf8'); + const data = yaml.load(yamlData); + + const fileName = path.basename(yamlFilePath, path.extname(yamlFilePath)).toLowerCase(); + + const convertKeysToLowerCase = (obj) => { + if (Array.isArray(obj)) { + return obj.map(item => convertKeysToLowerCase(item)); + } else if (typeof obj === 'object' && obj !== null) { + const result = {}; + for (const [key, value] of Object.entries(obj)) { + const lowercasedKey = key.toLowerCase(); + result[lowercasedKey] = convertKeysToLowerCase(value); + } + return result; + } else { + return obj; + } + }; + + const convertedData = convertKeysToLowerCase(data); + + for (const entry of convertedData) { + entry.tags = ['target', fileName]; + if (entry.Alternatives) { + for (const alternative of entry.Alternatives) { + alternative.tags = ['supported', fileName]; + } + } + } + + return convertedData; +} + +function combineJsonFiles(directory) { + const combinedData = {}; + + if (fs.existsSync(directory)) { + const files = fs.readdirSync(directory).filter(file => file.endsWith('.yaml') || file.endsWith('.yml')); + for (const filename of files.sort()) { + const yamlFilePath = path.join(directory, filename); + const key = path.basename(filename, path.extname(filename)).toLowerCase(); + combinedData[key] = JSON.parse(JSON.stringify(convertYamlToJson(yamlFilePath))); + } + } else { + console.error('Invalid directory path.'); + process.exit(1); + } + + return combinedData; +} + +if (process.argv.length !== 3) { + console.error('Usage: node yaml_to_json_converter.js '); + process.exit(1); +} + +const inputPath = process.argv[2]; +const outputData = fs.statSync(inputPath).isDirectory() ? combineJsonFiles(inputPath) : convertYamlToJson(inputPath); + +console.log(JSON.stringify(outputData, null, 2));