gulp-function/index.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-02-01 02:33:33 +00:00
#!/usr/bin/env node
/// <reference path="typings/main.d.ts" />
var plugins = {
beautylog: require("beautylog"),
Q: require("q"),
through: require("through2")
};
2016-02-12 04:36:23 +00:00
module.exports = function (functionsToExecuteArg, executionModeArg) {
2016-02-01 02:33:33 +00:00
if (executionModeArg === void 0) { executionModeArg = '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);
2015-11-30 08:49:52 +00:00
}
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-11-30 08:49:52 +00:00
}
return plugins.Q.all(promiseArray);
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
};
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-10-25 19:02:14 +00:00
};