validateRegistry.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. var assert = require('assert');
  3. function isFunction(fn) {
  4. return typeof fn === 'function';
  5. }
  6. function isConstructor(registry) {
  7. if (!(registry && registry.prototype)) {
  8. return false;
  9. }
  10. var hasProtoGet = isFunction(registry.prototype.get);
  11. var hasProtoSet = isFunction(registry.prototype.set);
  12. var hasProtoInit = isFunction(registry.prototype.init);
  13. var hasProtoTasks = isFunction(registry.prototype.tasks);
  14. if (hasProtoGet || hasProtoSet || hasProtoInit || hasProtoTasks) {
  15. return true;
  16. }
  17. return false;
  18. }
  19. function validateRegistry(registry) {
  20. try {
  21. assert(isFunction(registry.get), 'Custom registry must have `get` function');
  22. assert(isFunction(registry.set), 'Custom registry must have `set` function');
  23. assert(isFunction(registry.init), 'Custom registry must have `init` function');
  24. assert(isFunction(registry.tasks), 'Custom registry must have `tasks` function');
  25. } catch (err) {
  26. if (isConstructor(registry)) {
  27. assert(false, 'Custom registries must be instantiated, but it looks like you passed a constructor');
  28. } else {
  29. throw err;
  30. }
  31. }
  32. }
  33. module.exports = validateRegistry;