All files / lib config.js

100% Statements 21/21
100% Branches 11/11
100% Functions 5/5
100% Lines 21/21

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 84 85 86 87 88 89 90 91 92 93 94    1x 1x 1x   1x                             3x                       3x 3x                       1x                       5x 14x   14x 4x       1x                     1x 5x   5x 1x     4x   1x   2x   1x      
'use strict';
 
const fs = require('fs');
const path = require('path');
const stripComments = require('strip-json-comments');
 
const CONFIG_FILES = [
  '.gintrc.js',
  '.gintrc.json',
  '.gintrc',
  'package.json'
];
 
 
/**
 * Convenience wrapper for synchronously reading file contents.
 * @param {string} filePath The filename to read.
 * @returns {string} The file contents, with the BOM removed.
 * @private
 */
function readFile(filePath) {
  return fs.readFileSync(filePath, 'utf8').replace(/^\ufeff/, '');
}
 
 
/**
 * Loads a JSON configuration from a file.
 * @param {string} filePath The filename to load.
 * @returns {Object} The configuration object from the file.
 * @throws {Error} If the file cannot be read.
 * @private
 */
function loadJSONConfigFile(filePath) {
  const config = JSON.parse(stripComments(readFile(filePath)));
  return (path.basename(filePath) === 'package.json') ? config.gintConfig : config;
}
 
 
/**
 * Loads a JavaScript configuration from a file.
 * @param {string} filePath The filename to load.
 * @returns {Object} The configuration object from the file.
 * @throws {Error} If the file cannot be read.
 * @private
 */
function loadJSConfigFile(filePath) {
  return require(filePath);
}
 
 
/**
   * Retrieves the configuration filename for a given directory. It loops over all
   * of the valid configuration filenames in order to find the first one that exists.
   * @param {string} directory The directory to check for a config file.
   * @returns {?string} The filename of the configuration file for the directory
   *      or null if there is no configuration file in the directory.
   */
function getFilenameForDirectory(directory) {
  for (let i = 0; i < CONFIG_FILES.length; i++) {
    const filename = path.join(directory, CONFIG_FILES[i]);
 
    if (fs.existsSync(filename) && fs.statSync(filename).isFile()) {
      return filename;
    }
  }
 
  return null;
}
 
 
/**
 * Loads a configuration file regardless of the source. Inspects the file path
 * to determine the correctly way to load the config file.
 * @param {Object} file The path to the configuration.
 * @returns {Object} The configuration information.
 * @private
 */
module.exports = function(dir) {
  const filePath = getFilenameForDirectory(dir);
 
  if (!filePath) {
    return null;
  }
 
  switch (path.extname(filePath)) {
    case '.js':
      return loadJSConfigFile(filePath);
    case '.json':
      return loadJSONConfigFile(filePath);
    default:
      return loadJSONConfigFile(filePath);
  }
};