All files / src/cli-validator/utils init.js

19.05% Statements 8/42
0% Branches 0/10
0% Functions 0/4
19.05% Lines 8/42

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8412x 12x 12x 12x   12x 12x   12x         12x                                                                                                                                            
const fs = require('fs');
const util = require('util');
const findUp = require('find-up');
const printError = require('./printError');
 
const { defaults } = require('../../.defaultsForValidator');
const fileToCreate = process.cwd() + '/.validaterc';
 
module.exports.printDefaults = async function(chalk) {
  const successMessage = `'.validaterc' file created and set to defaults.`;
  return await writeConfigFile(defaults, successMessage, chalk);
};
 
module.exports.migrate = async function(chalk) {
  let oldConfigFile;
  try {
    oldConfigFile = await readCurrentConfig();
  } catch (err) {
    return err;
  }
 
  for (const category in oldConfigFile) {
    for (const rule in oldConfigFile[category]) {
      if (defaults.shared[category] && defaults.shared[category][rule]) {
        defaults.shared[category][rule] = oldConfigFile[category][rule];
      } else if (
        defaults.swagger2[category] &&
        defaults.swagger2[category][rule]
      ) {
        defaults.swagger2[category][rule] = oldConfigFile[category][rule];
      }
    }
  }
 
  const successMessage = `'.validaterc' file converted to new format.`;
  return await writeConfigFile(defaults, successMessage, chalk);
};
 
async function writeConfigFile(configObject, successMessage, chalk) {
  const writeFile = util.promisify(fs.writeFile);
  try {
    const indentationSpaces = 2;
    await writeFile(
      fileToCreate,
      JSON.stringify(defaults, null, indentationSpaces)
    );
    console.log('\n' + chalk.green('[Success]') + ` ${successMessage}\n`);
    return Promise.resolve(0);
  } catch (err) {
    const description =
      'Problem writing the .validaterc file. See below for details.';
    printError(chalk, description, err);
    return Promise.reject(2);
  }
}
 
async function readCurrentConfig(chalk) {
  let configObject;
  const configFile = await findUp('.validaterc');
 
  // if the user does not have a config file,
  // there is no need for the migration step
  if (configFile === null) {
    console.log(`No .validaterc file found to migrate.`);
    return Promise.reject(2);
  }
 
  const readFile = util.promisify(fs.readFile);
 
  try {
    // the config file must be in the root folder of the project
    const fileAsString = await readFile(configFile, 'utf8');
    configObject = JSON.parse(fileAsString);
  } catch (err) {
    // this most likely means there is a problem in the json syntax itself
    const description =
      'Could not read the .validaterc file. See below for details.';
    printError(chalk, description, err);
    return Promise.reject(2);
  }
 
  return configObject;
}