Comparing version 1.2.1 to 1.3.0
@@ -17,4 +17,3 @@ module.exports = Automait | ||
var loadTriggers = createTriggerLoader(this.plugins, this.logger) | ||
loadTriggers(this.config.triggers) | ||
createTriggerLoader(this.plugins, this.logger)() | ||
@@ -21,0 +20,0 @@ Object.keys(this.plugins).forEach(function (name) { |
module.exports = createTriggerLoader | ||
var async = require('async') | ||
, glob = require('glob') | ||
function createTriggerLoader(plugins, logger) { | ||
function loadTriggers(triggers) { | ||
triggers = triggers.filter(function (trigger) { | ||
return trigger.active && trigger.when && trigger.then | ||
function loadTriggers() { | ||
var triggers = [] | ||
, triggerDir = __dirname + '/../../../triggers/**/*.json' | ||
glob(triggerDir, function (error, paths) { | ||
paths.forEach(function (path) { | ||
var triggerData = null | ||
try { | ||
triggerData = require(path) | ||
} catch (e) { | ||
throw new Error('Invalid trigger file at: ' + path) | ||
} | ||
if (!Array.isArray(triggerData)) { | ||
triggerData = [ triggerData ] | ||
} | ||
triggers = triggers.concat(triggerData) | ||
}) | ||
triggers = triggers.filter(function (trigger) { | ||
return trigger.active && trigger.when && trigger.then | ||
}) | ||
triggers.forEach(loadSingleTrigger) | ||
}) | ||
triggers.forEach(loadSingleTrigger) | ||
} | ||
@@ -56,20 +74,30 @@ | ||
if (!conditions) return callback(true) | ||
async.every(conditions | ||
, function (cond, evCb) { | ||
var plugin = plugins[cond.plugin] | ||
, args = cond.action.input.slice(0) | ||
async.every(conditions, checkCondition, callback) | ||
} | ||
function checkResult(error, result) { | ||
if (error) { | ||
logger.error('Error checking condition:', cond.plugin, cond.action) | ||
return logger.error(error) | ||
} | ||
evCb(cond.action.equal === result) | ||
} | ||
function checkCondition(cond, evCb) { | ||
// handle OR conditions | ||
if (Array.isArray(cond)) { | ||
return async.some(cond, checkCondition, evCb) | ||
} | ||
var plugin = plugins[cond.plugin] | ||
, args = cond.action.input.slice(0) | ||
args.push(checkResult) | ||
plugin[cond.action.name].apply(plugin, args) | ||
function checkResult(error, result) { | ||
if (error) { | ||
logger.error('Error checking condition:', cond.plugin, cond.action) | ||
return logger.error(error) | ||
} | ||
, callback | ||
) | ||
var possibleValues = cond.action.equal | ||
if (!Array.isArray(possibleValues)) { | ||
possibleValues = [ possibleValues ] | ||
} | ||
evCb(possibleValues.indexOf(result) > -1) | ||
} | ||
args.push(checkResult) | ||
if (!plugin[cond.action.name]) { | ||
throw new Error('Plugin "' + cond.plugin + '" does not have an action called "' + cond.action.name + '"') | ||
} | ||
plugin[cond.action.name].apply(plugin, args) | ||
} | ||
@@ -76,0 +104,0 @@ |
{ | ||
"name": "automait", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"main": "index.js", | ||
@@ -8,4 +8,5 @@ "license": "ISC", | ||
"async": "^1.5.0", | ||
"bunyan": "^1.5.1" | ||
"bunyan": "^1.5.1", | ||
"glob": "^7.1.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7727
164
3
4
+ Addedglob@^7.1.2
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)