gulp-function/dist/index.js

66 lines
4.8 KiB
JavaScript
Raw Normal View History

"use strict";
2016-06-03 23:14:25 +00:00
require("typings-global");
const q = require("q");
const through2 = require("through2");
module.exports = (functionsToExecuteArg, executionModeArg = 'forEach') => {
let promiseArray = [];
let runFunction = function (functionArg, file, enc) {
let returnValue = functionArg(file, enc);
if (typeof returnValue !== 'undefined' && typeof returnValue.then !== 'undefined') {
2016-03-26 16:22:46 +00:00
promiseArray.push(returnValue);
}
};
let checkAndRunFunction = function (file, enc) {
if (typeof functionsToExecuteArg === 'function') {
runFunction(functionsToExecuteArg, file, enc);
2016-03-26 16:22:46 +00:00
}
else if (Array.isArray(functionsToExecuteArg)) {
for (let anyFunction in functionsToExecuteArg) {
runFunction(functionsToExecuteArg[anyFunction], file, enc);
2016-03-26 16:22:46 +00:00
}
}
else {
throw new Error('gulp-callfunction: something is strange with the given arguments');
2016-03-26 16:22:46 +00:00
}
return q.all(promiseArray);
2016-03-26 16:22:46 +00:00
};
let hasExecutedOnce = false;
let forEach = function (file, enc, cb) {
switch (executionModeArg) {
case 'forEach':
checkAndRunFunction(file, enc).then(function () {
2016-03-26 16:22:46 +00:00
cb(null, file);
});
break;
case 'forFirst':
if (hasExecutedOnce) {
checkAndRunFunction(file, enc)
.then(function () {
cb(null, file);
});
}
else {
2016-03-26 16:22:46 +00:00
cb(null, file);
}
2016-03-26 16:22:46 +00:00
hasExecutedOnce = true;
break;
case 'atEnd':
2016-03-26 16:22:46 +00:00
cb(null, file);
break;
default:
break;
}
};
let atEnd = function (cb) {
if (executionModeArg === 'atEnd') {
checkAndRunFunction(null, null).then(function () {
2016-03-26 16:22:46 +00:00
cb();
});
}
else {
cb();
}
};
return through2.obj(forEach, atEnd);
2016-03-26 16:22:46 +00:00
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1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