gulp-function/dist/index.js

68 lines
4.9 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");
2016-10-18 23:10:45 +00:00
let mainExportFunction = (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
};
2016-10-18 23:10:45 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = mainExportFunction;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJ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