mirror of
https://github.com/mtan93/homebridge.git
synced 2026-05-29 22:07:56 +01:00
Merge pull request #491 from straccio/master
Added pluginPath based on npm
This commit is contained in:
@@ -10,3 +10,5 @@ npm-debug.log
|
|||||||
# Ignore any extra plugins in the example directory that aren't in Git already
|
# Ignore any extra plugins in the example directory that aren't in Git already
|
||||||
# (this is a sandbox for the user)
|
# (this is a sandbox for the user)
|
||||||
example-plugins
|
example-plugins
|
||||||
|
|
||||||
|
.idea
|
||||||
+10
-10
@@ -31,7 +31,7 @@ Logger.prototype.debug = function(msg) {
|
|||||||
if (DEBUG_ENABLED)
|
if (DEBUG_ENABLED)
|
||||||
this.log.apply(this, ['debug'].concat(Array.prototype.slice.call(arguments)));
|
this.log.apply(this, ['debug'].concat(Array.prototype.slice.call(arguments)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.prototype.info = function(msg) {
|
Logger.prototype.info = function(msg) {
|
||||||
this.log.apply(this, ['info'].concat(Array.prototype.slice.call(arguments)));
|
this.log.apply(this, ['info'].concat(Array.prototype.slice.call(arguments)));
|
||||||
}
|
}
|
||||||
@@ -43,35 +43,35 @@ Logger.prototype.warn = function(msg) {
|
|||||||
Logger.prototype.error = function(msg) {
|
Logger.prototype.error = function(msg) {
|
||||||
this.log.apply(this, ['error'].concat(Array.prototype.slice.call(arguments)));
|
this.log.apply(this, ['error'].concat(Array.prototype.slice.call(arguments)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.prototype.log = function(level, msg) {
|
Logger.prototype.log = function(level, msg) {
|
||||||
|
|
||||||
msg = util.format.apply(util, Array.prototype.slice.call(arguments, 1));
|
msg = util.format.apply(util, Array.prototype.slice.call(arguments, 1));
|
||||||
func = console.log;
|
func = console.log;
|
||||||
|
|
||||||
if (level == 'debug') {
|
if (level == 'debug') {
|
||||||
msg = chalk.gray(msg);
|
msg = chalk.gray(msg);
|
||||||
}
|
}
|
||||||
else if (level == 'warn') {
|
else if (level == 'warn') {
|
||||||
msg = chalk.yellow(msg);
|
msg = chalk.yellow(msg);
|
||||||
func = console.error;
|
func = console.error;
|
||||||
}
|
}
|
||||||
else if (level == 'error') {
|
else if (level == 'error') {
|
||||||
msg = chalk.bold.red(msg);
|
msg = chalk.bold.red(msg);
|
||||||
func = console.error;
|
func = console.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepend prefix if applicable
|
// prepend prefix if applicable
|
||||||
if (this.prefix)
|
if (this.prefix)
|
||||||
msg = chalk.cyan("[" + this.prefix + "]") + " " + msg;
|
msg = chalk.cyan("[" + this.prefix + "]") + " " + msg;
|
||||||
|
|
||||||
// prepend timestamp
|
// prepend timestamp
|
||||||
var date = new Date();
|
var date = new Date();
|
||||||
msg = "[" + date.toLocaleString() + "]" + " " + msg;
|
msg = chalk.white("[" + date.toLocaleString() + "]") + " " + msg;
|
||||||
|
|
||||||
func(msg);
|
func(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.withPrefix = function(prefix) {
|
Logger.withPrefix = function(prefix) {
|
||||||
|
|
||||||
if (!loggerCache[prefix]) {
|
if (!loggerCache[prefix]) {
|
||||||
@@ -87,6 +87,6 @@ Logger.withPrefix = function(prefix) {
|
|||||||
log.prefix = logger.prefix;
|
log.prefix = logger.prefix;
|
||||||
loggerCache[prefix] = log;
|
loggerCache[prefix] = log;
|
||||||
}
|
}
|
||||||
|
|
||||||
return loggerCache[prefix];
|
return loggerCache[prefix];
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-25
@@ -13,7 +13,7 @@ module.exports = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Homebridge Plugin.
|
* Homebridge Plugin.
|
||||||
*
|
*
|
||||||
* Allows for discovering and loading installed Homebridge plugins.
|
* Allows for discovering and loading installed Homebridge plugins.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,39 +28,39 @@ Plugin.prototype.name = function() {
|
|||||||
|
|
||||||
Plugin.prototype.load = function(options) {
|
Plugin.prototype.load = function(options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
// does this plugin exist at all?
|
// does this plugin exist at all?
|
||||||
if (!fs.existsSync(this.pluginPath)) {
|
if (!fs.existsSync(this.pluginPath)) {
|
||||||
throw new Error("Plugin " + this.pluginPath + " was not found. Make sure the module '" + this.pluginPath + "' is installed.");
|
throw new Error("Plugin " + this.pluginPath + " was not found. Make sure the module '" + this.pluginPath + "' is installed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// attempt to load package.json
|
// attempt to load package.json
|
||||||
var pjson = Plugin.loadPackageJSON(this.pluginPath);
|
var pjson = Plugin.loadPackageJSON(this.pluginPath);
|
||||||
|
|
||||||
// very temporary fix for first wave of plugins
|
// very temporary fix for first wave of plugins
|
||||||
if (pjson.peerDepdendencies && (!pjson.engines || !pjson.engines.homebridge)) {
|
if (pjson.peerDepdendencies && (!pjson.engines || !pjson.engines.homebridge)) {
|
||||||
var engines = pjson.engines || {}
|
var engines = pjson.engines || {}
|
||||||
engines.homebridge = pjson.peerDepdendencies.homebridge;
|
engines.homebridge = pjson.peerDepdendencies.homebridge;
|
||||||
pjson.engines = engines;
|
pjson.engines = engines;
|
||||||
}
|
}
|
||||||
|
|
||||||
// pluck out the HomeBridge version requirement
|
// pluck out the HomeBridge version requirement
|
||||||
if (!pjson.engines || !pjson.engines.homebridge) {
|
if (!pjson.engines || !pjson.engines.homebridge) {
|
||||||
throw new Error("Plugin " + this.pluginPath + " does not contain the 'homebridge' package in 'engines'.");
|
throw new Error("Plugin " + this.pluginPath + " does not contain the 'homebridge' package in 'engines'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var versionRequired = pjson.engines.homebridge;
|
var versionRequired = pjson.engines.homebridge;
|
||||||
|
|
||||||
// make sure the version is satisfied by the currently running version of HomeBridge
|
// make sure the version is satisfied by the currently running version of HomeBridge
|
||||||
if (!semver.satisfies(version, versionRequired)) {
|
if (!semver.satisfies(version, versionRequired)) {
|
||||||
throw new Error("Plugin " + this.pluginPath + " requires a HomeBridge version of " + versionRequired + " which does not satisfy the current HomeBridge version of " + version + ". You may need to upgrade your installation of HomeBridge.");
|
throw new Error("Plugin " + this.pluginPath + " requires a HomeBridge version of " + versionRequired + " which does not satisfy the current HomeBridge version of " + version + ". You may need to upgrade your installation of HomeBridge.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// figure out the main module - index.js unless otherwise specified
|
// figure out the main module - index.js unless otherwise specified
|
||||||
var main = pjson.main || "./index.js";
|
var main = pjson.main || "./index.js";
|
||||||
|
|
||||||
var mainPath = path.join(this.pluginPath, main);
|
var mainPath = path.join(this.pluginPath, main);
|
||||||
|
|
||||||
// try to require() it and grab the exported initialization hook
|
// try to require() it and grab the exported initialization hook
|
||||||
this.initializer = require(mainPath);
|
this.initializer = require(mainPath);
|
||||||
}
|
}
|
||||||
@@ -69,11 +69,11 @@ Plugin.loadPackageJSON = function(pluginPath) {
|
|||||||
// check for a package.json
|
// check for a package.json
|
||||||
var pjsonPath = path.join(pluginPath, "package.json");
|
var pjsonPath = path.join(pluginPath, "package.json");
|
||||||
var pjson = null;
|
var pjson = null;
|
||||||
|
|
||||||
if (!fs.existsSync(pjsonPath)) {
|
if (!fs.existsSync(pjsonPath)) {
|
||||||
throw new Error("Plugin " + pluginPath + " does not contain a package.json.");
|
throw new Error("Plugin " + pluginPath + " does not contain a package.json.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// attempt to parse package.json
|
// attempt to parse package.json
|
||||||
pjson = JSON.parse(fs.readFileSync(pjsonPath));
|
pjson = JSON.parse(fs.readFileSync(pjsonPath));
|
||||||
@@ -81,7 +81,7 @@ Plugin.loadPackageJSON = function(pluginPath) {
|
|||||||
catch (err) {
|
catch (err) {
|
||||||
throw new Error("Plugin " + pluginPath + " contains an invalid package.json. Error: " + err);
|
throw new Error("Plugin " + pluginPath + " contains an invalid package.json. Error: " + err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure the name is prefixed with 'homebridge-'
|
// make sure the name is prefixed with 'homebridge-'
|
||||||
if (!pjson.name || pjson.name.indexOf('homebridge-') != 0) {
|
if (!pjson.name || pjson.name.indexOf('homebridge-') != 0) {
|
||||||
throw new Error("Plugin " + pluginPath + " does not have a package name that begins with 'homebridge-'.");
|
throw new Error("Plugin " + pluginPath + " does not have a package name that begins with 'homebridge-'.");
|
||||||
@@ -91,7 +91,7 @@ Plugin.loadPackageJSON = function(pluginPath) {
|
|||||||
if (!pjson.keywords || pjson.keywords.indexOf("homebridge-plugin") == -1) {
|
if (!pjson.keywords || pjson.keywords.indexOf("homebridge-plugin") == -1) {
|
||||||
throw new Error("Plugin " + pluginPath + " package.json does not contain the keyword 'homebridge-plugin'.");
|
throw new Error("Plugin " + pluginPath + " package.json does not contain the keyword 'homebridge-plugin'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return pjson;
|
return pjson;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,9 +119,10 @@ Plugin.getDefaultPaths = function() {
|
|||||||
} else {
|
} else {
|
||||||
paths.push('/usr/local/lib/node_modules');
|
paths.push('/usr/local/lib/node_modules');
|
||||||
paths.push('/usr/lib/node_modules');
|
paths.push('/usr/lib/node_modules');
|
||||||
|
const exec = require('child_process').execSync;
|
||||||
|
paths.push(exec('/bin/echo -n "$(npm -g prefix)/lib/node_modules"').toString('utf8'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,17 +139,17 @@ Plugin.installed = function() {
|
|||||||
var plugins = [];
|
var plugins = [];
|
||||||
var pluginsByName = {}; // don't add duplicate plugins
|
var pluginsByName = {}; // don't add duplicate plugins
|
||||||
var searchedPaths = {}; // don't search the same paths twice
|
var searchedPaths = {}; // don't search the same paths twice
|
||||||
|
|
||||||
// search for plugins among all known paths, in order
|
// search for plugins among all known paths, in order
|
||||||
for (var index in Plugin.paths) {
|
for (var index in Plugin.paths) {
|
||||||
var requirePath = Plugin.paths[index];
|
var requirePath = Plugin.paths[index];
|
||||||
|
|
||||||
// did we already search this path?
|
// did we already search this path?
|
||||||
if (searchedPaths[requirePath])
|
if (searchedPaths[requirePath])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
searchedPaths[requirePath] = true;
|
searchedPaths[requirePath] = true;
|
||||||
|
|
||||||
// just because this path is in require.main.paths doesn't mean it necessarily exists!
|
// just because this path is in require.main.paths doesn't mean it necessarily exists!
|
||||||
if (!fs.existsSync(requirePath))
|
if (!fs.existsSync(requirePath))
|
||||||
continue;
|
continue;
|
||||||
@@ -158,21 +159,19 @@ Plugin.installed = function() {
|
|||||||
// does this path point inside a single plugin and not a directory containing plugins?
|
// does this path point inside a single plugin and not a directory containing plugins?
|
||||||
if (fs.existsSync(path.join(requirePath, "package.json")))
|
if (fs.existsSync(path.join(requirePath, "package.json")))
|
||||||
names = [""];
|
names = [""];
|
||||||
|
|
||||||
// read through each directory in this node_modules folder
|
// read through each directory in this node_modules folder
|
||||||
for (var index2 in names) {
|
for (var index2 in names) {
|
||||||
var name = names[index2];
|
var name = names[index2];
|
||||||
|
|
||||||
// reconstruct full path
|
// reconstruct full path
|
||||||
var pluginPath = path.join(requirePath, name);
|
var pluginPath = path.join(requirePath, name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// we only care about directories
|
// we only care about directories
|
||||||
if (!fs.statSync(pluginPath).isDirectory()) continue;
|
if (!fs.statSync(pluginPath).isDirectory()) continue;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// does this module contain a package.json?
|
// does this module contain a package.json?
|
||||||
var pjson;
|
var pjson;
|
||||||
try {
|
try {
|
||||||
@@ -184,14 +183,14 @@ Plugin.installed = function() {
|
|||||||
if (!name || name.indexOf('homebridge-') == 0) {
|
if (!name || name.indexOf('homebridge-') == 0) {
|
||||||
log.warn(err.message);
|
log.warn(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip this module
|
// skip this module
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get actual name if this path points inside a single plugin
|
// get actual name if this path points inside a single plugin
|
||||||
if (!name) name = pjson.name;
|
if (!name) name = pjson.name;
|
||||||
|
|
||||||
// add it to the return list
|
// add it to the return list
|
||||||
if (!pluginsByName[name]) {
|
if (!pluginsByName[name]) {
|
||||||
pluginsByName[name] = pluginPath;
|
pluginsByName[name] = pluginPath;
|
||||||
@@ -202,6 +201,6 @@ Plugin.installed = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return plugins;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user