mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	a couple minor 'tweaks' to the plugin system so that it works with npm installed plugins
This commit is contained in:
		| @@ -153,6 +153,11 @@ | |||||||
| 						.addClass('badge-inverse') | 						.addClass('badge-inverse') | ||||||
| 				} | 				} | ||||||
| 			}); | 			}); | ||||||
|  | 		}, | ||||||
|  |  | ||||||
|  | 		isRelativeUrl: function(url) { | ||||||
|  | 			var	firstChar = url.slice(0, 1); | ||||||
|  | 			return (firstChar === '.' || firstChar === '/'); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -20,28 +20,14 @@ var	fs = require('fs'), | |||||||
| 				function(plugins, next) { | 				function(plugins, next) { | ||||||
| 					async.each(plugins, function(plugin) { | 					async.each(plugins, function(plugin) { | ||||||
| 						// TODO: Update this check to also check node_modules | 						// TODO: Update this check to also check node_modules | ||||||
| 						var	pluginPath = path.join(__dirname, '../plugins/', plugin); | 						var	pluginPath = path.join(__dirname, '../plugins/', plugin), | ||||||
| 						fs.exists(pluginPath, function(exists) { | 							modulePath = path.join(__dirname, '../node_modules/', plugin); | ||||||
| 							if (exists) { | 						if (fs.existsSync(pluginPath)) _self.loadPlugin(pluginPath, next); | ||||||
| 								fs.readFile(path.join(pluginPath, 'plugin.json'), function(err, data) { | 						else if (fs.existsSync(modulePath)) _self.loadPlugin(modulePath, next); | ||||||
| 									if (err) return next(err); | 						else { | ||||||
|  | 							if (global.env === 'development') winston.info('[plugins] Plugin \'' + plugin + '\' not found'); | ||||||
| 									var	pluginData = JSON.parse(data); | 							next();	// Ignore this plugin silently | ||||||
| 									_self.libraries[pluginData.id] = require(path.join(pluginPath, pluginData.library)); | 						} | ||||||
| 									if (pluginData.hooks) { |  | ||||||
| 										for(var x=0,numHooks=pluginData.hooks.length;x<numHooks;x++) { |  | ||||||
| 											_self.registerHook(pluginData.id, pluginData.hooks[x]); |  | ||||||
| 										} |  | ||||||
| 									} |  | ||||||
| 									if (global.env === 'development') winston.info('[plugins] Loaded plugin: ' + pluginData.id); |  | ||||||
|  |  | ||||||
| 									next(); |  | ||||||
| 								}); |  | ||||||
| 							} else { |  | ||||||
| 								if (global.env === 'development') winston.info('[plugins] Plugin \'' + plugin + '\' not found'); |  | ||||||
| 								next();	// Ignore this plugin silently |  | ||||||
| 							} |  | ||||||
| 						}) |  | ||||||
| 					}, next); | 					}, next); | ||||||
| 				} | 				} | ||||||
| 			], function(err) { | 			], function(err) { | ||||||
| @@ -54,6 +40,24 @@ var	fs = require('fs'), | |||||||
| 			}); | 			}); | ||||||
| 		}, | 		}, | ||||||
| 		initialized: false, | 		initialized: false, | ||||||
|  | 		loadPlugin: function(pluginPath, callback) { | ||||||
|  | 			var	_self = this; | ||||||
|  |  | ||||||
|  | 			fs.readFile(path.join(pluginPath, 'plugin.json'), function(err, data) { | ||||||
|  | 				if (err) return callback(err); | ||||||
|  |  | ||||||
|  | 				var	pluginData = JSON.parse(data); | ||||||
|  | 				_self.libraries[pluginData.id] = require(path.join(pluginPath, pluginData.library)); | ||||||
|  | 				if (pluginData.hooks) { | ||||||
|  | 					for(var x=0,numHooks=pluginData.hooks.length;x<numHooks;x++) { | ||||||
|  | 						_self.registerHook(pluginData.id, pluginData.hooks[x]); | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 				if (global.env === 'development') winston.info('[plugins] Loaded plugin: ' + pluginData.id); | ||||||
|  |  | ||||||
|  | 				callback(); | ||||||
|  | 			}); | ||||||
|  | 		}, | ||||||
| 		registerHook: function(id, data) { | 		registerHook: function(id, data) { | ||||||
| 			/* | 			/* | ||||||
| 				`data` is an object consisting of (* is required): | 				`data` is an object consisting of (* is required): | ||||||
| @@ -66,7 +70,7 @@ var	fs = require('fs'), | |||||||
|  |  | ||||||
| 			if (data.hook && data.method) { | 			if (data.hook && data.method) { | ||||||
| 				_self.loadedHooks[data.hook] = _self.loadedHooks[data.hook] || []; | 				_self.loadedHooks[data.hook] = _self.loadedHooks[data.hook] || []; | ||||||
| 				_self.loadedHooks[data.hook].push([id, data.method]); | 				_self.loadedHooks[data.hook].push([id, data.method, !!data.callbacked]); | ||||||
| 				if (global.env === 'development') winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id); | 				if (global.env === 'development') winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id); | ||||||
| 			} else return; | 			} else return; | ||||||
| 		}, | 		}, | ||||||
| @@ -84,7 +88,7 @@ var	fs = require('fs'), | |||||||
| 						var	returnVal = (Array.isArray(args) ? args[0] : args); | 						var	returnVal = (Array.isArray(args) ? args[0] : args); | ||||||
|  |  | ||||||
| 						async.each(hookList, function(hookObj, next) { | 						async.each(hookList, function(hookObj, next) { | ||||||
| 							if (hookObj.callbacked) { | 							if (hookObj[2]) { | ||||||
| 								_self.libraries[hookObj[0]][hookObj[1]](returnVal, function(err, afterVal) { | 								_self.libraries[hookObj[0]][hookObj[1]](returnVal, function(err, afterVal) { | ||||||
| 									returnVal = afterVal; | 									returnVal = afterVal; | ||||||
| 									next(err); | 									next(err); | ||||||
| @@ -95,7 +99,9 @@ var	fs = require('fs'), | |||||||
| 							} | 							} | ||||||
| 						}, function(err) { | 						}, function(err) { | ||||||
| 							if (err) { | 							if (err) { | ||||||
| 								if (global.env === 'development') winston.info('[plugins] Problem executing hook: ' + hook); | 								if (global.env === 'development') { | ||||||
|  | 									winston.info('[plugins] Problem executing hook: ' + hook); | ||||||
|  | 								} | ||||||
| 							} | 							} | ||||||
|  |  | ||||||
| 							callback(returnVal); | 							callback(returnVal); | ||||||
|   | |||||||
| @@ -175,7 +175,7 @@ var	RDB = require('./redis.js'), | |||||||
| 				this.attr('rel', 'nofollow'); | 				this.attr('rel', 'nofollow'); | ||||||
| 				var href = this.attr('href'); | 				var href = this.attr('href'); | ||||||
|  |  | ||||||
| 				if (href && !href.match(domain)) { | 				if (href && !href.match(domain) && !utils.isRelativeUrl(href)) { | ||||||
| 					this.attr('href', domain + 'outgoing?url=' + encodeURIComponent(href)); | 					this.attr('href', domain + 'outgoing?url=' + encodeURIComponent(href)); | ||||||
| 					if (!isSignature) this.append(' <i class="icon-external-link"></i>'); | 					if (!isSignature) this.append(' <i class="icon-external-link"></i>'); | ||||||
| 				} | 				} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user