json-parser-yaml-converter
Version:
Enhanced JSON Parser with verbose error messages and JSON to YAML conversion
98 lines (91 loc) • 3.81 kB
JavaScript
/**
* This module contains the functions jsonLogger and json2Yaml that log messages to the console.
*
* @module loggers
*/
;
import { jsonParse } from './index.js';
import { jsonToYaml } from './index.js';
import { GREEN, RED, RESET, BOLD, UNDERLINE } from './colors.js';
import traverse from 'traverse';
import { astToObject } from './parser.js';
/**
* Checks if a JSON file is valid.
* @param {string} jsonFile JSON file to check.
* @returns {void}
*/
function jsonLogger(jsonFile) {
try {
jsonParse(jsonFile);
console.log(GREEN + 'The JSON file ' + RESET + BOLD + UNDERLINE + jsonFile + RESET + GREEN + ' is valid!' + RESET);
} catch (error) {
console.error(RED + 'The JSON file ' + RESET + BOLD + UNDERLINE + jsonFile + RESET + RED + ' is ' + BOLD + UNDERLINE + 'not' + RESET + RED + ' valid!' + RESET);
if (error.token === undefined) {
console.error(RED + 'Error: ' + RESET + BOLD + error.message + RESET);
return;
}
const EXPECTED_MESSAGE = error.expected.join(', ');
console.error(RED + 'Error at line ' + RESET + BOLD + error.line + RESET + RED + ' and column ' + RESET + BOLD + error.col + RESET + RED + '.\nExpected ' + RESET + BOLD + EXPECTED_MESSAGE + RESET + RED + ' but found ' + RESET + BOLD + error.token + RESET);
}
}
/**
* Converts a JSON file to YAML format.
* @param {string} jsonFile JSON file to convert.
* @param {Object} options Command line options.
* @returns {void}
*/
function json2Yaml(jsonFile, options) {
if (options.output === null || options.output === true) {
options.output = jsonFile.replace(/\.[^.]*$/, '.yml');
}
if (options.output === jsonFile) {
options.output = jsonFile + '.yml';
}
try {
jsonToYaml(jsonFile, options.output);
console.log(GREEN + 'The JSON file ' + RESET + BOLD + UNDERLINE + jsonFile + RESET + GREEN + ' has been converted to YAML at ' + RESET + BOLD + UNDERLINE + options.output + RESET + GREEN + ' sucessfully!' + RESET);
} catch (error) {
console.error(RED + 'The JSON file ' + RESET + BOLD + UNDERLINE + jsonFile + RESET + RED + ' could not be converted to YAML!' + RESET);
if (error.token === undefined) {
console.error(RED + 'Error: ' + RESET + BOLD + error.message + RESET);
return;
}
const EXPECTED_MESSAGE = error.expected.join(', ');
console.error(RED + 'Error at line ' + RESET + BOLD + error.line + RESET + RED + ' and column ' + RESET + BOLD + error.col + RESET + RED + '.\nExpected ' + RESET + BOLD + EXPECTED_MESSAGE + RESET + RED + ' but found ' + RESET + BOLD + error.token + RESET);
}
}
/**
* Searches for a key in a JSON file.
* @param {string} jsonFile JSON file to search.
* @param {string} key Key to search for.
* @returns {void}
*/
function searchKeyLogger(jsonFile, key) {
const oldError = console.error;
const oldLog = console.log;
let output = '';
console.error = (message) => {
output += message + '\n';
};
console.log = () => {};
jsonLogger(jsonFile);
console.log = oldLog;
console.error = oldError;
if (output !== '') {
console.error(output);
return;
}
const JSON_AST = jsonParse(jsonFile, true);
let found = false;
traverse(JSON_AST).forEach((node) => {
if (node.key === key) {
found = true;
let value = astToObject(node.value);
console.log(GREEN + BOLD + UNDERLINE + key + RESET + ': ' + BOLD + UNDERLINE + JSON.stringify(value) + RESET);
}
});
if (!found) {
console.error(RED + 'The key ' + RESET + BOLD + UNDERLINE + key + RESET + RED + ' was not found in the JSON file ' + RESET + BOLD + UNDERLINE + jsonFile + RESET + RED + '!' + RESET);
}
}
export { jsonLogger, json2Yaml, searchKeyLogger };