gulp-function/dist/index.js

76 lines
5.5 KiB
JavaScript
Raw Normal View History

"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");
const through2 = require("through2");
2016-10-19 05:36:32 +00:00
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') {
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
}
2017-04-29 22:44:11 +00:00
return Promise.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-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 22:44:11 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBCQUF1QjtBQUV2QixxQ0FBb0M7QUFTcEMsSUFBSSxhQUFhLEdBQUcsQ0FDbEIscUJBQTRELEVBQzVELG1CQUFtQyxTQUFTO0lBRzVDLElBQUksWUFBWSxHQUFHLEVBQUUsQ0FBQTtJQUNyQixJQUFJLFdBQVcsR0FBRyxVQUFVLFdBQVcsRUFBRSxJQUFJLEVBQUUsR0FBRztRQUNoRCxJQUFJLFdBQVcsR0FBRyxXQUFXLENBQUMsSUFBSSxFQUFFLEdBQUcsQ0FBQyxDQUFBO1FBQ3hDLEVBQUUsQ0FBQyxDQUFDLE9BQU8sV0FBVyxLQUFLLFdBQVcsSUFBSSxPQUFPLFdBQVcsQ0FBQyxJQUFJLEtBQUssV0FBVyxDQUFDLENBQUMsQ0FBQztZQUNsRixZQUFZLENBQUMsSUFBSSxDQUFDLFdBQVcsQ0FBQyxDQUFBO1FBQ2hDLENBQUM7SUFDSCxDQUFDLENBQUE7SUFFRCxJQUFJLG1CQUFtQixHQUFHLFVBQVUsSUFBSSxFQUFFLEdBQUc7UUFDM0MsRUFBRSxDQUFDLENBQUMsT0FBTyxxQkFBcUIsS0FBSyxVQUFVLENBQUMsQ0FBQyxDQUFDO1lBQ2hELFdBQVcsQ0FBQyxxQkFBcUI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