| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var fs = require('fs'), | 
					
						
							|  |  |  | 	path = require('path'), | 
					
						
							|  |  |  | 	semver = require('semver'), | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2015-04-16 11:30:53 +02:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2016-01-15 10:09:02 -05:00
										 |  |  | 	_ = require('underscore'), | 
					
						
							| 
									
										
										
										
											2016-04-04 20:43:21 -04:00
										 |  |  | 	file = require('../file'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var utils = require('../../public/src/utils'), | 
					
						
							|  |  |  | 	meta = require('../meta'); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(Plugins) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Plugins.loadPlugin = function(pluginPath, callback) { | 
					
						
							| 
									
										
										
										
											2015-01-06 23:29:48 -05:00
										 |  |  | 		Plugins.loadPluginInfo(pluginPath, function(err, pluginData) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2015-03-11 18:04:27 -04:00
										 |  |  | 				if (err.message === '[[error:parse-error]]') { | 
					
						
							|  |  |  | 					return callback(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 				return callback(pluginPath.match('nodebb-theme') ? null : err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 11:01:59 -04:00
										 |  |  | 			checkVersion(pluginData); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					registerHooks(pluginData, pluginPath, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					mapStaticDirectories(pluginData, pluginPath, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2014-12-26 19:02:50 -05:00
										 |  |  | 					mapFiles(pluginData, 'css', 'cssFiles', next); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2014-12-26 19:02:50 -05:00
										 |  |  | 					mapFiles(pluginData, 'less', 'lessFiles', next); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					mapClientSideScripts(pluginData, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-04-04 20:43:21 -04:00
										 |  |  | 				function(next) { | 
					
						
							|  |  |  | 					mapClientModules(pluginData, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 				function(next) { | 
					
						
							|  |  |  | 					loadLanguages(pluginData, next); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			], function(err) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					winston.verbose('[plugins] Could not load plugin : ' + pluginData.id); | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				winston.verbose('[plugins] Loaded plugin: ' + pluginData.id); | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 11:01:59 -04:00
										 |  |  | 	function checkVersion(pluginData) { | 
					
						
							|  |  |  | 		function add() { | 
					
						
							|  |  |  | 			if (Plugins.versionWarning.indexOf(pluginData.id) === -1) { | 
					
						
							|  |  |  | 				Plugins.versionWarning.push(pluginData.id); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-03-07 00:59:03 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (pluginData.nbbpm && pluginData.nbbpm.compatibility && semver.validRange(pluginData.nbbpm.compatibility)) { | 
					
						
							| 
									
										
										
										
											2015-08-20 10:29:58 -04:00
										 |  |  | 			if (!semver.satisfies(nconf.get('version'), pluginData.nbbpm.compatibility)) { | 
					
						
							| 
									
										
										
										
											2015-08-20 11:01:59 -04:00
										 |  |  | 				add(); | 
					
						
							| 
									
										
										
										
											2015-03-07 00:59:03 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-08-20 11:01:59 -04:00
										 |  |  | 			add(); | 
					
						
							| 
									
										
										
										
											2015-03-07 00:59:03 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 	function registerHooks(pluginData, pluginPath, callback) { | 
					
						
							|  |  |  | 		if (!pluginData.library) { | 
					
						
							| 
									
										
										
										
											2015-06-02 17:35:41 -04:00
										 |  |  | 			return callback(); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var libraryPath = path.join(pluginPath, pluginData.library); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-12 23:24:19 -05:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			if (!Plugins.libraries[pluginData.id]) { | 
					
						
							| 
									
										
										
										
											2015-01-06 23:29:48 -05:00
										 |  |  | 				Plugins.requireLibrary(pluginData.id, libraryPath); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (Array.isArray(pluginData.hooks) && pluginData.hooks.length > 0) { | 
					
						
							|  |  |  | 				async.each(pluginData.hooks, function(hook, next) { | 
					
						
							|  |  |  | 					Plugins.registerHook(pluginData.id, hook, next); | 
					
						
							|  |  |  | 				}, callback); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-01-12 23:24:19 -05:00
										 |  |  | 		} catch(err) { | 
					
						
							|  |  |  | 			winston.error(err.stack); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:35:41 -04:00
										 |  |  | 			winston.warn('[plugins] Unable to parse library for: ' + pluginData.id); | 
					
						
							|  |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2015-01-12 23:24:19 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function mapStaticDirectories(pluginData, pluginPath, callback) { | 
					
						
							|  |  |  | 		function mapStaticDirs(mappedPath, callback) { | 
					
						
							|  |  |  | 			if (Plugins.staticDirs[mappedPath]) { | 
					
						
							|  |  |  | 				winston.warn('[plugins/' + pluginData.id + '] Mapped path (' + mappedPath + ') already specified!'); | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			} else if (!validMappedPath.test(mappedPath)) { | 
					
						
							|  |  |  | 				winston.warn('[plugins/' + pluginData.id + '] Invalid mapped path specified: ' + mappedPath + '. Path must adhere to: ' + validMappedPath.toString()); | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				var realPath = pluginData.staticDirs[mappedPath]; | 
					
						
							|  |  |  | 				var staticDir = path.join(pluginPath, realPath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 18:22:41 -04:00
										 |  |  | 				file.exists(staticDir, function(exists) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 					if (exists) { | 
					
						
							|  |  |  | 						Plugins.staticDirs[pluginData.id + '/' + mappedPath] = staticDir; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						winston.warn('[plugins/' + pluginData.id + '] Mapped path \'' + mappedPath + ' => ' + staticDir + '\' not found.'); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					callback(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var validMappedPath = /^[\w\-_]+$/; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pluginData.staticDirs = pluginData.staticDirs || {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var dirs = Object.keys(pluginData.staticDirs); | 
					
						
							|  |  |  | 		async.each(dirs, mapStaticDirs, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 19:02:50 -05:00
										 |  |  | 	function mapFiles(pluginData, type, globalArray, callback) { | 
					
						
							|  |  |  | 		if (Array.isArray(pluginData[type])) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			if (global.env === 'development') { | 
					
						
							| 
									
										
										
										
											2014-12-26 19:02:50 -05:00
										 |  |  | 				winston.verbose('[plugins] Found ' + pluginData[type].length + ' ' + type + ' file(s) for plugin ' + pluginData.id); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 19:02:50 -05:00
										 |  |  | 			Plugins[globalArray] = Plugins[globalArray].concat(pluginData[type].map(function(file) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 				return path.join(pluginData.id, file); | 
					
						
							|  |  |  | 			})); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function mapClientSideScripts(pluginData, callback) { | 
					
						
							|  |  |  | 		if (Array.isArray(pluginData.scripts)) { | 
					
						
							|  |  |  | 			if (global.env === 'development') { | 
					
						
							|  |  |  | 				winston.verbose('[plugins] Found ' + pluginData.scripts.length + ' js file(s) for plugin ' + pluginData.id); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Plugins.clientScripts = Plugins.clientScripts.concat(pluginData.scripts.map(function(file) { | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 				return resolveModulePath(path.join(__dirname, '../../node_modules/', pluginData.id, file), file); | 
					
						
							|  |  |  | 			})).filter(Boolean); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-18 15:17:21 -05:00
										 |  |  | 		if (Array.isArray(pluginData.acpScripts)) { | 
					
						
							|  |  |  | 			if (global.env === 'development') { | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 				winston.verbose('[plugins] Found ' + pluginData.acpScripts.length + ' ACP js file(s) for plugin ' + pluginData.id); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:17:21 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Plugins.acpScripts = Plugins.acpScripts.concat(pluginData.acpScripts.map(function(file) { | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 				return resolveModulePath(path.join(__dirname, '../../node_modules/', pluginData.id, file), file); | 
					
						
							|  |  |  | 			})).filter(Boolean); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:17:21 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 		callback(); | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-04 20:43:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	function mapClientModules(pluginData, callback) { | 
					
						
							| 
									
										
										
										
											2016-04-27 14:28:57 -04:00
										 |  |  | 		if (!pluginData.hasOwnProperty('modules')) { | 
					
						
							|  |  |  | 			return callback(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var modules = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 20:43:21 -04:00
										 |  |  | 		if (Array.isArray(pluginData.modules)) { | 
					
						
							|  |  |  | 			if (global.env === 'development') { | 
					
						
							|  |  |  | 				winston.verbose('[plugins] Found ' + pluginData.modules.length + ' AMD-style module(s) for plugin ' + pluginData.id); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 14:28:57 -04:00
										 |  |  | 			var strip = pluginData.hasOwnProperty('modulesStrip') ? parseInt(pluginData.modulesStrip, 10) : 0; | 
					
						
							| 
									
										
										
										
											2016-04-27 14:14:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			pluginData.modules.forEach(function(file) { | 
					
						
							|  |  |  | 				if (strip) { | 
					
						
							|  |  |  | 					modules[file.replace(new RegExp('\.?(\/[^\/]+){' + strip + '}\/'), '')] = path.join('./node_modules/', pluginData.id, file); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					modules[path.basename(file)] = path.join('./node_modules/', pluginData.id, file); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			meta.js.scripts.modules = _.extend(meta.js.scripts.modules, modules); | 
					
						
							| 
									
										
										
										
											2016-04-27 14:28:57 -04:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			var keys = Object.keys(pluginData.modules); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (global.env === 'development') { | 
					
						
							|  |  |  | 				winston.verbose('[plugins] Found ' + keys.length + ' AMD-style module(s) for plugin ' + pluginData.id); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for (var name in pluginData.modules) { | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 				if (pluginData.modules.hasOwnProperty(name)) { | 
					
						
							|  |  |  | 					modules[name] = path.join('./node_modules/', pluginData.id, pluginData.modules[name]); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-04-27 14:28:57 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			meta.js.scripts.modules = _.extend(meta.js.scripts.modules, modules); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-04 20:43:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		callback(); | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	function loadLanguages(pluginData, callback) { | 
					
						
							|  |  |  | 		if (typeof pluginData.languages !== 'string') { | 
					
						
							|  |  |  | 			return callback(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-15 12:08:43 -04:00
										 |  |  | 		var pathToFolder = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages), | 
					
						
							|  |  |  | 			fallbackMap = {}; | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		utils.walk(pathToFolder, function(err, languages) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			async.each(languages, function(pathToLang, next) { | 
					
						
							|  |  |  | 				fs.readFile(pathToLang, function(err, file) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-16 10:32:28 -04:00
										 |  |  | 					var data; | 
					
						
							|  |  |  | 					var route = pathToLang.replace(pathToFolder + '/', ''); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					try { | 
					
						
							| 
									
										
										
										
											2016-05-16 10:32:28 -04:00
										 |  |  | 						data = JSON.parse(file.toString()); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 					} catch (err) { | 
					
						
							|  |  |  | 						winston.error('[plugins] Unable to parse custom language file: ' + pathToLang + '\r\n' + err.stack); | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 10:32:28 -04:00
										 |  |  | 					Plugins.customLanguages[route] = Plugins.customLanguages[route] || {}; | 
					
						
							|  |  |  | 					_.extendOwn(Plugins.customLanguages[route], data); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 10:09:02 -05:00
										 |  |  | 					if (pluginData.defaultLang && pathToLang.endsWith(pluginData.defaultLang + '/' + path.basename(pathToLang))) { | 
					
						
							| 
									
										
										
										
											2016-05-16 15:23:21 -04:00
										 |  |  | 						Plugins.languageCodes.map(function(code) { | 
					
						
							|  |  |  | 							if (pluginData.defaultLang !== code) { | 
					
						
							|  |  |  | 								return code + '/' + path.basename(pathToLang); | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								return null; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						}).filter(Boolean).forEach(function(key) { | 
					
						
							|  |  |  | 							Plugins.customLanguages[key] = _.defaults(Plugins.customLanguages[key] || {}, data); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2015-07-15 12:08:43 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 					next(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, function(err) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-07-15 12:08:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 				callback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 	function resolveModulePath(fullPath, relPath) { | 
					
						
							|  |  |  | 		/** | 
					
						
							|  |  |  | 		  * With npm@3, dependencies can become flattened, and appear at the root level. | 
					
						
							|  |  |  | 		  * This method resolves these differences if it can. | 
					
						
							|  |  |  | 		  */ | 
					
						
							| 
									
										
										
										
											2016-08-24 17:48:04 -04:00
										 |  |  | 		var matches = fullPath.match(/node_modules/g); | 
					
						
							|  |  |  | 		var atRootLevel = !matches || matches.length === 1; | 
					
						
							| 
									
										
										
										
											2016-08-09 12:32:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		try { | 
					
						
							|  |  |  | 			fs.statSync(fullPath); | 
					
						
							|  |  |  | 			winston.verbose('[plugins/load] File found: ' + fullPath); | 
					
						
							|  |  |  | 			return fullPath; | 
					
						
							|  |  |  | 		} catch (e) { | 
					
						
							|  |  |  | 			// File not visible to the calling process, ascend to root level if possible and try again
 | 
					
						
							|  |  |  | 			if (!atRootLevel && relPath) { | 
					
						
							|  |  |  | 				winston.verbose('[plugins/load] File not found: ' + fullPath + ' (Ascending)'); | 
					
						
							|  |  |  | 				return resolveModulePath(path.join(__dirname, '../..', relPath)); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// Already at root level, file was simply not found
 | 
					
						
							|  |  |  | 				winston.warn('[plugins/load] File not found: ' + fullPath + ' (Ignoring)'); | 
					
						
							|  |  |  | 				return null; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 23:29:48 -05:00
										 |  |  | 	Plugins.loadPluginInfo = function(pluginPath, callback) { | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			package: function(next) { | 
					
						
							|  |  |  | 				fs.readFile(path.join(pluginPath, 'package.json'), next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			plugin: function(next) { | 
					
						
							|  |  |  | 				fs.readFile(path.join(pluginPath, 'plugin.json'), next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				var pluginData = JSON.parse(results.plugin); | 
					
						
							|  |  |  | 				var packageData = JSON.parse(results.package); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-12 23:10:36 -05:00
										 |  |  | 				pluginData.id = packageData.name; | 
					
						
							|  |  |  | 				pluginData.name = packageData.name; | 
					
						
							|  |  |  | 				pluginData.description = packageData.description; | 
					
						
							|  |  |  | 				pluginData.version = packageData.version; | 
					
						
							|  |  |  | 				pluginData.repository = packageData.repository; | 
					
						
							|  |  |  | 				pluginData.nbbpm = packageData.nbbpm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, pluginData); | 
					
						
							| 
									
										
										
										
											2015-01-06 23:29:48 -05:00
										 |  |  | 			} catch(err) { | 
					
						
							|  |  |  | 				var pluginDir = pluginPath.split(path.sep); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:42:29 +02:00
										 |  |  | 				pluginDir = pluginDir[pluginDir.length - 1]; | 
					
						
							| 
									
										
										
										
											2015-01-06 23:29:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 18:04:27 -04:00
										 |  |  | 				winston.error('[plugins/' + pluginDir + '] Error in plugin.json or package.json! ' + err.message); | 
					
						
							| 
									
										
										
										
											2015-01-06 23:29:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 18:04:27 -04:00
										 |  |  | 				callback(new Error('[[error:parse-error]]')); | 
					
						
							| 
									
										
										
										
											2015-01-06 23:29:48 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-08-24 17:48:04 -04:00
										 |  |  | }; |