Refactor plugin logic.

This relies more heavily on `async` and also makes the loading process more asynchronous. It does remove one warning in the case that a plugin is enabled but not installed.
v1.18.x
Micheil Smith 11 years ago
parent 5f6d5cd9a5
commit 01013f5c9d

@ -72,25 +72,21 @@ var fs = require('fs'),
db.getSetMembers('plugins:active', next); db.getSetMembers('plugins:active', next);
}, },
function(plugins, next) { function(plugins, next) {
if (plugins && Array.isArray(plugins)) { if (!plugins || !Array.isArray(plugins)) {
plugins.push(meta.config['theme:id']); next();
}
async.each(plugins, function(plugin, next) { plugins.push(meta.config['theme:id']);
if (!plugin || typeof plugin !== 'string') {
return next();
}
var modulePath = path.join(__dirname, '../node_modules/', plugin); plugins = plugins.filter(function(plugin){
if (fs.existsSync(modulePath)) { return plugin && typeof plugin === 'string';
Plugins.loadPlugin(modulePath, next); }).map(function(plugin){
} else { return path.join(__dirname, '../node_modules/', plugin);
if (global.env === 'development') { });
winston.warn('[plugins] Plugin \'' + plugin + '\' not found');
} async.filter(plugins, fs.exists, function(plugins){
next(); // Ignore this plugin silently async.each(plugins, Plugins.loadPlugin, next);
} });
}, next);
} else next();
}, },
function(next) { function(next) {
if (global.env === 'development') winston.info('[plugins] Sorting hooks to fire in priority sequence'); if (global.env === 'development') winston.info('[plugins] Sorting hooks to fire in priority sequence');
@ -434,28 +430,31 @@ var fs = require('fs'),
}; };
Plugins.showInstalled = function(callback) { Plugins.showInstalled = function(callback) {
npmPluginPath = path.join(__dirname, '../node_modules'); var npmPluginPath = path.join(__dirname, '../node_modules');
async.waterfall([ async.waterfall([
function(next) { async.apply(fs.readdir, npmPluginPath),
fs.readdir(npmPluginPath, function(err, dirs) {
dirs = dirs.map(function(file) { function(dirs, next) {
return path.join(npmPluginPath, file); dirs = dirs.filter(function(dir){
}).filter(function(file) { return dir.substr(0, 14) === 'nodebb-plugin-' || dir.substr(0, 14) === 'nodebb-widget-';
if (fs.existsSync(file)) { }).map(function(dir){
var stats = fs.statSync(file), return path.join(npmPluginPath, dir);
isPlugin = file.substr(npmPluginPath.length + 1, 14) === 'nodebb-plugin-' || file.substr(npmPluginPath.length + 1, 14) === 'nodebb-widget-'; });
if (stats.isDirectory() && isPlugin) return true; async.filter(dirs, function(dir, callback){
else return false; fs.stat(dir, function(err, stats){
} else { if (err) {
return false; return callback(false);
} }
});
next(err, dirs); callback(stats.isDirectory());
})
}, function(plugins){
next(null, plugins);
}); });
}, },
function(files, next) { function(files, next) {
var plugins = []; var plugins = [];

Loading…
Cancel
Save