All files loggers.js

93.33% Statements 56/60
80% Branches 8/10
100% Functions 2/2
93.33% Lines 56/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 612x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 24x 24x 24x 24x 9x 9x 1x 1x 1x 8x 8x 8x 24x 2x 2x 2x 2x 2x 2x 2x 3x 3x     3x     3x 3x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 3x 2x 2x  
/**
 * This module contains the functions jsonLogger and json2Yaml that log messages to the console.
 *
 * @module loggers
 */
 
'use strict';
 
import { jsonParse } from './index.js';
import { jsonToYaml } from './index.js';
import { GREEN, RED, RESET, BOLD, UNDERLINE } from './colors.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);
  }
}
 
export { jsonLogger, json2Yaml };