-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.js
89 lines (72 loc) · 1.83 KB
/
store.js
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
89
'use strict';
var _ = require('lodash');
/**
* The plugin store
* This is used to store plugin (npm packages) reference and instantiate them when
* requested.
* @constructor
* @private
*/
var Store = module.exports = function Store() {
this._plugins = {};
this._meta = {};
};
/**
* Store a module under the namespace key
* @param {String} namespace - The key under which the plugin can be retrieved
* @param {String|Function} plugin - A plugin module or a module path
*/
Store.prototype.add = function add(namespace, plugin) {
if (_.isString(plugin)) {
this._storeAsPath(namespace, plugin);
return;
}
this._storeAsModule(namespace, plugin);
};
Store.prototype._storeAsPath = function _storeAsPath(namespace, path) {
this._meta[namespace] = {
resolved: path,
namespace: namespace
};
Object.defineProperty(this._plugins, namespace, {
get: function () {
var plugin = require(path);
return plugin;
},
enumerable: true,
configurable: true
});
};
Store.prototype._storeAsModule = function _storeAsModule(namespace, plugin) {
this._meta[namespace] = {
resolved: 'unknown',
namespace: namespace
};
this._plugins[namespace] = plugin;
};
/**
* Get the module registered under the given namespace
* @param {String} namespace
* @return {Module}
*/
Store.prototype.get = function get(namespace) {
var plugin = this._plugins[namespace];
if (!plugin) {
return;
}
return _.extend(plugin, this._meta[namespace]);
};
/**
* Returns the list of registered namespace.
* @return {Array} Namespaces array
*/
Store.prototype.namespaces = function namespaces() {
return Object.keys(this._plugins);
};
/**
* Get the stored plugins meta data
* @return {Object} plugins metadata
*/
Store.prototype.getpluginsMeta = function getpluginsMeta() {
return this._meta;
};