2016-04-04 20:32:30 +00:00
|
|
|
"use strict";
|
2017-04-29 22:44:11 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2016-06-03 23:14:25 +00:00
|
|
|
require("typings-global");
|
2016-10-18 22:35:41 +00:00
|
|
|
const through2 = require("through2");
|
2016-10-19 05:36:32 +00:00
|
|
|
let defaultExport = (functionsToExecuteArg, executionModeArg = 'forEach') => {
|
2016-10-18 22:35:41 +00:00
|
|
|
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
|
|
|
}
|
2017-04-29 22:44:11 +00:00
|
|
|
return Promise.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':
|
2017-04-29 23:07:30 +00:00
|
|
|
cb();
|
2016-03-26 16:22:46 +00:00
|
|
|
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-19 05:36:32 +00:00
|
|
|
exports.forEach = (funcArg) => {
|
2016-11-25 10:19:12 +00:00
|
|
|
return defaultExport(funcArg, 'forEach');
|
2016-10-19 05:36:32 +00:00
|
|
|
};
|
|
|
|
exports.forFirst = (funcArg) => {
|
2016-11-25 10:19:12 +00:00
|
|
|
return defaultExport(funcArg, 'forFirst');
|
2016-10-19 05:36:32 +00:00
|
|
|
};
|
|
|
|
exports.atEnd = (funcArg) => {
|
2016-11-25 10:19:12 +00:00
|
|
|
return defaultExport(funcArg, 'atEnd');
|
2016-10-19 05:36:32 +00:00
|
|
|
};
|
|
|
|
exports.default = defaultExport;
|
2017-04-29 23:07:30 +00:00
|
|
|
//#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