all files / lib/common/lifecycle/ index.js

15.79% Statements 6/38
0% Branches 0/16
33.33% Functions 1/3
16.67% Lines 6/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        1× 1× 1× 1×             1×                                                                                                                                           1×  
/* eslint-disable global-require */
 
'use strict';
 
const appRoot = require('app-root-path');
const colors  = require('colors');
const Promise = require('bluebird');
const _       = require('lodash');
 
/**
 * Class that manages CLI's lifecycle
 */
class Lifecycle {
  constructor(sqz) {
    this.sqz = sqz;
  }
 
  /**
   * Run each event from the lifecycle
   *
   * @param {Array} lifecycle - lifecycle array
   */
  run(lifecycle) {
    const hooks = this.sortHooks(lifecycle);
 
    this.sqz.cli.log.debug(`# Starting CLI lifecycle : [ ${colors.blue.bold(hooks.join(colors.green(' , ')))} ]`);
 
    Promise.each(hooks, (val) => {
      const hook = _.find(this.sqz.vars.hooks, { identifier : val });
 
      this.sqz.cli.log.debug(`# Running lifecycle event ${colors.blue.bold(val)}`);
 
      if (!hook) {
        this.sqz.cli.log.error(`No hook available for the lifecycle event ${colors.blue.bold(val)}`);
      }
 
      const Class = require(`${hook.path}`);
      const fn    = new Class(this.sqz);
 
      if (typeof fn[hook.function] !== 'function') {
        this.sqz.cli.log.error(
          `No hook function ${colors.blue.bold(hook.function)} available on ${colors.blue.bold(`${appRoot}/lib/${hook.path}`)}`
        );
      }
 
      const ret = fn[hook.function]();
 
      if (!ret || typeof ret.then !== 'function') {
        this.sqz.cli.log.error(`Function ${colors.blue.bold(hook.function)} for hook ${colors.blue.bold(hook.identifier)} should be a Promise`);
      }
 
      return ret;
    }).then(() => {
      this.sqz.cli.log.debug('CLI lifecycle finished.');
    });
  }
 
  sortHooks(hooks) {
    const cleanHooks    = hooks.filter(val => (['before', 'after'].indexOf(val.split(':')[0]) < 0));
    const priorityHooks = hooks.filter(val => (['before', 'after'].indexOf(val.split(':')[0]) > -1));
 
    priorityHooks.forEach((hook) => {
      const spl              = hook.split(':');
      const firstHookElement = spl[0];
      const restHookPart     = spl.slice(1).join(':');
      const restHookPosition = cleanHooks.indexOf(restHookPart);
 
      if (firstHookElement === 'before') {
        if (restHookPosition > -1) {
          cleanHooks.splice(restHookPosition, 0, hook);
        }
      }
 
      if (firstHookElement === 'after') {
        if (restHookPosition > -1) {
          cleanHooks.splice(restHookPosition + 1, 0, hook);
        }
      }
    });
 
    return cleanHooks;
  }
}
 
module.exports = Lifecycle;