All files / lib task.js

100% Statements 53/53
100% Branches 10/10
100% Functions 3/3
100% Lines 53/53

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 542x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 5x 4x 4x 5x 5x 3x 3x 3x 3x 2x 3x 2x 2x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x  
"use strict";
import async from "async";
import fs from "fs";
import p from "path";
import util from "util";
 
/**
 * Load Bob task files in parallel.
 * Only task files in specified taskNames will be loaded.
 *
 * @param {Array} taskNames: an array of task names
 * @param {String} dir: base directory where task files are located
 * @param {Function} cb: standard cb(err, result) callback
 */
function load(taskNames, dir, cb) {
  const jobs = {};
 
  taskNames.forEach((taskName) => {
    jobs[taskName] = function (cb) {
      const file = p.join(dir, taskName + ".json");
      fs.access(file, fs.constants.F_OK, (err) => {
        if (err) {
          const _err = new Error(
            util.format(
              "Unknown command: %s, use --help for more info",
              taskName,
            ),
          );
          cb(_err);
        } else {
          fs.readFile(file, cb);
        }
      });
    };
  });
 
  function parse(err, results) {
    if (!err) {
      Object.keys(results).forEach((taskName) => {
        results[taskName] = JSON.parse(results[taskName]);
      });
    }
    cb(err, results);
  }
 
  async.parallel(jobs, parse);
}
 
const exports = {
  load: load,
};
 
export { exports as default };