gulp-function/ts/index.ts

61 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2016-02-01 02:33:33 +00:00
/// <reference path="typings/main.d.ts" />
var plugins = {
beautylog: require("beautylog"),
Q: require("q"),
through: require("through2")
}
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
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);
}
2015-11-30 08:49:52 +00:00
};
var checkAndRunFunction = function () {
if (typeof functionsToExecute === "function" ) {
runFunction(functionsToExecute);
} else if (Array.isArray(functionsToExecute)) {
for (var anyFunction in functionsToExecute) {
runFunction(functionsToExecute[anyFunction]);
2015-11-30 08:49:52 +00:00
}
} else {
plugins.beautylog.error('gulp-callfunction: something is strange with the given arguments');
2015-10-25 21:45:49 +00:00
}
return plugins.Q.all(promiseArray);
2015-11-30 08:49:52 +00:00
};
2015-09-17 20:58:19 +00:00
2015-11-30 08:49:52 +00:00
var forEach = function (file, enc, cb) {
if (executionMode === 'forEach') {
checkAndRunFunction().then(function(){
cb(null, file);
});
} else {
cb(null, file);
2015-11-30 08:49:52 +00:00
}
//tell gulp that we are complete
2015-11-30 08:49:52 +00:00
};
2015-10-25 21:45:49 +00:00
2015-11-30 08:49:52 +00:00
var atEnd = function(cb) {
if (executionMode === "atEnd") {
checkAndRunFunction().then(function(){
cb();
});
} else {
cb();
2015-11-30 08:49:52 +00:00
}
};
return plugins.through.obj(forEach,atEnd);
2015-09-17 20:58:19 +00:00
};