add .forEach .atEnd .forFirst

This commit is contained in:
Philipp Kunz 2016-10-19 07:36:32 +02:00
parent a2b37a066d
commit 1d8de68a66
6 changed files with 97 additions and 70 deletions

View File

@ -21,6 +21,7 @@ accepts call to execute in gulp pipeline.
```javascript ```javascript
import * as gulp from 'gulp'; import * as gulp from 'gulp';
import gulpFunction from 'gulp-function' // default ES6 export import gulpFunction from 'gulp-function' // default ES6 export
// import {forFirst, forEach, atEnd} from 'gulp-function'
let Q = require("q"); let Q = require("q");
let myFunction = function (file, enc) { // file and enc are optional in case you want to modify the file object let myFunction = function (file, enc) { // file and enc are optional in case you want to modify the file object
@ -38,6 +39,7 @@ let myFunction = function (file, enc) { // file and enc are optional in case you
gulp.task('gulpTest',function() { gulp.task('gulpTest',function() {
let stream = gulp.src('./mydir/*.something') let stream = gulp.src('./mydir/*.something')
.pipe(gulpFunction(myFunction,'forEach')) //read the notes below .pipe(gulpFunction(myFunction,'forEach')) //read the notes below
// .pipe(gulpFunction.forEach(myFunction))
.pipe(gulp.dest("./build/")); .pipe(gulp.dest("./build/"));
return stream; // by returning the stream gulp knows when our task has finished. return stream; // by returning the stream gulp knows when our task has finished.
}); });

7
dist/index.d.ts vendored
View File

@ -5,5 +5,8 @@ export declare type TExecutionMode = 'forEach' | 'forFirst' | 'atEnd';
export interface IPromiseFunction { export interface IPromiseFunction {
(file?: any, enc?: any): PromiseLike<any>; (file?: any, enc?: any): PromiseLike<any>;
} }
declare let mainExportFunction: (functionsToExecuteArg: IPromiseFunction | IPromiseFunction[], executionModeArg?: TExecutionMode) => Transform; declare let defaultExport: (functionsToExecuteArg: IPromiseFunction | IPromiseFunction[], executionModeArg?: TExecutionMode) => Transform;
export default mainExportFunction; export declare let forEach: (funcArg: IPromiseFunction) => void;
export declare let forFirst: (funcArg: IPromiseFunction) => void;
export declare let atEnd: (funcArg: IPromiseFunction) => void;
export default defaultExport;

15
dist/index.js vendored
View File

@ -2,7 +2,7 @@
require("typings-global"); require("typings-global");
const q = require("q"); const q = require("q");
const through2 = require("through2"); const through2 = require("through2");
let mainExportFunction = (functionsToExecuteArg, executionModeArg = 'forEach') => { let defaultExport = (functionsToExecuteArg, executionModeArg = 'forEach') => {
let promiseArray = []; let promiseArray = [];
let runFunction = function (functionArg, file, enc) { let runFunction = function (functionArg, file, enc) {
let returnValue = functionArg(file, enc); let returnValue = functionArg(file, enc);
@ -63,6 +63,15 @@ let mainExportFunction = (functionsToExecuteArg, executionModeArg = 'forEach') =
}; };
return through2.obj(forEach, atEnd); return through2.obj(forEach, atEnd);
}; };
exports.forEach = (funcArg) => {
defaultExport(funcArg, 'forEach');
};
exports.forFirst = (funcArg) => {
defaultExport(funcArg, 'forFirst');
};
exports.atEnd = (funcArg) => {
defaultExport(funcArg, 'atEnd');
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.default = mainExportFunction; exports.default = defaultExport;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLHVCQUFzQjtBQUN0QixxQ0FBb0M7QUFTcEMsSUFBSSxrQkFBa0IsR0FBRyxDQUNyQixxQkFBNEQsRUFDNUQsZ0JBQWdCLEdBQW1CLFNBQVM7SUFHNUMsSUFBSSxZQUFZLEdBQUcsRUFBRSxDQUFBO0lBQ3JCLElBQUksV0FBVyxHQUFHLFVBQVUsV0FBVyxFQUFFLElBQUksRUFBRSxHQUFHO1FBQzlDLElBQUksV0FBVyxHQUFHLFdBQVcsQ0FBQyxJQUFJLEVBQUUsR0FBRyxDQUFDLENBQUE7UUFDeEMsRUFBRSxDQUFDLENBQUMsT0FBTyxXQUFXLEtBQUssV0FBVyxJQUFJLE9BQU8sV0FBVyxDQUFDLElBQUksS0FBSyxXQUFXLENBQU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UMsT0FBeUI7SUFDekMsYUFBYSxDQUFDLE9BQU8sRUFBRSxPQUFPLENBQUMsQ0FBQTtBQUNuQyxDQUFDLENBQUE7O0FBRUQsa0JBQWUsYUFBYSxDQUFBIn0=

File diff suppressed because one or more lines are too long

View File

@ -1,63 +1,64 @@
import 'typings-test' import 'typings-test'
let gulp = require('gulp'); let gulp = require('gulp')
import gulpFunction from '../dist/index' import gulpFunction from '../dist/index'
let beautylog = require('beautylog')
import * as beautylog from 'beautylog'
let Q = require('q') let Q = require('q')
let myFunction = function () { let myFunction = function () {
let done = Q.defer() let done = Q.defer()
beautylog.log('Function executed'); beautylog.log('Function executed')
done.resolve(); done.resolve()
return done.promise; return done.promise
}; }
let myFunction2 = function () { let myFunction2 = function () {
let done = Q.defer(); let done = Q.defer()
beautylog.ok('Function2 executed'); beautylog.ok('Function2 executed')
done.resolve(); done.resolve()
return done.promise; return done.promise
}; }
let myFunction3 = function () { let myFunction3 = function () {
let done = Q.defer(); let done = Q.defer()
beautylog.success('Function3 executed'); beautylog.success('Function3 executed')
done.resolve(); done.resolve()
return done.promise; return done.promise
}; }
let beforeFunction = function () { let beforeFunction = function () {
let done = Q.defer(); let done = Q.defer()
beautylog.success('beforeFunction executed'); beautylog.success('beforeFunction executed')
done.resolve(); done.resolve()
return done.promise; return done.promise
}; }
let middleFunctionRun = false; let middleFunctionRun = false
let middleFunction = function () { let middleFunction = function () {
let done = Q.defer(); let done = Q.defer()
beautylog.success('middleFunction executed'); beautylog.success('middleFunction executed')
setTimeout(function(){ setTimeout(function(){
beautylog.log('timeout fired'); beautylog.log('timeout fired')
middleFunctionRun = true; middleFunctionRun = true
done.resolve(); done.resolve()
}, 500); }, 500)
return done.promise; return done.promise
}; }
let afterFunction = function () { let afterFunction = function () {
let done = Q.defer(); let done = Q.defer()
beautylog.success('afterFunction executed'); beautylog.success('afterFunction executed')
done.resolve(); done.resolve()
return done.promise; return done.promise
}; }
let timeoutFunction = function(){ let timeoutFunction = function(){
let done = Q.defer(); let done = Q.defer()
setTimeout(function(){ setTimeout(function(){
beautylog.log('largeTimeout fired'); beautylog.log('largeTimeout fired')
done.resolve(); done.resolve()
},2000); },2000)
return done.promise; return done.promise
}; }
@ -65,54 +66,54 @@ describe('gulpFunction',function(){
it('should run through smoothly with ' + "'forEach'",function(done){ it('should run through smoothly with ' + "'forEach'",function(done){
gulp.src('./test/*.md') gulp.src('./test/*.md')
.pipe(gulpFunction(myFunction,'forEach')) .pipe(gulpFunction(myFunction,'forEach'))
.pipe(gulp.dest('./test/result/')); .pipe(gulp.dest('./test/result/'))
gulp.src('./test/*.md') gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'forEach')) .pipe(gulpFunction([myFunction2,myFunction3],'forEach'))
.pipe(gulp.dest('./test/result/')) .pipe(gulp.dest('./test/result/'))
.pipe(gulpFunction(done,'atEnd')); .pipe(gulpFunction(done,'atEnd'))
}); })
it('should run through smoothly with ' + "'atEnd'",function(done){ it('should run through smoothly with ' + "'atEnd'",function(done){
gulp.src('./test/*.md') gulp.src('./test/*.md')
.pipe(gulpFunction(myFunction,'atEnd')) .pipe(gulpFunction(myFunction,'atEnd'))
.pipe(gulp.dest('./test/result/')); .pipe(gulp.dest('./test/result/'))
gulp.src('./test/*.md') gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'atEnd')) .pipe(gulpFunction([myFunction2,myFunction3],'atEnd'))
.pipe(gulp.dest('./test/result/')) .pipe(gulp.dest('./test/result/'))
.pipe(gulpFunction(done,'atEnd')); .pipe(gulpFunction(done,'atEnd'))
}); })
it('should run through smoothly once with ' + "'atFirst'",function(done){ it('should run through smoothly once with ' + "'atFirst'",function(done){
gulp.src('./test/*.md') gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'forFirst')) .pipe(gulpFunction([myFunction2,myFunction3],'forFirst'))
.pipe(gulp.dest('./test/result/')) .pipe(gulp.dest('./test/result/'))
.pipe(gulpFunction(done,'atEnd')); .pipe(gulpFunction(done,'atEnd'))
}); })
it('should run in order',function(done){ it('should run in order',function(done){
this.timeout(5000); this.timeout(5000)
let stream = gulp.src('./test/*.md') let stream = gulp.src('./test/*.md')
.pipe(gulpFunction([beforeFunction,middleFunction,middleFunction],'atEnd')) .pipe(gulpFunction([beforeFunction,middleFunction,middleFunction],'atEnd'))
.pipe(gulpFunction(function(){ .pipe(gulpFunction(function(){
beautylog.log('stream progressed'); beautylog.log('stream progressed')
let done2 = Q.defer(); let done2 = Q.defer()
done2.resolve(); done2.resolve()
return done2.promise; return done2.promise
},'forEach')) },'forEach'))
.pipe(gulpFunction(function(){ .pipe(gulpFunction(function(){
beautylog.log('nextStep'); beautylog.log('nextStep')
})) }))
.pipe(gulpFunction(afterFunction,'atEnd')) .pipe(gulpFunction(afterFunction,'atEnd'))
.pipe(gulpFunction(timeoutFunction,'atEnd')); .pipe(gulpFunction(timeoutFunction,'atEnd'))
stream.on('finish',function(){ stream.on('finish',function(){
beautylog.info('stream finished'); beautylog.info('stream finished')
done(); done()
}); })
}); })
}); })

View File

@ -9,7 +9,7 @@ export interface IPromiseFunction {
(file?, enc?): PromiseLike<any> (file?, enc?): PromiseLike<any>
} }
let mainExportFunction = ( let defaultExport = (
functionsToExecuteArg: IPromiseFunction | IPromiseFunction[], functionsToExecuteArg: IPromiseFunction | IPromiseFunction[],
executionModeArg: TExecutionMode = 'forEach' executionModeArg: TExecutionMode = 'forEach'
): Transform => { ): Transform => {
@ -74,4 +74,16 @@ let mainExportFunction = (
return through2.obj(forEach, atEnd) return through2.obj(forEach, atEnd)
} }
export default mainExportFunction export let forEach = (funcArg: IPromiseFunction) => {
defaultExport(funcArg, 'forEach')
}
export let forFirst = (funcArg: IPromiseFunction) => {
defaultExport(funcArg, 'forFirst')
}
export let atEnd = (funcArg: IPromiseFunction) => {
defaultExport(funcArg, 'atEnd')
}
export default defaultExport