All files bob.js

100% Statements 141/141
100% Branches 25/25
100% Functions 7/7
100% Lines 141/141

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 129 130 131 132 133 134 135 136 137 138 139 140 141 1421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 1x 1x 2x 2x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 4x 4x 4x 4x 4x 4x 3x 3x 4x 4x 1x 1x 1x 1x 1x  
"use strict"
import async from 'async';
import config from './config.js';
import jazz from 'jazz';
import p from 'path';
import runner from './runner.js';
import task from './task.js';
import util from 'util';
 
class Bob {
 
  /**
   * Constructor for initialising Bob.
   *
   * @param {Object} opts: optional
   * - appDir: directory where the userland application calls Bob from
   * - bobDir: directory where Bob's installation is located
   * - appName: name of the userland application (retrieved from its package.json)
   * - appVersion: version value of the userland application (retrieved from its package.json)
   * - bobMode: either `human` or `robot`, when you are a robot - you have to declare yourself :-]
   * - quiet: when true, only display task name and command, but without the command output
   */
  constructor(opts) {
    this.opts = opts;
  }
 
  /**
   * Bob is goin' to work!
   * Execute specified tasks
   *
   * @param {Array} taskNames: an array of task names
   * @param {Function} cb: standard cb(err, result) callback
   */
  build(taskNames, cb) {
    const EXEC_OPTS = {
      cwd: this.opts.appDir,
      quiet: this.opts.quiet,
      maxBuffer: this.opts.maxBufferInKb * 1024
    };
    const self = this;
 
    this._init(taskNames, (err, settings) => {
      if (err) { cb(err); } else {
        self._commands(taskNames, settings, (err, commands) => {
          if (err) { cb(err); } else {
            runner.execSeries(commands, EXEC_OPTS, cb);
          }
        });
      }
    });
  }
 
  /**
   * Load Bob task files and application config file, and install optional dependencies, all in parallel.
   *
   * @param {Array} taskNames: an array of task names
   * @param {Function} cb: standard cb(err, result) callback
   */
  _init(taskNames, cb) {
    const self = this;
 
    function loadTask(cb) {
      task.load(taskNames, p.join(self.opts.bobDir, 'conf', 'tasks'), cb);
    }
    function loadConfig(cb) {
      config.load(taskNames, self.opts.appDir, cb);
    }
    async.parallel({ bobTasks: loadTask, appConfig: loadConfig }, cb);
  }
 
  /**
   * Prepare commands to execute.
   *
   * @param {Array} taskNames: an array of task names
   * @param {Object} settings: tasks and application configs
   * @param {Function} cb: standard cb(err, result) callback
   */
  _commands(taskNames, settings, cb) {
    const TPL_PARAMS = {
      app: this.opts.appDir,
      bob: this.opts.bobDir,
      name: this.opts.appName,
      tmp: this.opts.tmpDir,
      version: this.opts.appVersion
    };
    const bobTasks = settings.bobTasks,
      bobMode = this.opts.bobMode,
      self = this;
    let commands = [];
 
    taskNames.forEach((taskName) => {
 
      self._taskTypeNames(taskName, settings).forEach((taskTypeName) => {
        let taskType = bobTasks[taskName].types[taskTypeName];
        Object.keys(taskType).forEach((key) => {
          if (typeof taskType[key] === 'object') {
            taskType[key] = taskType[key][bobMode];
          }
        });
        taskType.preOpts = taskType.preOpts || taskType.opts;
        // strip undefined and null from command elements
        const elems = [taskType.bin, taskType.preOpts, taskType.args, taskType.postOpts].filter(elem => elem);
        commands.push({
          format: util.format(elems.join(' ')),
          meta: {
            task: taskName,
            type: taskTypeName
          }
        });
      });
    });
 
    let jobs = [];
    commands.forEach((command) => {
      jobs.push((cb) => {
        jazz.compile(command.format).process(TPL_PARAMS, (result) => {
          command.exec = result;
          delete command.format;
          cb(null, command);
        });
      });
    });
    async.parallel(jobs, cb);
  }
 
  _taskTypeNames(taskName, settings) {
    // NOTE: allow multiple types in application config
    let taskTypeNames =
      (settings.appConfig[taskName] && settings.appConfig[taskName].type) ?
      settings.appConfig[taskName].type :
      settings.bobTasks[taskName].default;
    if (!Array.isArray(taskTypeNames)) {
      taskTypeNames = [taskTypeNames];
    }
    return taskTypeNames;
  }
}
 
export {
  Bob as default
};