From 01013f5c9db06aa28d911c6da26499c0dc13c61e Mon Sep 17 00:00:00 2001 From: Micheil Smith Date: Tue, 8 Apr 2014 21:36:18 +0100 Subject: [PATCH] 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. --- src/plugins.js | 65 +++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index d915d347ab..16e421ce62 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -72,25 +72,21 @@ var fs = require('fs'), db.getSetMembers('plugins:active', next); }, function(plugins, next) { - if (plugins && Array.isArray(plugins)) { - plugins.push(meta.config['theme:id']); + if (!plugins || !Array.isArray(plugins)) { + next(); + } - async.each(plugins, function(plugin, next) { - if (!plugin || typeof plugin !== 'string') { - return next(); - } + plugins.push(meta.config['theme:id']); - var modulePath = path.join(__dirname, '../node_modules/', plugin); - if (fs.existsSync(modulePath)) { - Plugins.loadPlugin(modulePath, next); - } else { - if (global.env === 'development') { - winston.warn('[plugins] Plugin \'' + plugin + '\' not found'); - } - next(); // Ignore this plugin silently - } - }, next); - } else next(); + plugins = plugins.filter(function(plugin){ + return plugin && typeof plugin === 'string'; + }).map(function(plugin){ + return path.join(__dirname, '../node_modules/', plugin); + }); + + async.filter(plugins, fs.exists, function(plugins){ + async.each(plugins, Plugins.loadPlugin, next); + }); }, function(next) { 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) { - npmPluginPath = path.join(__dirname, '../node_modules'); + var npmPluginPath = path.join(__dirname, '../node_modules'); async.waterfall([ - function(next) { - fs.readdir(npmPluginPath, function(err, dirs) { - dirs = dirs.map(function(file) { - return path.join(npmPluginPath, file); - }).filter(function(file) { - if (fs.existsSync(file)) { - var stats = fs.statSync(file), - isPlugin = file.substr(npmPluginPath.length + 1, 14) === 'nodebb-plugin-' || file.substr(npmPluginPath.length + 1, 14) === 'nodebb-widget-'; - - if (stats.isDirectory() && isPlugin) return true; - else return false; - } else { - return false; + async.apply(fs.readdir, npmPluginPath), + + function(dirs, next) { + dirs = dirs.filter(function(dir){ + return dir.substr(0, 14) === 'nodebb-plugin-' || dir.substr(0, 14) === 'nodebb-widget-'; + }).map(function(dir){ + return path.join(npmPluginPath, dir); + }); + + async.filter(dirs, function(dir, callback){ + fs.stat(dir, function(err, stats){ + if (err) { + return callback(false); } - }); - next(err, dirs); + callback(stats.isDirectory()); + }) + }, function(plugins){ + next(null, plugins); }); }, + function(files, next) { var plugins = [];