2016-06-03 23:14:25 +00:00
|
|
|
import "typings-global";
|
2016-02-14 17:36:34 +00:00
|
|
|
|
2016-04-04 20:32:30 +00:00
|
|
|
import plugins = require("./gulpfunction.plugins");
|
2016-02-14 17:36:34 +00:00
|
|
|
|
|
|
|
|
2015-09-17 20:58:19 +00:00
|
|
|
|
2015-10-26 15:18:37 +00:00
|
|
|
|
2016-02-12 04:36:23 +00:00
|
|
|
module.exports = function (functionsToExecuteArg:any|any[],executionModeArg:string = 'forEach') {
|
2015-11-30 08:49:52 +00:00
|
|
|
//important vars
|
2016-03-26 16:22:46 +00:00
|
|
|
let executionMode = executionModeArg; //can be forEach or atEnd
|
|
|
|
let functionsToExecute = functionsToExecuteArg;
|
|
|
|
let promiseArray = [];
|
|
|
|
let runFunction = function(functionArg){
|
|
|
|
let returnValue = functionArg();
|
2016-02-14 17:36:34 +00:00
|
|
|
if (typeof returnValue !== "undefined" && typeof returnValue.then !== "undefined") {
|
|
|
|
promiseArray.push(returnValue);
|
|
|
|
}
|
2015-11-30 08:49:52 +00:00
|
|
|
};
|
|
|
|
|
2016-03-26 16:22:46 +00:00
|
|
|
let checkAndRunFunction = function () {
|
2016-02-14 17:36:34 +00:00
|
|
|
if (typeof functionsToExecute === "function" ) {
|
|
|
|
runFunction(functionsToExecute);
|
|
|
|
} else if (Array.isArray(functionsToExecute)) {
|
2016-03-26 16:22:46 +00:00
|
|
|
for (let anyFunction in functionsToExecute) {
|
2016-02-14 17:36:34 +00:00
|
|
|
runFunction(functionsToExecute[anyFunction]);
|
2015-11-30 08:49:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-04 20:32:30 +00:00
|
|
|
throw new Error("gulp-callfunction: something is strange with the given arguments");
|
2015-10-25 21:45:49 +00:00
|
|
|
}
|
2016-02-14 17:36:34 +00:00
|
|
|
return plugins.Q.all(promiseArray);
|
2015-11-30 08:49:52 +00:00
|
|
|
};
|
2015-09-17 20:58:19 +00:00
|
|
|
|
2016-03-26 16:22:46 +00:00
|
|
|
let hasExecutedOnce = false;
|
|
|
|
let forEach = function (file, enc, cb) { //the forEach function is called for every chunk
|
|
|
|
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":
|
2016-02-14 17:36:34 +00:00
|
|
|
cb(null, file);
|
2016-03-26 16:22:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-11-30 08:49:52 +00:00
|
|
|
}
|
|
|
|
};
|
2015-10-25 21:45:49 +00:00
|
|
|
|
2016-03-26 16:22:46 +00:00
|
|
|
let atEnd = function(cb) {
|
2016-02-14 17:36:34 +00:00
|
|
|
if (executionMode === "atEnd") {
|
|
|
|
checkAndRunFunction().then(function(){
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb();
|
2015-11-30 08:49:52 +00:00
|
|
|
}
|
|
|
|
};
|
2016-04-04 20:32:30 +00:00
|
|
|
return plugins.through2.obj(forEach,atEnd);
|
2015-09-17 20:58:19 +00:00
|
|
|
};
|