All files Config.js

91.3% Statements 21/23
80% Branches 8/10
88.89% Functions 8/9
91.3% Lines 21/23

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x 1x 1x   1x 1x                                                               3x                 2x                 2x                   3x                       3x 1x 2x   2x 1x   1x 1x     3x               8x                                   2x                 1x       1x  
const fe = require('file-exists');
const fs = require('fs');
const isJson = require('is-valid-json');
 
const DEFAULT_CONFIG_WARNING_MESSAGE = 'No configuration file or object included. Defaults will be used.';
const DEFAULT_CONFIG = {
  accentColor: 'red',
  filename: 'example',
  header: {
    banner: 'http://www.example.com/image.png',
    link: 'http://www.example.com/',
    title: 'Example Header',
  },
  greeting: 'Hey there,',
  intro: 'Thanks for opening the email! Here are some links I want you to check out:',
  feeds: [
    {
      description: 'A short custom feed description',
      title: 'A custom feed title',
      url: 'http://www.feedforall.com/sample.xml',
    },
    {
      description: 'A short custom feed description',
      title: 'A custom feed title',
      url: 'http://www.feedforall.com/sample.xml',
    },
  ],
  outro: "Thanks for reading. We'll be back next week with more!",
  signature: 'John Smith, CMO at Example Co.',
};
 
/**
 * Determines whether an input is a filepath or not
 * @param {(string|Object)} input
 * @returns {boolean}
 */
function isFilepathString(input) {
  return typeof input === 'string' && fe.sync(input);
}
 
/**
 * Determines whether an input string is a JSON object or not
 * @param {(string|Object)} input
 * @returns {boolean}
 */
function isJsonString(input) {
  return typeof input === 'string' && isJson(input);
}
 
/**
 * Determines whether an input is a configuration object or not
 * @param {(string|Object)} input
 * @returns {boolean}
 */
function isConfigObject(input) {
  return typeof input === 'object';
}
 
/**
 * Merge two configuration objects
 * @param {Object} config1
 * @param {Object} config2
 * @returns {Object}
 */
function merge(config1, config2) {
  return Object.assign(config1, config2);
}
 
class Config {
  /**
   * Instantiates a new config object based on a string or object input
   * @param  {(string|Object)} input
   * @returns {Object}
   */
  constructor(input) {
    let config;
 
    if (isFilepathString(input)) {
      config = Config.getFromFile(input);
    } else Iif (isJsonString(input)) {
      config = Config.getFromJson(input);
    } else if (isConfigObject(input)) {
      config = Config.getFromObject(input);
    } else {
      console.warn(DEFAULT_CONFIG_WARNING_MESSAGE);
      config = Config.getDefault();
    }
 
    return config;
  }
 
  /**
   * Get the default config object
   * @returns {Object}
   */
  static getDefault() {
    return DEFAULT_CONFIG;
  }
 
  /**
   * Generate a config object from a json string
   * @param {string} input
   * @returns {Object}
   */
  static getFromJson(input) {
    return merge(Config.getDefault(), Config.getFromObject(JSON.parse(input)));
  }
 
  /**
   * Generate a config object from a filepath
   * @param {string} input
   * @returns {Object}
   */
  static getFromFile(input) {
    return merge(Config.getDefault(), JSON.parse(fs.readFileSync(input)));
  }
 
  /**
   * Generate a config object from an object
   * @param {Object} input
   * @returns {Object}
   */
  static getFromObject(input) {
    return merge(Config.getDefault(), input);
  }
}
 
module.exports = Config;