add possibility to modify the file object
This commit is contained in:
		@@ -1,4 +0,0 @@
 | 
			
		||||
import "typings-global";
 | 
			
		||||
 | 
			
		||||
export let Q = require("q");
 | 
			
		||||
export let through2 = require("through2");
 | 
			
		||||
							
								
								
									
										110
									
								
								ts/index.ts
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								ts/index.ts
									
									
									
									
									
								
							@@ -1,65 +1,75 @@
 | 
			
		||||
import "typings-global";
 | 
			
		||||
import 'typings-global'
 | 
			
		||||
import * as q from 'q'
 | 
			
		||||
import * as through2 from 'through2'
 | 
			
		||||
import { Transform } from 'stream'
 | 
			
		||||
 | 
			
		||||
import plugins = require("./gulpfunction.plugins");
 | 
			
		||||
export type TExecutionMode = 'forEach' | 'forFirst' | 'atEnd'
 | 
			
		||||
 | 
			
		||||
export interface IPromiseFunction {
 | 
			
		||||
    (file?, enc?): PromiseLike<any>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = (
 | 
			
		||||
    functionsToExecuteArg: IPromiseFunction | IPromiseFunction[],
 | 
			
		||||
    executionModeArg: TExecutionMode = 'forEach'
 | 
			
		||||
): Transform => {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports = function (functionsToExecuteArg:any|any[],executionModeArg:string = 'forEach') {
 | 
			
		||||
    //important vars
 | 
			
		||||
    let executionMode = executionModeArg; //can be forEach or atEnd
 | 
			
		||||
    let functionsToExecute = functionsToExecuteArg;
 | 
			
		||||
    let promiseArray = [];
 | 
			
		||||
    let runFunction = function(functionArg){
 | 
			
		||||
        let returnValue = functionArg();
 | 
			
		||||
        if (typeof returnValue !== "undefined" && typeof returnValue.then !== "undefined") {
 | 
			
		||||
            promiseArray.push(returnValue);
 | 
			
		||||
    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 () {
 | 
			
		||||
        if (typeof functionsToExecute === "function" ) {
 | 
			
		||||
            runFunction(functionsToExecute);
 | 
			
		||||
        } else if (Array.isArray(functionsToExecute)) {
 | 
			
		||||
            for (let anyFunction in functionsToExecute) {
 | 
			
		||||
                runFunction(functionsToExecute[anyFunction]);
 | 
			
		||||
    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");
 | 
			
		||||
            throw new Error('gulp-callfunction: something is strange with the given arguments')
 | 
			
		||||
        }
 | 
			
		||||
        return plugins.Q.all(promiseArray);
 | 
			
		||||
    };
 | 
			
		||||
        return q.all(promiseArray)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let hasExecutedOnce = false;
 | 
			
		||||
    let forEach = function (file, enc, cb) { //the forEach function is called for every chunk
 | 
			
		||||
        switch (executionMode){
 | 
			
		||||
            case "forEach":
 | 
			
		||||
                checkAndRunFunction().then(function(){
 | 
			
		||||
                    cb(null, file);
 | 
			
		||||
                });
 | 
			
		||||
                break;
 | 
			
		||||
            case "forFirst":
 | 
			
		||||
                !hasExecutedOnce ? checkAndRunFunction().then(function(){
 | 
			
		||||
                    cb(null, file);
 | 
			
		||||
                }) : cb(null, file);
 | 
			
		||||
                hasExecutedOnce = true;
 | 
			
		||||
                break;
 | 
			
		||||
            case "atEnd":
 | 
			
		||||
                cb(null, file);
 | 
			
		||||
                break;
 | 
			
		||||
    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
 | 
			
		||||
            default:
 | 
			
		||||
                break;
 | 
			
		||||
                break
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let atEnd = function(cb) {
 | 
			
		||||
        if (executionMode === "atEnd") {
 | 
			
		||||
            checkAndRunFunction().then(function(){
 | 
			
		||||
                cb();
 | 
			
		||||
            });
 | 
			
		||||
    let atEnd = function (cb) {
 | 
			
		||||
        if (executionModeArg === 'atEnd') {
 | 
			
		||||
            checkAndRunFunction(null, null).then(function () {
 | 
			
		||||
                cb()
 | 
			
		||||
            })
 | 
			
		||||
        } else {
 | 
			
		||||
            cb();
 | 
			
		||||
            cb()
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    return plugins.through2.obj(forEach,atEnd);
 | 
			
		||||
};
 | 
			
		||||
    }
 | 
			
		||||
    return through2.obj(forEach, atEnd)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								ts/tsd.json
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								ts/tsd.json
									
									
									
									
									
								
							@@ -1,15 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
  "version": "v4",
 | 
			
		||||
  "repo": "borisyankov/DefinitelyTyped",
 | 
			
		||||
  "ref": "master",
 | 
			
		||||
  "path": "typings",
 | 
			
		||||
  "bundle": "typings/tsd.d.ts",
 | 
			
		||||
  "installed": {
 | 
			
		||||
    "node/node.d.ts": {
 | 
			
		||||
      "commit": "efa0c1196d7280640e624ac1e7fa604502e7bd63"
 | 
			
		||||
    },
 | 
			
		||||
    "colors/colors.d.ts": {
 | 
			
		||||
      "commit": "3191f6e0088eee07c4d8fd24e4d27a40a60d9eb9"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user