| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | var fs = require('fs'), | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 	path = require('path'), | 
					
						
							|  |  |  | 	RDB = require('./redis.js'), | 
					
						
							| 
									
										
										
										
											2013-07-28 02:24:41 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 	eventEmitter = require('events').EventEmitter, | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 	plugins = { | 
					
						
							| 
									
										
										
										
											2013-09-04 13:36:25 -04:00
										 |  |  | 		libraries: {}, | 
					
						
							| 
									
										
										
										
											2013-07-28 02:24:41 -04:00
										 |  |  | 		loadedHooks: {}, | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 		staticDirs: {}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Events
 | 
					
						
							|  |  |  | 		readyEvent: new eventEmitter, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 		init: function() { | 
					
						
							|  |  |  | 			if (this.initialized) return; | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 			if (global.env === 'development') winston.info('[plugins] Initializing plugins system'); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			var _self = this; | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Read the list of activated plugins and require their libraries
 | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					RDB.smembers('plugins:active', next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(plugins, next) { | 
					
						
							| 
									
										
										
										
											2013-09-01 16:01:15 -04:00
										 |  |  | 					if (plugins && Array.isArray(plugins) && plugins.length > 0) { | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 						async.each(plugins, function(plugin, next) { | 
					
						
							| 
									
										
										
										
											2013-09-01 15:27:45 -04:00
										 |  |  | 							// TODO: Update this check to also check node_modules
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 							var pluginPath = path.join(__dirname, '../plugins/', plugin), | 
					
						
							| 
									
										
										
										
											2013-09-01 15:27:45 -04:00
										 |  |  | 								modulePath = path.join(__dirname, '../node_modules/', plugin); | 
					
						
							|  |  |  | 							if (fs.existsSync(pluginPath)) _self.loadPlugin(pluginPath, next); | 
					
						
							|  |  |  | 							else if (fs.existsSync(modulePath)) _self.loadPlugin(modulePath, next); | 
					
						
							|  |  |  | 							else { | 
					
						
							| 
									
										
										
										
											2013-09-01 23:19:16 -04:00
										 |  |  | 								if (global.env === 'development') winston.warn('[plugins] Plugin \'' + plugin + '\' not found'); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 								next(); // Ignore this plugin silently
 | 
					
						
							| 
									
										
										
										
											2013-09-01 15:27:45 -04:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						}, next); | 
					
						
							|  |  |  | 					} else next(); | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2013-09-03 22:03:04 -04:00
										 |  |  | 					if (global.env === 'development') winston.info('[plugins] Sorting hooks to fire in priority sequence'); | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 					Object.keys(_self.loadedHooks).forEach(function(hook) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						var hooks = _self.loadedHooks[hook]; | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 						hooks = hooks.sort(function(a, b) { | 
					
						
							|  |  |  | 							return a[3] - b[3]; | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					next(); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			], function(err) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 					if (global.env === 'development') winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 				if (global.env === 'development') winston.info('[plugins] Plugins OK'); | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				_self.readyEvent.emit('ready'); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 		ready: function(callback) { | 
					
						
							|  |  |  | 			this.readyEvent.once('ready', callback); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 		initialized: false, | 
					
						
							| 
									
										
										
										
											2013-08-22 09:50:29 -04:00
										 |  |  | 		loadPlugin: function(pluginPath, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			var _self = this; | 
					
						
							| 
									
										
										
										
											2013-08-22 09:50:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			fs.readFile(path.join(pluginPath, 'plugin.json'), function(err, data) { | 
					
						
							|  |  |  | 				if (err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				var pluginData = JSON.parse(data), | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 					libraryPath, staticDir; | 
					
						
							| 
									
										
										
										
											2013-08-22 09:50:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 				async.parallel([ | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						if (pluginData.library) { | 
					
						
							|  |  |  | 							libraryPath = path.join(pluginPath, pluginData.library); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							fs.exists(libraryPath, function(exists) { | 
					
						
							|  |  |  | 								if (exists) { | 
					
						
							|  |  |  | 									_self.libraries[pluginData.id] = require(libraryPath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 									if (pluginData.hooks && Array.isArray(pluginData.hooks) && pluginData.hooks.length > 0) { | 
					
						
							|  |  |  | 										async.each(pluginData.hooks, function(hook, next) { | 
					
						
							|  |  |  | 											_self.registerHook(pluginData.id, hook, next); | 
					
						
							|  |  |  | 										}, next); | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} else next(); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						if (pluginData.staticDir) { | 
					
						
							|  |  |  | 							staticDir = path.join(pluginPath, pluginData.staticDir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							fs.exists(staticDir, function(exists) { | 
					
						
							|  |  |  | 								if (exists) { | 
					
						
							|  |  |  | 									_self.staticDirs[pluginData.id] = staticDir; | 
					
						
							|  |  |  | 									next(); | 
					
						
							|  |  |  | 								} else next(); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} else next(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				], function(err) { | 
					
						
							|  |  |  | 					if (!err) { | 
					
						
							|  |  |  | 						if (global.env === 'development') winston.info('[plugins] Loaded plugin: ' + pluginData.id); | 
					
						
							|  |  |  | 						callback(); | 
					
						
							|  |  |  | 					} else callback(new Error('Could not load plugin system')) | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-08-22 09:50:29 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 		registerHook: function(id, data, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 			/* | 
					
						
							|  |  |  | 				`data` is an object consisting of (* is required): | 
					
						
							|  |  |  | 					`data.hook`*, the name of the NodeBB hook | 
					
						
							|  |  |  | 					`data.method`*, the method called in that plugin | 
					
						
							| 
									
										
										
										
											2013-07-28 02:24:41 -04:00
										 |  |  | 					`data.callbacked`, whether or not the hook expects a callback (true), or a return (false). Only used for filters. (Default: false) | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 					`data.priority`, the relative priority of the method when it is eventually called (default: 10) | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 			*/ | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			var _self = this; | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (data.hook && data.method) { | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 				// Assign default priority of 10 if none is passed-in
 | 
					
						
							|  |  |  | 				if (!data.priority) data.priority = 10; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 02:24:41 -04:00
										 |  |  | 				_self.loadedHooks[data.hook] = _self.loadedHooks[data.hook] || []; | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				_self.loadedHooks[data.hook].push([id, data.method, !! data.callbacked, data.priority]); | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 				if (global.env === 'development') winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id); | 
					
						
							| 
									
										
										
										
											2013-09-13 11:10:17 -04:00
										 |  |  | 				callback(); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 			} else return; | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-07-28 02:24:41 -04:00
										 |  |  | 		fireHook: function(hook, args, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			var _self = this | 
					
						
							|  |  |  | 			hookList = this.loadedHooks[hook]; | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-29 15:14:41 -04:00
										 |  |  | 			if (hookList && Array.isArray(hookList)) { | 
					
						
							| 
									
										
										
										
											2013-09-23 13:43:15 -04:00
										 |  |  | 				//if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\'');
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				var hookType = hook.split(':')[0]; | 
					
						
							|  |  |  | 				switch (hookType) { | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 					case 'filter': | 
					
						
							|  |  |  | 						// Filters only take one argument, so only args[0] will be passed in
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						var returnVal = (Array.isArray(args) ? args[0] : args); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-02 21:53:15 -04:00
										 |  |  | 						async.eachSeries(hookList, function(hookObj, next) { | 
					
						
							| 
									
										
										
										
											2013-08-22 09:50:29 -04:00
										 |  |  | 							if (hookObj[2]) { | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 								_self.libraries[hookObj[0]][hookObj[1]](returnVal, function(err, afterVal) { | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 									returnVal = afterVal; | 
					
						
							|  |  |  | 									next(err); | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							} else { | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 								returnVal = _self.libraries[hookObj[0]][hookObj[1]](returnVal); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 								next(); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						}, function(err) { | 
					
						
							|  |  |  | 							if (err) { | 
					
						
							| 
									
										
										
										
											2013-08-22 09:50:29 -04:00
										 |  |  | 								if (global.env === 'development') { | 
					
						
							|  |  |  | 									winston.info('[plugins] Problem executing hook: ' + hook); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							callback(returnVal); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 					case 'action': | 
					
						
							| 
									
										
										
										
											2013-07-28 02:24:41 -04:00
										 |  |  | 						async.each(hookList, function(hookObj) { | 
					
						
							| 
									
										
										
										
											2013-07-28 14:24:34 -04:00
										 |  |  | 							if ( | 
					
						
							|  |  |  | 								_self.libraries[hookObj[0]] && | 
					
						
							|  |  |  | 								_self.libraries[hookObj[0]][hookObj[1]] && | 
					
						
							|  |  |  | 								typeof _self.libraries[hookObj[0]][hookObj[1]] === 'function' | 
					
						
							|  |  |  | 							) { | 
					
						
							|  |  |  | 								_self.libraries[hookObj[0]][hookObj[1]].apply(_self.libraries[hookObj[0]], args); | 
					
						
							|  |  |  | 							} else { | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 								if (global.env === 'development') winston.info('[plugins] Expected method \'' + hookObj[1] + '\' in plugin \'' + hookObj[0] + '\' not found, skipping.'); | 
					
						
							| 
									
										
										
										
											2013-07-28 14:24:34 -04:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-07-28 02:24:41 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 					default: | 
					
						
							|  |  |  | 						// Do nothing...
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-29 15:14:41 -04:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				// Otherwise, this hook contains no methods
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				var returnVal = (Array.isArray(args) ? args[0] : args); | 
					
						
							| 
									
										
										
										
											2013-07-29 15:14:41 -04:00
										 |  |  | 				if (callback) callback(returnVal); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 		isActive: function(id, callback) { | 
					
						
							|  |  |  | 			RDB.sismember('plugins:active', id, callback); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		toggleActive: function(id, callback) { | 
					
						
							|  |  |  | 			this.isActive(id, function(err, active) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 					if (global.env === 'development') winston.info('[plugins] Could not toggle active state on plugin \'' + id + '\''); | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				RDB[(active ? 'srem' : 'sadd')]('plugins:active', id, function(err, success) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 						if (global.env === 'development') winston.info('[plugins] Could not toggle active state on plugin \'' + id + '\''); | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-07 11:45:04 -04:00
										 |  |  | 					if (callback) { | 
					
						
							|  |  |  | 						callback({ | 
					
						
							|  |  |  | 							id: id, | 
					
						
							|  |  |  | 							active: !active | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		showInstalled: function(callback) { | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 			// TODO: Also check /node_modules
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			var _self = this; | 
					
						
							|  |  |  | 			localPluginPath = path.join(__dirname, '../plugins'), | 
					
						
							|  |  |  | 			npmPluginPath = path.join(__dirname, '../node_modules'); | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2013-08-22 10:47:24 -04:00
										 |  |  | 					async.parallel([ | 
					
						
							|  |  |  | 						function(next) { | 
					
						
							|  |  |  | 							fs.readdir(localPluginPath, next); | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 						function(next) { | 
					
						
							|  |  |  | 							fs.readdir(npmPluginPath, next); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					], function(err, dirs) { | 
					
						
							|  |  |  | 						if (err) return next(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						dirs[0] = dirs[0].map(function(file) { | 
					
						
							|  |  |  | 							return path.join(localPluginPath, file); | 
					
						
							|  |  |  | 						}).filter(function(file) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 							var stats = fs.statSync(file); | 
					
						
							| 
									
										
										
										
											2013-08-22 10:47:24 -04:00
										 |  |  | 							if (stats.isDirectory()) return true; | 
					
						
							|  |  |  | 							else return false; | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						dirs[1] = dirs[1].map(function(file) { | 
					
						
							|  |  |  | 							return path.join(npmPluginPath, file); | 
					
						
							|  |  |  | 						}).filter(function(file) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 							var stats = fs.statSync(file); | 
					
						
							|  |  |  | 							if (stats.isDirectory() && file.substr(npmPluginPath.length + 1, 14) === 'nodebb-plugin-') return true; | 
					
						
							| 
									
										
										
										
											2013-08-22 10:47:24 -04:00
										 |  |  | 							else return false; | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						next(err, dirs[0].concat(dirs[1])); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(files, next) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					var plugins = []; | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					async.each(files, function(file, next) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						var configPath; | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 						async.waterfall([ | 
					
						
							|  |  |  | 							function(next) { | 
					
						
							| 
									
										
										
										
											2013-08-22 10:47:24 -04:00
										 |  |  | 								fs.readFile(path.join(file, 'plugin.json'), next); | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 							}, | 
					
						
							|  |  |  | 							function(configJSON, next) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 								var config = JSON.parse(configJSON); | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 								_self.isActive(config.id, function(err, active) { | 
					
						
							|  |  |  | 									if (err) next(new Error('no-active-state')); | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 									delete config.library; | 
					
						
							|  |  |  | 									delete config.hooks; | 
					
						
							|  |  |  | 									config.active = active; | 
					
						
							| 
									
										
										
										
											2013-07-29 15:45:13 -04:00
										 |  |  | 									config.activeText = '<i class="icon-off"></i> ' + (active ? 'Dea' : 'A') + 'ctivate'; | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 									next(null, config); | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						], function(err, config) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 							if (err) return next(); // Silently fail
 | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							plugins.push(config); | 
					
						
							| 
									
										
										
										
											2013-07-29 15:45:13 -04:00
										 |  |  | 							next(); | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					}, function(err) { | 
					
						
							|  |  |  | 						next(null, plugins); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			], function(err, plugins) { | 
					
						
							| 
									
										
										
										
											2013-07-29 15:15:49 -04:00
										 |  |  | 				callback(err, plugins); | 
					
						
							| 
									
										
										
										
											2013-07-28 12:52:58 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:16:21 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | plugins.init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = plugins; |