all files / lib/ swaggyc.js

100% Statements 37/37
100% Branches 2/2
100% Functions 9/9
100% Lines 36/36
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                                                                                                            
const async  = require('async');
const jazz   = require('jazz');
const p      = require('path');
const runner = require('./runner');
const yamljs = require('yamljs');
 
/**
 * class SwaggyC
 * Stores params and opts, loads tasks definition from tasks.yml config.
 *
 * @param {Object} params: parameters to be merged to command format
 * @param {Object} opts: optional
 *   - inputPath: path to directory containing response files, or path to a single response file
 *   - reporter: an array of reporters, available reporters: console and file
 *   - outFile: path to output file, used when 'file' reporter is set
 */
function SwaggyC(params, opts) {
  this.params   = params;
  this.opts     = opts;
  this.tasksDef = yamljs.load(p.join(opts.swaggycDir, 'conf', 'tasks.yml'));
}
 
/**
 * Execute the specified tasks in sequence.
 *
 * @param {Array} tasks: an array of task names
 * @param {Function} cb: standard cb(err, result) callback
 */
SwaggyC.prototype.run = function (tasks, cb) {
  var self = this;
 
  this._commands(tasks, function (err, commands) {
    if (err) { cb(err); } else {
      runner.execSeries(commands, self.opts, cb);
    }
  });
};
 
/**
 * Prepare commands to execute.
 * Merge parameters specified in command format and CLI args.
 *
 * @param {Array} tasks: an array of task names
 * @param {Function} cb: standard cb(err, result) callback
 */
SwaggyC.prototype._commands = function (tasks, cb) {
  var self = this;
 
  var commands = [];
  tasks.forEach(function (task) {
 
    var taskElems = task.split('-');
    var language  = taskElems[0];
    var type      = taskElems[1];
    var format    = self.tasksDef[language][type];
 
    var command = {
      format: format,
      meta: {
        task: task,
        language: language,
        type: type
      }
    };
    commands.push(command);
  });
 
  var jobs = [];
  commands.forEach(function (command) {
    jobs.push(function (cb) {
      try {
        // merge parameters specified in command format in tasks.yml config
        jazz.compile(command.format).process(self.params, function (command_result) {
          // merge parameters specified in CLI args
          jazz.compile(command_result).process({ lang: command.meta.language }, function (result) {
            command.exec = result;
            delete command.format;
            cb(null, command);
          });
        });
      } catch (err) {
        cb(err);
      }
    });
  });
  async.parallel(jobs, cb);
};
 
module.exports = SwaggyC;