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.

44 lines
1.1 KiB

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