All files / lib config.js

100% Statements 45/45
100% Branches 9/9
100% Functions 1/1
100% Lines 45/45

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 462x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 4x 4x 3x 3x 11x 8x 8x 3x 3x 4x 4x 5x 1x 1x 5x 5x 2x 2x 2x 2x 2x 2x  
/**
 * Bob configuration loading module.
 * Handles loading and filtering of Bob configuration files.
 *
 * @module config
 */
"use strict";
import fs from "fs";
import p from "path";
 
/**
 * Load and filter Bob application configuration file.
 * Config file will be filtered; fields not in taskNames will be removed.
 *
 * @param {Array} taskNames: an array of task names
 * @param {String} dir: base directory where config file is located
 * @param {Function} cb: standard cb(err, result) callback
 */
function load(taskNames, dir, cb) {
  const file = p.join(dir, ".bob.json");
  // TODO: add validation
  fs.exists(file, (exists) => {
    if (exists) {
      fs.readFile(file, (err, data) => {
        if (!err) {
          data = JSON.parse(data);
          Object.keys(data).forEach((key) => {
            if (taskNames.indexOf(key) === -1) {
              delete data[key];
            }
          });
        }
        cb(err, data);
      });
    } else {
      cb(null, {});
    }
  });
}
 
const exports = {
  load: load,
};
 
export { exports as default };