You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

8 years ago
  1. 'use strict'
  2. /**
  3. * Feature interface.
  4. */
  5. class Feature {
  6. /**
  7. * localWebServer instance passed to constructor in case feature needs access to http server instance.
  8. */
  9. constructor (localWebServer) {}
  10. /**
  11. * Return one or more options definitions to collect command-line input
  12. * @returns {OptionDefinition|OptionDefinition[]}
  13. */
  14. optionDefinitions () {}
  15. /**
  16. * Return one of more middleware functions with three args (req, res and next). Can be created by express, Koa or hand-rolled.
  17. */
  18. middleware (options) {}
  19. expandStack () {
  20. const flatten = require('reduce-flatten')
  21. if (this.stack) {
  22. const featureStack = this.stack()
  23. .map(Feature => new Feature())
  24. this.optionDefinitions = function () {
  25. return featureStack
  26. .map(feature => feature.optionDefinitions && feature.optionDefinitions())
  27. .filter(definitions => definitions)
  28. .reduce(flatten, [])
  29. }
  30. this.middleware = function (options, view) {
  31. return featureStack
  32. .map(feature => feature.middleware(options, view))
  33. .reduce(flatten, [])
  34. .filter(mw => mw)
  35. }
  36. }
  37. return this
  38. }
  39. }
  40. module.exports = Feature