2024-03-10 22:29:56 +00:00
|
|
|
import * as smartpromise from '@push.rocks/smartpromise';
|
2018-12-09 00:19:01 +00:00
|
|
|
import * as through2 from 'through2';
|
|
|
|
import { Transform } from 'stream';
|
2016-02-14 17:36:34 +00:00
|
|
|
|
2018-12-09 00:19:01 +00:00
|
|
|
export type TExecutionMode = 'forEach' | 'forFirst' | 'atEnd';
|
2016-02-14 17:36:34 +00:00
|
|
|
|
2016-10-18 22:35:41 +00:00
|
|
|
export interface IPromiseFunction {
|
2018-01-29 22:28:03 +00:00
|
|
|
(file?, enc?): PromiseLike<any>;
|
2016-10-18 22:35:41 +00:00
|
|
|
}
|
2016-02-14 17:36:34 +00:00
|
|
|
|
2016-10-19 05:36:32 +00:00
|
|
|
let defaultExport = (
|
2017-04-29 22:44:11 +00:00
|
|
|
functionsToExecuteArg: IPromiseFunction | IPromiseFunction[],
|
2018-12-09 00:19:01 +00:00
|
|
|
executionModeArg: TExecutionMode = 'forEach'
|
2016-10-18 22:35:41 +00:00
|
|
|
): Transform => {
|
2018-01-29 22:28:03 +00:00
|
|
|
let promiseArray = [];
|
|
|
|
let runFunction = function(functionArg, file, enc) {
|
|
|
|
let returnValue = functionArg(file, enc);
|
2018-12-09 00:19:01 +00:00
|
|
|
if (typeof returnValue !== 'undefined' && typeof returnValue.then !== 'undefined') {
|
2018-01-29 22:28:03 +00:00
|
|
|
promiseArray.push(returnValue);
|
2016-10-18 22:35:41 +00:00
|
|
|
}
|
2018-01-29 22:28:03 +00:00
|
|
|
};
|
2015-09-17 20:58:19 +00:00
|
|
|
|
2018-01-29 22:28:03 +00:00
|
|
|
let checkAndRunFunction = function(file, enc) {
|
2018-12-09 00:19:01 +00:00
|
|
|
if (typeof functionsToExecuteArg === 'function') {
|
2018-01-29 22:28:03 +00:00
|
|
|
runFunction(functionsToExecuteArg, file, enc);
|
2017-04-29 22:44:11 +00:00
|
|
|
} else if (Array.isArray(functionsToExecuteArg)) {
|
|
|
|
for (let anyFunction in functionsToExecuteArg) {
|
2018-01-29 22:28:03 +00:00
|
|
|
runFunction(functionsToExecuteArg[anyFunction], file, enc);
|
2017-04-29 22:44:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-09 00:19:01 +00:00
|
|
|
throw new Error('gulp-callfunction: something is strange with the given arguments');
|
2016-10-18 22:35:41 +00:00
|
|
|
}
|
2018-01-29 22:28:03 +00:00
|
|
|
return Promise.all(promiseArray);
|
|
|
|
};
|
2015-10-25 21:45:49 +00:00
|
|
|
|
2018-01-29 22:28:03 +00:00
|
|
|
let hasExecutedOnce = false;
|
|
|
|
let forEach = function(file, enc, cb) {
|
|
|
|
// the forEach function is called for every chunk
|
2017-04-29 22:44:11 +00:00
|
|
|
switch (executionModeArg) {
|
2018-12-09 00:19:01 +00:00
|
|
|
case 'forEach':
|
2018-01-29 22:28:03 +00:00
|
|
|
checkAndRunFunction(file, enc).then(function() {
|
|
|
|
cb(null, file);
|
|
|
|
});
|
|
|
|
break;
|
2018-12-09 00:19:01 +00:00
|
|
|
case 'forFirst':
|
2017-04-29 22:44:11 +00:00
|
|
|
if (hasExecutedOnce) {
|
2018-01-29 22:28:03 +00:00
|
|
|
checkAndRunFunction(file, enc).then(function() {
|
|
|
|
cb(null, file);
|
|
|
|
});
|
2016-02-14 17:36:34 +00:00
|
|
|
} else {
|
2018-01-29 22:28:03 +00:00
|
|
|
cb(null, file);
|
2015-11-30 08:49:52 +00:00
|
|
|
}
|
2018-01-29 22:28:03 +00:00
|
|
|
hasExecutedOnce = true;
|
|
|
|
break;
|
2018-12-09 00:19:01 +00:00
|
|
|
case 'atEnd':
|
2018-01-29 22:28:03 +00:00
|
|
|
cb();
|
|
|
|
break;
|
2017-04-29 22:44:11 +00:00
|
|
|
default:
|
2018-01-29 22:28:03 +00:00
|
|
|
break;
|
2017-04-29 22:44:11 +00:00
|
|
|
}
|
2018-01-29 22:28:03 +00:00
|
|
|
};
|
2017-04-29 22:44:11 +00:00
|
|
|
|
2018-01-29 22:28:03 +00:00
|
|
|
let atEnd = function(cb) {
|
2018-12-09 00:19:01 +00:00
|
|
|
if (executionModeArg === 'atEnd') {
|
2018-01-29 22:28:03 +00:00
|
|
|
checkAndRunFunction(null, null).then(function() {
|
|
|
|
cb();
|
|
|
|
});
|
2017-04-29 22:44:11 +00:00
|
|
|
} else {
|
2018-01-29 22:28:03 +00:00
|
|
|
cb();
|
2016-10-18 22:35:41 +00:00
|
|
|
}
|
2018-01-29 22:28:03 +00:00
|
|
|
};
|
|
|
|
return through2.obj(forEach, atEnd);
|
|
|
|
};
|
2016-10-18 23:10:45 +00:00
|
|
|
|
2016-10-19 05:36:32 +00:00
|
|
|
export let forEach = (funcArg: IPromiseFunction) => {
|
2018-12-09 00:19:01 +00:00
|
|
|
return defaultExport(funcArg, 'forEach');
|
2018-01-29 22:28:03 +00:00
|
|
|
};
|
2016-10-19 05:36:32 +00:00
|
|
|
|
|
|
|
export let forFirst = (funcArg: IPromiseFunction) => {
|
2018-12-09 00:19:01 +00:00
|
|
|
return defaultExport(funcArg, 'forFirst');
|
2018-01-29 22:28:03 +00:00
|
|
|
};
|
2016-10-19 05:36:32 +00:00
|
|
|
|
|
|
|
export let atEnd = (funcArg: IPromiseFunction) => {
|
2018-12-09 00:19:01 +00:00
|
|
|
return defaultExport(funcArg, 'atEnd');
|
2018-01-29 22:28:03 +00:00
|
|
|
};
|
2016-10-19 05:36:32 +00:00
|
|
|
|
2018-01-29 22:28:03 +00:00
|
|
|
export default defaultExport;
|