All files / piscosour/lib step.js

0% Statements 0/63
0% Branches 0/34
0% Functions 0/10
0% Lines 0/63
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 142 143 144 145 146 147 148 149 150 151 152                                                                                                                                                                                                                                                                                                               
'use strict';
 
const moment = require('moment');
const _ = require('lodash');
 
const bus = require('./bus');
const config = require('./config');
const logger = require('./logger');
const Waterfall = require('./utils/waterfall');
 
/**
 *
 * A step is a **Step** in a execution. **Flows** are considered as a pipeline.
 *
 * @param runner this is the configuration object inside a step.
 * @returns {Step}
 * @constructor Step
 */
const Step = function(step, plugins) {
  this.logger = logger;
  this.plugins = plugins || {};
 
  this._augment(step);
  Object.getOwnPropertyNames(this.plugins).forEach((plugin) => {
    if (this.plugins[plugin] && this.plugins[plugin].addons) {
      Object.getOwnPropertyNames(this.plugins[plugin].addons || {}).forEach((addon) => {
        if (config.get().stages.indexOf(addon) >= 0) {
          delete this.plugins[plugin].addons[addon];
        }
      });
      this._augment(this.plugins[plugin].addons);
    }
  });
  return this;
};
 
/**
 * Use to add functions to the object instance.
 * @param which. function to be added
 * @param namespace. namespace to add the function.
 * @private
 */
Step.prototype._augment = function(which, namespace) {
  const target = namespace ? this[namespace] : this;
 
  for (const name in which) {
    if (typeof which[name] === 'function') {
      target[name] = which[name].bind(this);
    } else {
      target[name] = which[name];
    }
  }
};
 
/**
 * Main function of a step. Execute the stage of the step, if this stage is implemented
 * @param stage
 * @returns {Promise}
 * @private
 */
Step.prototype._do = function(stage) {
  this.init = moment();
  const operation = this[stage];
  if (operation) {
    logger.info('#magenta', stage, 'stage running...');
    bus.emit('stage:start', { name: stage });
    return new Promise((resolve, reject) => {
      if (stage === 'emit') {
        this.outputs = this.outputs ? this.outputs : {};
        _.merge(this.outputs, operation());
        resolve();
      } else if (!operation(resolve, reject)) {
        logger.trace('auto-resolve is called!');
        resolve();
      }
    }).catch((err) => {
      const onError = this.onError;
      if (onError) {
        onError(stage, err);
      }
      throw err;
    });
  } else {
    return;
  }
};
 
/**
 * Execute all the plugins hooks configured for the Step.
 * @param stage
 * @private
 */
Step.prototype._doPlugins = function(stage) {
  logger.silly('doPlugins -in-', stage);
  const promises = [];
  if (this.plugins) {
    Object.getOwnPropertyNames(this.plugins).forEach((name) => {
      const plugin = this.plugins[name];
      if (plugin) {
        const operation = plugin[stage];
 
        if (operation) {
          if (stage === 'emit') {
            logger.warn('#yellow', 'WARNING!:', 'Emit parameters is not possible for plugins');
          } else {
            logger.trace('Executing plugin', name, 'pre-hook:', stage);
            promises.push({
              fn: operation.bind(this),
              args: [],
              obj: null
            });
          }
        }
      }
    });
  }
  if (promises.length > 0) {
    bus.emit('stage:start', { name: stage });
  }
  const waterfall = new Waterfall({
    promises: promises,
    logger: logger
  });
  logger.silly('doPlugins -out-', promises.length);
  return waterfall.start();
};
 
/**
 * Write reporting information of the step.
 *
 * example:
 * result has this aspect:
 * ```
 * const result = {
 *               status: 1|0,
 *               message: 'some text',
 *               content: 'some text',
 *               time: time in milliseconds,
 *               order: number,
 *               last: last
 *           };
 * this.report(result);
 * ```
 * @param result The object above
 * @function
 */
Step.prototype.report = function(result) {
  bus.emit('stage:end', result);
};
 
module.exports = Step;