2016-10-18 22:35:41 +00:00
|
|
|
import 'typings-global'
|
|
|
|
import * as q from 'q'
|
|
|
|
import * as through2 from 'through2'
|
|
|
|
import { Transform } from 'stream'
|
2016-02-14 17:36:34 +00:00
|
|
|
|
2016-10-18 22:35:41 +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 {
|
|
|
|
(file?, enc?): PromiseLike<any>
|
|
|
|
}
|
2016-02-14 17:36:34 +00:00
|
|
|
|
2016-10-18 22:35:41 +00:00
|
|
|
module.exports = (
|
|
|
|
functionsToExecuteArg: IPromiseFunction | IPromiseFunction[],
|
|
|
|
executionModeArg: TExecutionMode = 'forEach'
|
|
|
|
): Transform => {
|
2015-09-17 20:58:19 +00:00
|
|
|
|
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') {
|
|
|
|
promiseArray.push(returnValue)
|
2016-02-14 17:36:34 +00:00
|
|
|
}
|
2016-10-18 22:35:41 +00:00
|
|
|
}
|
2015-11-30 08:49:52 +00:00
|
|
|
|
2016-10-18 22:35:41 +00:00
|
|
|
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)
|
2015-11-30 08:49:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-18 22:35:41 +00:00
|
|
|
throw new Error('gulp-callfunction: something is strange with the given arguments')
|
2015-10-25 21:45:49 +00:00
|
|
|
}
|
2016-10-18 22:35:41 +00:00
|
|
|
return q.all(promiseArray)
|
|
|
|
}
|
2015-09-17 20:58:19 +00:00
|
|
|
|
2016-10-18 22:35:41 +00:00
|
|
|
let hasExecutedOnce = false
|
|
|
|
let forEach = function (file, enc, cb) { // the forEach function is called for every chunk
|
|
|
|
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
|
2016-03-26 16:22:46 +00:00
|
|
|
default:
|
2016-10-18 22:35:41 +00:00
|
|
|
break
|
2015-11-30 08:49:52 +00:00
|
|
|
}
|
2016-10-18 22:35:41 +00:00
|
|
|
}
|
2015-10-25 21:45:49 +00:00
|
|
|
|
2016-10-18 22:35:41 +00:00
|
|
|
let atEnd = function (cb) {
|
|
|
|
if (executionModeArg === 'atEnd') {
|
|
|
|
checkAndRunFunction(null, null).then(function () {
|
|
|
|
cb()
|
|
|
|
})
|
2016-02-14 17:36:34 +00:00
|
|
|
} else {
|
2016-10-18 22:35:41 +00:00
|
|
|
cb()
|
2015-11-30 08:49:52 +00:00
|
|
|
}
|
2016-10-18 22:35:41 +00:00
|
|
|
}
|
|
|
|
return through2.obj(forEach, atEnd)
|
|
|
|
}
|