From aeff3c9cf5b818150b83796984a0e187f05d65e4 Mon Sep 17 00:00:00 2001 From: visualasparagus Date: Wed, 16 Dec 2015 00:44:25 +0100 Subject: [PATCH] Centralized and fixed event.json loading Refactored event.json loading code to be a call to a central function as opposed to duplicating the same code. See _loadTestEvent on line 159 and line 72 Referenced absolute path to event.json file in order to correctly load the file. See 162. --- lib/actions/FunctionRun.js | 43 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/actions/FunctionRun.js b/lib/actions/FunctionRun.js index 514538fa7..43fe352fb 100644 --- a/lib/actions/FunctionRun.js +++ b/lib/actions/FunctionRun.js @@ -69,6 +69,7 @@ class FunctionRun extends SPlugin { return _this._loadFunction(evt) .bind(_this) + .then(_this._loadTestEvent) .then(_this._runFunction) .then(function(evt) { return evt; @@ -97,12 +98,6 @@ class FunctionRun extends SPlugin { // Load event object evt.function = functions[0]; - let eventPath = evt.function.pathFunction.replace('s-function.json', ''); - if (fs.existsSync(path.join(eventPath, 'event.json'))) { - evt.function.event = SUtils.readAndParseJsonSync(path.join(eventPath, 'event.json')); - } else { - evt.function.event = {}; - } // Return return evt; @@ -122,20 +117,14 @@ class FunctionRun extends SPlugin { // Add function to evt for (let i = 0; i < functions.length; i++) { + SUtils.sDebug(functions[i].name+" :: "+evt.name); + if (functions[i].name === evt.name || functions[i].name.toLowerCase() === evt.name) { evt.function = functions[i]; } } if (!evt.function) throw new SError(`Could not find a function with the name ${evt.name}`); - // Load event object - let eventPath = evt.function.pathFunction.replace('s-function.json', ''); - if (fs.existsSync(path.join(eventPath, 'event.json'))) { - evt.function.event = SUtils.readAndParseJsonSync(path.join(eventPath, 'event.json')); - } else { - evt.function.event = {}; - } - // Return return evt; }); @@ -155,12 +144,6 @@ class FunctionRun extends SPlugin { evt.function = selected[0]; - let eventPath = evt.function.pathFunction.replace('s-function.json', ''); - if (fs.existsSync(path.join(eventPath, 'event.json'))) { - evt.function.event = SUtils.readAndParseJsonSync(path.join(eventPath, 'event.json')); - } else { - evt.function.event = {}; - } return evt; }); } @@ -169,6 +152,24 @@ class FunctionRun extends SPlugin { throw new SError(`No function specified`); } + /** + * Populate the test event from the correct event.json file + */ + + _loadTestEvent(evt) { + + let _this = this, + eventPath = path.join(_this.S._projectRootPath, evt.function.pathFunction.replace('s-function.json', ''), 'event.json'); + + if (fs.existsSync(eventPath)) { + evt.function.event = SUtils.readAndParseJsonSync(eventPath); + } else { + evt.function.event = {}; + } + + return evt; + } + /** * Run Function By Runtime */ @@ -196,4 +197,4 @@ class FunctionRun extends SPlugin { } } -module.exports = FunctionRun; \ No newline at end of file +module.exports = FunctionRun;