All files / lib config.js

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

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 412x 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 2x 2x
"use strict";
import fs from 'fs';
import p from 'path';
 
/**
 * Load application config file if exists, otherwise ignore.
 * Config file will be filtered, fields not in taskNames won't be .
 *
 * @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
};