all files / core/lib/ plugins.js

100% Statements 27/27
100% Branches 2/2
100% Functions 5/5
100% Lines 26/26
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                                    189× 189× 189×             215× 215× 215× 215×                   218× 218× 244× 244× 244×                         244× 244×   244× 239× 239×   239×                     215×          
 
'use strict'
 
let debug = require('debug')('mako:plugins')
let flatten = require('array-flatten')
let pretty = require('pretty-hrtime')
let Promise = require('bluebird')
let wrapped = require('wrapped')
 
/**
 * A helper for wrapping and running plugins.
 *
 * TODO: move this to it's own module.
 *
 * @class
 */
class Plugins {
  /**
   * Creates a new instance.
   *
   * @param {String} id  Used for debug output and in the returned stats.
   */
  constructor (id) {
    debug('initialize %s', id)
    this.id = id
    this.fns = []
  }
 
  /**
   * Adds the given plugin functions to the queue. (arrays are flattened)
   */
  use () {
    let fns = this.fns
    let plugins = flatten.from(arguments).map(wrap)
    plugins.forEach(plugin => debug('use %s:%s', this.id, plugin.name))
    fns.push.apply(fns, plugins)
  }
 
  /**
   * Runs the plugins with the given arguments.
   *
   * @param {Array} args  The arguments to pass in.
   * @return {Promise} results
   */
  run (args) {
    debug('run %s', this.id)
    return Promise.mapSeries(this.fns, (plugin) => {
      let start = process.hrtime()
      debug('running %s:%s', this.id, plugin.name)
      return plugin.fn.apply(null, args).then(this.finish(plugin, start))
    })
  }
 
  /**
   * Creates a helper function for stopping the timer.
   *
   * @private
   * @param {Function} plugin  The plugin object
   * @param {Array} start      The hrtime array for when the plugin started
   * @return {Function} handler
   */
  finish (plugin, start) {
    let id = this.id
    let name = plugin.name
 
    return () => {
      let duration = process.hrtime(start)
      debug('finished %s:%s (took %s)', id, name, pretty(duration))
 
      return {
        id: id,
        plugin: name,
        duration: duration
      }
    }
  }
}
 
// single export
module.exports = Plugins
 
function wrap (fn) {
  return {
    name: fn.name || '[anonymous]',
    fn: Promise.promisify(wrapped(fn))
  }
}