gulp-function/dist/index.js

77 lines
5.5 KiB
JavaScript

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