gulp-function/dist/index.js

68 lines
7.8 KiB
JavaScript
Raw Normal View History

2016-03-26 16:22:46 +00:00
/// <reference path="typings/main.d.ts" />
var plugins = {
beautylog: require("beautylog"),
Q: require("q"),
through: require("through2")
};
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 {
plugins.beautylog.error('gulp-callfunction: something is strange with the given arguments');
}
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();
}
};
return plugins.through.obj(forEach, atEnd);
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLDBDQUEwQztBQUUxQyxJQUFJLE9BQU8sR0FBRztJQUNWLFNBQVMsRUFBRSxPQUFPLENBQUMsV0FBVyxDQUFDO0lBQy9CLENBQUMsRUFBRSxPQUFPLENBQUMsR0FBRyxDQUFDO0lBQ2YsT0FBTyxFQUFFLE9BQU8sQ0FBQyxVQUFVLENBQU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