forked from coralproject/talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.js
261 lines (223 loc) · 6.77 KB
/
plugins.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const fs = require('fs');
const path = require('path');
const resolve = require('resolve');
const debug = require('debug')('talk:plugins');
const Joi = require('joi');
const amp = require('app-module-path');
const PLUGINS_JSON = process.env.TALK_PLUGINS_JSON;
// Add the current path to the module root.
amp.addPath(__dirname);
let pluginsPath;
let plugins = {};
// Try to parse the plugins.json file, logging out an error if the plugins.json
// file isn't loaded, but continuing. Else, like a parsing error, throw it and
// crash the program.
try {
let envPlugins = path.join(__dirname, 'plugins.env.js');
let customPlugins = path.join(__dirname, 'plugins.json');
let defaultPlugins = path.join(__dirname, 'plugins.default.json');
if (PLUGINS_JSON && PLUGINS_JSON.length > 0) {
debug('Now using TALK_PLUGINS_JSON environment variable for plugins');
pluginsPath = envPlugins;
} else if (fs.existsSync(customPlugins)) {
debug(`Now using ${customPlugins} for plugins`);
pluginsPath = customPlugins;
} else {
debug(`Now using ${defaultPlugins} for plugins`);
pluginsPath = defaultPlugins;
}
plugins = require(pluginsPath);
} catch (err) {
if (err.code === 'ENOENT') {
console.error('plugins.json and plugins.default.json not found, plugins will not be active');
} else {
throw err;
}
}
/**
* All the hooks from plugins must match the schema defined here.
*/
const hookSchemas = {
passport: Joi.func().arity(1),
router: Joi.func().arity(1),
context: Joi.object().pattern(/\w/, Joi.func().maxArity(1)),
hooks: Joi.object().pattern(/\w/, Joi.object().pattern(/(?:__resolveType|\w+)/, Joi.object({
pre: Joi.func(),
post: Joi.func()
}))),
loaders: Joi.func().maxArity(1),
mutators: Joi.func().maxArity(1),
resolvers: Joi.object().pattern(/\w/, Joi.object().pattern(/(?:__resolveType|\w+)/, Joi.func())),
typeDefs: Joi.string()
};
/**
* isInternal checks to see if a given plugin is internal, and returns true
* if it is.
*
* @param {String} name
* @returns {Boolean}
*/
function isInternal(name) {
const internalPluginPath = path.join(__dirname, 'plugins', name);
// Check to see if this plugin exists internally, because if it doesn't, it is
// external.
return fs.existsSync(internalPluginPath);
}
/**
* Returns the plugin path for the given plugin name.
*
* @param {any} name
* @returns
*/
function pluginPath(name) {
if (isInternal(name)) {
try {
return resolve.sync(name, {moduleDirectory: 'plugins', basedir: process.cwd()});
} catch (e) {
console.warn(e);
return undefined;
}
}
try {
return resolve.sync(name, {basedir: process.cwd()});
} catch (e) {
return undefined;
}
}
class Plugin {
constructor(entry) {
// This checks to see if the structure for this entry is an object:
//
// {"people": "^1.2.0"}
//
// otherwise it's checked whether it matches the local version:
//
// "people"
//
if (typeof entry === 'object') {
this.name = Object.keys(entry).find((name) => name !== null);
this.version = entry[this.name];
} else if (typeof entry === 'string') {
this.name = entry;
this.version = `file:./plugins/${this.name}`;
} else {
throw new Error(`plugins.json is malformed, refer to PLUGINS.md for formatting, expected a string or an object for a plugin entry, found a ${typeof entry}`);
}
// Get the path for the plugin.
this.path = pluginPath(this.name);
}
require() {
if (typeof this.path === 'undefined') {
throw new Error(`plugin '${this.name}' is not local and is not resolvable, plugin reconsiliation may be required`);
}
try {
this.module = require(this.path);
} catch (e) {
if (e && e.code && e.code === 'MODULE_NOT_FOUND' && isInternal(this.name)) {
console.error(new Error(`plugin '${this.name}' could not be loaded due to missing dependencies, plugin reconsiliation may be required`));
throw e;
}
console.error(new Error(`plugin '${this.name}' could not be required from '${this.path}': ${e.message}`));
throw e;
}
}
}
/**
* Itterates over the plugins and gets the plugin path's, version, and name.
*
* @param {Array<Object|String>} plugins
* @returns {Array<Object>}
*/
const itteratePlugins = (plugins) => plugins.map((p) => new Plugin(p));
// Add each plugin folder to the allowed import path so that they can import our
// internal dependancies.
Object.keys(plugins).forEach((type) => itteratePlugins(plugins[type]).forEach((plugin) => {
// The plugin may be remote, and therefore not installed. We check here if the
// plugin path is available before trying to monkeypatch it's require path.
if (plugin.path) {
amp.enableForDir(path.dirname(plugin.path));
}
}));
/**
* Stores a reference to a section for a section of Plugins.
*/
class PluginSection {
constructor(plugins) {
this.required = false;
this.plugins = itteratePlugins(plugins);
}
require() {
if (this.required) {
return;
}
this.required = true;
this.plugins.forEach((plugin) => {
// Load the plugin.
plugin.require();
if (isInternal(plugin.name)) {
debug(`loading internal plugin '${plugin.name}' from '${plugin.path}'`);
} else {
debug(`loading external plugin '${plugin.name}' from '${plugin.path}'`);
}
return plugin;
});
}
/**
* This itterates over the section to provide all plugin hooks that are
* available.
*/
hook(hook) {
// Load the plugin source if we haven't already.
this.require();
return this.plugins
.filter(({module}) => hook in module)
.filter((plugin) => {
// Validate the hook.
if (hook in hookSchemas) {
Joi.assert(plugin.module[hook], hookSchemas[hook], `Plugin '${plugin.name}' failed validation for the '${hook}' hook`);
}
return true;
})
.map((plugin) => ({
plugin,
[hook]: plugin.module[hook]
}));
}
}
const NullPluginSection = new PluginSection([]);
/**
* Stores references to all the plugins available on the application.
*/
class PluginManager {
constructor(plugins) {
this.sections = {};
for (let section in plugins) {
this.sections[section] = new PluginSection(plugins[section]);
}
}
/**
* Utility function which combines the Plugins.section and PluginSection.hook
* calls.
*/
get(section, hook) {
return this.section(section).hook(hook);
}
/**
* Returns the named section if it exists, otherwise it returns an empty
* plugin section.
*/
section(section) {
if (section in this.sections) {
return this.sections[section];
}
return NullPluginSection;
}
}
module.exports = {
plugins,
pluginsPath,
PluginManager,
isInternal,
pluginPath,
itteratePlugins
};