Code coverage report for lib/load_config.js

Statements: 100% (7 / 7)      Branches: 100% (0 / 0)      Functions: 100% (1 / 1)      Lines: 100% (7 / 7)      Ignored: none     

All files » lib/ » load_config.js
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    25                           25 5 5         1       1       25  
'use strict';
 
var yaml = require('js-yaml'),
  fs = require('fs'),
  path = require('path'),
  stripComments = require('strip-json-comments');
 
/**
 * Try to load a configuration file: since this is configuration, we're
 * lenient with respect to its structure. It can be JSON or YAML,
 * and can contain comments, unlike normal JSON.
 *
 * @param {string} filePath the user-provided path to configuration
 * @returns {Object} configuration, if it can be parsed
 * @throws {Error} if the file cannot be read.
 */
function loadConfig(filePath) {
  try {
    return yaml.safeLoad(
      stripComments(
        fs.readFileSync(
          path.resolve(process.cwd(), filePath), 'utf8')));
  } catch (e) {
    e.message = 'Cannot read config file: ' +
      filePath +
      '\nError: ' +
      e.message;
    throw e;
  }
}
 
module.exports = loadConfig;