-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from dev4justice/main
Add script to convert yaml -> json
- Loading branch information
Showing
3 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,7 @@ | |
"husky": "^8.0.0", | ||
"yaml": "^2.3.4" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"js-yaml": "^4.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <yaml_directory_or_file>'); | ||
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)); |