New Plugin API

- Homebridge calls single exported initializer function and passes an
API object
  - No more require() for HAP classes (doesn't play well with plugin
structure)
This commit is contained in:
Nick Farina
2015-10-18 22:20:06 -07:00
parent a3c0df1c7c
commit 27b39cbfa0
8 changed files with 155 additions and 102 deletions

View File

@@ -18,12 +18,7 @@ module.exports = {
function Plugin(pluginPath) {
this.pluginPath = pluginPath; // like "/usr/local/lib/node_modules/plugin-lockitron"
// these are exports pulled from the loaded plugin module
this.accessory = null; // single exposed accessory
this.platform = null; // single exposed platform
this.accessories = []; // array of exposed accessories
this.platforms = []; // array of exposed platforms
this.initializer; // exported function from the plugin that initializes it
}
Plugin.prototype.name = function() {
@@ -58,12 +53,8 @@ Plugin.prototype.load = function(options) {
var mainPath = path.join(this.pluginPath, main);
// try to require() it
var pluginModule = require(mainPath);
// extract all exposed accessories and platforms
this.accessories = pluginModule.accessories || {};
this.platforms = pluginModule.platforms || {};
// try to require() it and grab the exported initialization hook
this.initializer = require(mainPath);
}
Plugin.loadPackageJSON = function(pluginPath) {
@@ -141,8 +132,12 @@ Plugin.installed = function() {
// just because this path is in require.main.paths doesn't mean it necessarily exists!
if (!fs.existsSync(requirePath))
continue;
var names = fs.readdirSync(requirePath);
var names = fs.readdirSync(requirePath);
// does this path point inside a single plugin and not a directory containing plugins?
if (fs.existsSync(path.join(requirePath, "package.json")))
names = [""];
// read through each directory in this node_modules folder
for (var index2 in names) {