2016-04-04 20:32:30 +00:00
|
|
|
"use strict";
|
2016-06-03 23:14:25 +00:00
|
|
|
require("typings-global");
|
2016-04-04 20:32:30 +00:00
|
|
|
var plugins = require("./gulpfunction.plugins");
|
2016-03-26 16:22:46 +00:00
|
|
|
module.exports = function (functionsToExecuteArg, executionModeArg) {
|
|
|
|
if (executionModeArg === void 0) { executionModeArg = 'forEach'; }
|
|
|
|
//important vars
|
|
|
|
var executionMode = executionModeArg; //can be forEach or atEnd
|
|
|
|
var functionsToExecute = functionsToExecuteArg;
|
|
|
|
var promiseArray = [];
|
|
|
|
var runFunction = function (functionArg) {
|
|
|
|
var returnValue = functionArg();
|
|
|
|
if (typeof returnValue !== "undefined" && typeof returnValue.then !== "undefined") {
|
|
|
|
promiseArray.push(returnValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var checkAndRunFunction = function () {
|
|
|
|
if (typeof functionsToExecute === "function") {
|
|
|
|
runFunction(functionsToExecute);
|
|
|
|
}
|
|
|
|
else if (Array.isArray(functionsToExecute)) {
|
|
|
|
for (var anyFunction in functionsToExecute) {
|
|
|
|
runFunction(functionsToExecute[anyFunction]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-04-04 20:32:30 +00:00
|
|
|
throw new Error("gulp-callfunction: something is strange with the given arguments");
|
2016-03-26 16:22:46 +00:00
|
|
|
}
|
|
|
|
return plugins.Q.all(promiseArray);
|
|
|
|
};
|
|
|
|
var hasExecutedOnce = false;
|
|
|
|
var forEach = function (file, enc, cb) {
|
|
|
|
switch (executionMode) {
|
|
|
|
case "forEach":
|
|
|
|
checkAndRunFunction().then(function () {
|
|
|
|
cb(null, file);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "forFirst":
|
|
|
|
!hasExecutedOnce ? checkAndRunFunction().then(function () {
|
|
|
|
cb(null, file);
|
|
|
|
}) : cb(null, file);
|
|
|
|
hasExecutedOnce = true;
|
|
|
|
break;
|
|
|
|
case "atEnd":
|
|
|
|
cb(null, file);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var atEnd = function (cb) {
|
|
|
|
if (executionMode === "atEnd") {
|
|
|
|
checkAndRunFunction().then(function () {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
2016-04-04 20:32:30 +00:00
|
|
|
return plugins.through2.obj(forEach, atEnd);
|
2016-03-26 16:22:46 +00:00
|
|
|
};
|
|
|
|
|
2016-06-03 23:14:25 +00:00
|
|
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxRQUFPLGdCQUFnQixDQUFDLENBQUE7QUFFeEIsSUFBTyxPQUFPLFdBQVcsd0JBQXdCLENBQUMsQ0FBQztBQUtuRCxNQUFNLENBQUMsT0FBTyxHQUFHLFVBQVUscUJBQStCLEVBQUMsZ0JBQW1DO0lBQW5DLGdDQUFtQyxHQUFuQyw0QkFBbUM7SUFDMUYsZ0JBQWdCO0lBQ2hCLElBQUksYUFBYSxHQUFHLGdCQUFnQixDQUFDLENBQUMseUJBQXlCO0lBQy9ELElBQUksa0JBQWtCLEdBQUcscUJBQXFCLENBQUM7SUFDL0MsSUFBSSxZQUFZLEdBQUcsRUFBRSxDQUFDO0lBQ3RCLElBQUksV0FBVyxHQUFHLFVBQVMsV0FBVztRQUNsQyxJQUFJLFdBQVcsR0FBRyxXQUFXLEVBQUUsQ0FBQztRQUNoQyxFQUFFLENBQUMsQ0FBQyxPQUFPLFdBQVcsS0FBSyxXQUFXLElBQUksT0FBTyxXQUFXLENBQUMsSUFBSSxLQUFLLFdBQVcsQ0FBQyxDQUFDLENBQUM7WUFDaEYsWUFBWSxDQUFDLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQztRQUNuQyxDQU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