UNPKG

plugin-manager

Version:

Plugin manager for providing a simple 'hook' framework for an application, similarly to Wordpress or Drupal

61 lines (56 loc) 2.02 kB
diff --git a/lib/plugin.js b/lib/plugin.js index 5c10cab..e1d4f65 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -163,28 +163,32 @@ Plugin.UNABLE_TO_READDIR = 'Unable to read directory "%s"'; * */ Plugin.prototype.loadModule = function(name) { - // Check for this module already having been loaded - if (this.modules[name]) { - this.api.logger.error(util.format(Plugin.MODULE_ALREADY_LOADED, name)) - return null; - } - - // Determine the module's full path - var module = {}; - module.path = path.join(this.basePath, name); - + var pluginPath = path.join(this.basePath, name) + , meta = {}; // Verify the module meta-data can be found and read try { - module.meta = JSON.parse(fs.readFileSync(path.join(module.path, "package.json"))); + meta = JSON.parse(fs.readFileSync(path.join(pluginPath, "package.json"))); } catch(err) { - this.api.logger.error(util.format(Plugin.UNABLE_TO_READ_MODULE, name)); + this.api.logger.error(util.format(Plugin.UNABLE_TO_READ_MODULE, pluginPath) ); return null; } + var key = meta.name || name + , title = meta.title || key; + // Check for this module already having been loaded + if (this.modules[key]) { + this.api.logger.error(util.format(Plugin.MODULE_ALREADY_LOADED, key)) + return null; + } + + // Determine the module's full path + var module = {}; + module.path = pluginPath; + module.meta = meta; // Does it have the properties we require? - if (!module.meta.name || !module.meta.description || !module.meta.version) { - this.api.logger.error(util.format(Plugin.MODULE_INVALID_SPEC, name)); + if (!key || !module.meta.description || !module.meta.version) { + this.api.logger.error(util.format(Plugin.MODULE_INVALID_SPEC, key)); return null; } @@ -192,7 +196,7 @@ Plugin.prototype.loadModule = function(name) { module.enabled = false; // All is well - store the module info and return it - this.modules[name] = module; + this.modules[key] = module; return module; }