gulp-function/dist/index.js

65 lines
7.5 KiB
JavaScript

/// <reference path="typings/main.d.ts" />
"use strict";
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;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLDBDQUEwQzs7QUFFMUMsSUFBTyxPQUFPLFdBQVcsd0JBQXdCLENBQUMsQ0FBQztBQUtuRCxNQUFNLENBQUMsT0FBTyxHQUFHLFVBQVUscUJBQStCLEVBQUMsZ0JBQW1DO0lBQW5DLGdDQUFtQyxHQUFuQyw0QkFBbUM7SUFDMUYsZ0JBQWdCO0lBQ2hCLElBQUksYUFBYSxHQUFHLGdCQUFnQixDQUF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4gICAgICAgICAgICBjYXNlIFwiZm9yRmlyc3RcIjpcbiAgICAgICAgICAgICAgICAhaGFzRXhlY3V0ZWRPbmNlID8gY2hlY2tBbmRSdW5GdW5jdGlvbigpLnRoZW4oZnVuY3Rpb24oKXtcbiAgICAgICAgICAgICAgICAgICAgY2IobnVsbCwgZmlsZSk7XG4gICAgICAgICAgICAgICAgfSkgOiBjYihudWxsLCBmaWxlKTtcbiAgICAgICAgICAgICAgICBoYXNFeGVjdXRlZE9uY2UgPSB0cnVlO1xuICAgICAgICAgICAgICAgIGJyZWFrO1xuICAgICAgICAgICAgY2FzZSBcImF0RW5kXCI6XG4gICAgICAgICAgICAgICAgY2IobnVsbCwgZmlsZSk7XG4gICAgICAgICAgICAgICAgYnJlYWs7XG4gICAgICAgICAgICBkZWZhdWx0OlxuICAgICAgICAgICAgICAgIGJyZWFrO1xuICAgICAgICB9XG4gICAgfTtcblxuICAgIGxldCBhdEVuZCA9IGZ1bmN0aW9uKGNiKSB7XG4gICAgICAgIGlmIChleGVjdXRpb25Nb2RlID09PSBcImF0RW5kXCIpIHtcbiAgICAgICAgICAgIGNoZWNrQW5kUnVuRnVuY3Rpb24oKS50aGVuKGZ1bmN0aW9uKCl7XG4gICAgICAgICAgICAgICAgY2IoKTtcbiAgICAgICAgICAgIH0pO1xuICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgY2IoKTtcbiAgICAgICAgfVxuICAgIH07XG4gICAgcmV0dXJuIHBsdWdpbnMudGhyb3VnaDIub2JqKGZvckVhY2gsYXRFbmQpO1xufTtcbiJdLCJzb3VyY2VSb290IjoiL3NvdXJjZS8ifQ==