This repository was archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathplugin_loader.js
111 lines (95 loc) · 2.42 KB
/
plugin_loader.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
/*
* apidoc
* https://apidocjs.com
*
* Authors:
* Peter Rottmann <rottmann@inveris.de>
* Nicolas CARPi @ Deltablot
* Copyright (c) 2013 inveris OHG
* Licensed under the MIT license.
*/
const _ = require('lodash');
const path = require('path');
const util = require('util');
const glob = require('glob');
let app = {};
function PluginLoader (_app) {
const self = this;
// global variables
app = _app;
// class variables
self.plugins = {};
// Try to load global apidoc-plugins (if apidoc is installed locally it tries only local)
this.detectPugins(__dirname);
// Try to load local apidoc-plugins
this.detectPugins(path.join(process.cwd(), '/node_modules'));
if (Object.keys(this.plugins).length === 0) {
app.log.debug('No plugins found.');
}
this.loadPlugins();
}
/**
* Inherit
*/
util.inherits(PluginLoader, Object);
/**
* Exports
*/
module.exports = PluginLoader;
/**
* Detect modules start with "apidoc-plugin-".
* Search up to root until found a plugin.
*/
PluginLoader.prototype.detectPugins = function (dir) {
const self = this;
// Every dir start with "apidoc-plugin-", because for the tests of apidoc-plugin-test.
let plugins;
try {
plugins = glob.sync(dir + '/apidoc-plugin-*')
.concat(glob.sync(dir + '/@*/apidoc-plugin-*'));
} catch (e) {
app.log.warn(e);
return;
}
if (plugins.length === 0) {
dir = path.join(dir, '..');
if (dir === '/' || dir.substr(1) === ':\\') {
return;
}
return this.detectPugins(dir);
}
const offset = dir.length + 1;
plugins.forEach(function (plugin) {
const name = plugin.substr(offset);
const filename = path.relative(__dirname, plugin);
app.log.debug('add plugin: ' + name + ', ' + filename);
self.addPlugin(name, plugin);
});
};
/**
* Add Plugin to plugin list.
*/
PluginLoader.prototype.addPlugin = function (name, filename) {
if (this.plugins[name]) {
app.log.debug('overwrite plugin: ' + name + ', ' + this.plugins[name]);
}
this.plugins[name] = filename;
};
/**
* Load and initialize Plugins.
*/
PluginLoader.prototype.loadPlugins = function () {
_.forEach(this.plugins, function (filename, name) {
app.log.debug('load plugin: ' + name + ', ' + filename);
let plugin;
try {
plugin = require(filename);
} catch (e) {
}
if (plugin && plugin.init) {
plugin.init(app);
} else {
app.log.debug('Ignored, no init function found.');
}
});
};