index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*!
  2. * use <https://github.com/jonschlinkert/use>
  3. *
  4. * Copyright (c) 2015, 2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var typeOf = require('kind-of');
  9. module.exports = function base(app, options) {
  10. if (typeOf(app) !== 'object' && typeof app !== 'function') {
  11. throw new TypeError('expected an object or function');
  12. }
  13. var opts = typeOf(options) === 'object' ? options : {};
  14. var prop = typeof opts.prop === 'string' ? opts.prop : 'fns';
  15. if (!Array.isArray(app[prop])) {
  16. define(app, prop, []);
  17. }
  18. /**
  19. * Define a plugin function to be passed to use. The only
  20. * parameter exposed to the plugin is `app`, the object or function.
  21. * passed to `use(app)`. `app` is also exposed as `this` in plugins.
  22. *
  23. * Additionally, **if a plugin returns a function, the function will
  24. * be pushed onto the `fns` array**, allowing the plugin to be
  25. * called at a later point by the `run` method.
  26. *
  27. * ```js
  28. * var use = require('use');
  29. *
  30. * // define a plugin
  31. * function foo(app) {
  32. * // do stuff
  33. * }
  34. *
  35. * var app = function(){};
  36. * use(app);
  37. *
  38. * // register plugins
  39. * app.use(foo);
  40. * app.use(bar);
  41. * app.use(baz);
  42. * ```
  43. * @name .use
  44. * @param {Function} `fn` plugin function to call
  45. * @api public
  46. */
  47. define(app, 'use', use);
  48. /**
  49. * Run all plugins on `fns`. Any plugin that returns a function
  50. * when called by `use` is pushed onto the `fns` array.
  51. *
  52. * ```js
  53. * var config = {};
  54. * app.run(config);
  55. * ```
  56. * @name .run
  57. * @param {Object} `value` Object to be modified by plugins.
  58. * @return {Object} Returns the object passed to `run`
  59. * @api public
  60. */
  61. define(app, 'run', function(val) {
  62. if (typeOf(val) !== 'object') return;
  63. if (!val.use || !val.run) {
  64. define(val, prop, val[prop] || []);
  65. define(val, 'use', use);
  66. }
  67. if (!val[prop] || val[prop].indexOf(base) === -1) {
  68. val.use(base);
  69. }
  70. var self = this || app;
  71. var fns = self[prop];
  72. var len = fns.length;
  73. var idx = -1;
  74. while (++idx < len) {
  75. val.use(fns[idx]);
  76. }
  77. return val;
  78. });
  79. /**
  80. * Call plugin `fn`. If a function is returned push it into the
  81. * `fns` array to be called by the `run` method.
  82. */
  83. function use(type, fn, options) {
  84. var offset = 1;
  85. if (typeof type === 'string' || Array.isArray(type)) {
  86. fn = wrap(type, fn);
  87. offset++;
  88. } else {
  89. options = fn;
  90. fn = type;
  91. }
  92. if (typeof fn !== 'function') {
  93. throw new TypeError('expected a function');
  94. }
  95. var self = this || app;
  96. var fns = self[prop];
  97. var args = [].slice.call(arguments, offset);
  98. args.unshift(self);
  99. if (typeof opts.hook === 'function') {
  100. opts.hook.apply(self, args);
  101. }
  102. var val = fn.apply(self, args);
  103. if (typeof val === 'function' && fns.indexOf(val) === -1) {
  104. fns.push(val);
  105. }
  106. return self;
  107. }
  108. /**
  109. * Wrap a named plugin function so that it's only called on objects of the
  110. * given `type`
  111. *
  112. * @param {String} `type`
  113. * @param {Function} `fn` Plugin function
  114. * @return {Function}
  115. */
  116. function wrap(type, fn) {
  117. return function plugin() {
  118. return this.type === type ? fn.apply(this, arguments) : plugin;
  119. };
  120. }
  121. return app;
  122. };
  123. function define(obj, key, val) {
  124. Object.defineProperty(obj, key, {
  125. configurable: true,
  126. writable: true,
  127. value: val
  128. });
  129. }