2016-03-26 16:22:46 +00:00
|
|
|
/// <reference path="typings/main.d.ts" />
|
2016-04-04 20:32:30 +00:00
|
|
|
"use strict";
|
|
|
|
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-04-04 20:32:30 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLDBDQUEwQzs7QUFFMUMsSUFBTyxPQUFPLFdBQVcsd0JBQXdCLENBQUMsQ0FBQztBQUtuRCxNQUFNLENBQUMsT0FBTyxHQUFHLFVBQVUscUJBQStCLEVBQUMsZ0JBQW1DO0lBQW5DLGdDQUFtQyxHQUFuQyw0QkFBbUM7SUFDMUYsZ0JBQWdCO0lBQ2hCLElBQUksYUFBYSxHQUFHLGdCQUFnQixDQUFDLENBQUMseUJBQXlCO0lBQy9ELElBQUksa0JBQWtCLEdBQUcscUJBQXFCLENBQUM7SUFDL0MsSUFBSSxZQUFZLEdBQUcsRUFBRSxDQUFDO0lBQ3RCLElBQUksV0FBVyxHQUFHLFVBQVMsV0FBVztRQUNsQyxJQUFJLFdBQVcsR0FBRyxXQUFXLEVBQUUsQ0FBQztRQUNoQyxFQUFFLENBQUMsQ0FBQyxPQUFPLFdBQVcsS0FBSyxXQUFXLElBQUksT0FBTyxXQUFXLENBQUMsSUFBSSxLQUFLLFdBQVcsQ0FBQyxDQUFDLENBQUM7WUFDaEYsWUFBWSxDQUFDLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQztRQUNuQyxDQUFDO0lBQ0wsQ0FBQyxDQUFDO0lBRUYs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