2016-04-04 20:32:30 +00:00
|
|
|
"use strict";
|
2016-06-03 23:14:25 +00:00
|
|
|
require("typings-global");
|
2016-10-18 22:35:41 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2016-10-18 22:35:41 +00:00
|
|
|
let checkAndRunFunction = function (file, enc) {
|
|
|
|
if (typeof functionsToExecuteArg === 'function') {
|
|
|
|
runFunction(functionsToExecuteArg, file, enc);
|
2016-03-26 16:22:46 +00:00
|
|
|
}
|
2016-10-18 22:35:41 +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 {
|
2016-10-18 22:35:41 +00:00
|
|
|
throw new Error('gulp-callfunction: something is strange with the given arguments');
|
2016-03-26 16:22:46 +00:00
|
|
|
}
|
2016-10-18 22:35:41 +00:00
|
|
|
return q.all(promiseArray);
|
2016-03-26 16:22:46 +00:00
|
|
|
};
|
2016-10-18 22:35:41 +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;
|
2016-10-18 22:35:41 +00:00
|
|
|
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-10-18 22:35:41 +00:00
|
|
|
}
|
2016-03-26 16:22:46 +00:00
|
|
|
hasExecutedOnce = true;
|
|
|
|
break;
|
2016-10-18 22:35:41 +00:00
|
|
|
case 'atEnd':
|
2016-03-26 16:22:46 +00:00
|
|
|
cb(null, file);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2016-10-18 22:35:41 +00:00
|
|
|
let atEnd = function (cb) {
|
|
|
|
if (executionModeArg === 'atEnd') {
|
|
|
|
checkAndRunFunction(null, null).then(function () {
|
2016-03-26 16:22:46 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
2016-10-18 22:35:41 +00:00
|
|
|
return through2.obj(forEach, atEnd);
|
2016-03-26 16:22:46 +00:00
|
|
|
};
|
2016-10-18 22:35:41 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLHVCQUFzQjtBQUN0QixxQ0FBb0M7QUFTcEMsTUFBTSxDQUFDLE9BQU8sR0FBRyxDQUNiLHFCQUE0RCxFQUM1RCxnQkFBZ0IsR0FBbUIsU0FBUztJQUc1QyxJQUFJLFlBQVksR0FBRyxFQUFFLENBQUE7SUFDckIsSUFBSSxXQUFXLEdBQUcsVUFBVSxXQUFXLEVBQUUsSUFBSSxFQUFFLEdBQUc7UUFDOUMsSUFBSSxXQUFXLEdBQUcsV0FBVyxDQU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