65 lines
7.6 KiB
JavaScript
65 lines
7.6 KiB
JavaScript
"use strict";
|
|
require("typings-global");
|
|
var plugins = require("./gulpfunction.plugins");
|
|
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 {
|
|
throw new 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.through2.obj(forEach, atEnd);
|
|
};
|
|
|
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnRzIl0s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SwgZW5jLCBjYikgeyAvL3RoZSBmb3JFYWNoIGZ1bmN0aW9uIGlzIGNhbGxlZCBmb3IgZXZlcnkgY2h1bmtcclxuICAgICAgICBzd2l0Y2ggKGV4ZWN1dGlvbk1vZGUpe1xyXG4gICAgICAgICAgICBjYXNlIFwiZm9yRWFjaFwiOlxyXG4gICAgICAgICAgICAgICAgY2hlY2tBbmRSdW5GdW5jdGlvbigpLnRoZW4oZnVuY3Rpb24oKXtcclxuICAgICAgICAgICAgICAgICAgICBjYihudWxsLCBmaWxlKTtcclxuICAgICAgICAgICAgICAgIH0pO1xyXG4gICAgICAgICAgICAgICAgYnJlYWs7XHJcbiAgICAgICAgICAgIGNhc2UgXCJmb3JGaXJzdFwiOlxyXG4gICAgICAgICAgICAgICAgIWhhc0V4ZWN1dGVkT25jZSA/IGNoZWNrQW5kUnVuRnVuY3Rpb24oKS50aGVuKGZ1bmN0aW9uKCl7XHJcbiAgICAgICAgICAgICAgICAgICAgY2IobnVsbCwgZmlsZSk7XHJcbiAgICAgICAgICAgICAgICB9KSA6IGNiKG51bGwsIGZpbGUpO1xyXG4gICAgICAgICAgICAgICAgaGFzRXhlY3V0ZWRPbmNlID0gdHJ1ZTtcclxuICAgICAgICAgICAgICAgIGJyZWFrO1xyXG4gICAgICAgICAgICBjYXNlIFwiYXRFbmRcIjpcclxuICAgICAgICAgICAgICAgIGNiKG51bGwsIGZpbGUpO1xyXG4gICAgICAgICAgICAgICAgYnJlYWs7XHJcbiAgICAgICAgICAgIGRlZmF1bHQ6XHJcbiAgICAgICAgICAgICAgICBicmVhaztcclxuICAgICAgICB9XHJcbiAgICB9O1xyXG5cclxuICAgIGxldCBhdEVuZCA9IGZ1bmN0aW9uKGNiKSB7XHJcbiAgICAgICAgaWYgKGV4ZWN1dGlvbk1vZGUgPT09IFwiYXRFbmRcIikge1xyXG4gICAgICAgICAgICBjaGVja0FuZFJ1bkZ1bmN0aW9uKCkudGhlbihmdW5jdGlvbigpe1xyXG4gICAgICAgICAgICAgICAgY2IoKTtcclxuICAgICAgICAgICAgfSk7XHJcbiAgICAgICAgfSBlbHNlIHtcclxuICAgICAgICAgICAgY2IoKTtcclxuICAgICAgICB9XHJcbiAgICB9O1xyXG4gICAgcmV0dXJuIHBsdWdpbnMudGhyb3VnaDIub2JqKGZvckVhY2gsYXRFbmQpO1xyXG59O1xyXG4iXX0=
|