added forFirst

This commit is contained in:
Philipp Kunz 2016-03-26 17:22:46 +01:00
parent 5a9cd96bed
commit 71bdcce41d
12 changed files with 279 additions and 147 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@ node_modules/
.settings/
.idea/
coverage/
docs/
ts/*.js
ts/*.js.map

View File

@ -40,4 +40,4 @@ gulp.task('gulpTest',function() {
> Note: The first argument of gulpFunction can also be an array of multiple functionnames.
Each function can return a promise. the pipe stop will finish when every promise is fullfilled.
> Note: the second argument can be empty (defaults to 'forEach') or 'atEnd'
> Note: the second argument can be empty, "forEach" (default), "forFirst" or "atEnd";

67
dist/index.js vendored Normal file

File diff suppressed because one or more lines are too long

6
index.d.ts vendored
View File

@ -1,6 +0,0 @@
/// <reference path="ts/typings/main.d.ts" />
declare var plugins: {
beautylog: any;
Q: any;
through: any;
};

View File

@ -1,57 +0,0 @@
#!/usr/bin/env node
/// <reference path="typings/main.d.ts" />
var plugins = {
beautylog: require("beautylog"),
Q: require("q"),
through: require("through2")
};
module.exports = function (functionsToExecuteArg, executionModeArg) {
if (executionModeArg === void 0) { executionModeArg = 'forEach'; }
//important vars
var executionMode = executionModeArg; //can be forEach or atEnd
var functionsToExecute = functionsToExecuteArg;
var promiseArray = [];
var runFunction = function (functionArg) {
var returnValue = functionArg();
if (typeof returnValue !== "undefined" && typeof returnValue.then !== "undefined") {
promiseArray.push(returnValue);
}
};
var checkAndRunFunction = function () {
if (typeof functionsToExecute === "function") {
runFunction(functionsToExecute);
}
else if (Array.isArray(functionsToExecute)) {
for (var anyFunction in functionsToExecute) {
runFunction(functionsToExecute[anyFunction]);
}
}
else {
plugins.beautylog.error('gulp-callfunction: something is strange with the given arguments');
}
return plugins.Q.all(promiseArray);
};
var forEach = function (file, enc, cb) {
if (executionMode === 'forEach') {
checkAndRunFunction().then(function () {
cb(null, file);
});
}
else {
cb(null, file);
}
//tell gulp that we are complete
};
var atEnd = function (cb) {
if (executionMode === "atEnd") {
checkAndRunFunction().then(function () {
cb();
});
}
else {
cb();
}
};
return plugins.through.obj(forEach, atEnd);
};

View File

@ -2,7 +2,7 @@
"name": "gulp-function",
"version": "1.1.1",
"description": "accepts a function call as parameter to execute in gulp pipeline",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "(npmts)",
"reinstall": "(rm -r node_modules && npm install)",
@ -18,19 +18,19 @@
"gulp",
"function"
],
"author": "Smart Coordination GmbH <office@push.rocks> (https://push.rocks)",
"author": "Lossless GmbH <office@lossless.com> (https://lossless.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/pushrocks/gulp-function/issues"
},
"homepage": "https://github.com/pushrocks/gulp-function",
"dependencies": {
"beautylog": "^2.1.1",
"beautylog": "^3.1.2",
"q": "^1.4.1",
"through2": "^2.0.1"
},
"devDependencies": {
"gulp": "^3.9.1",
"npmts": "^2.2.3"
"npmts": "^3.6.4"
}
}

8
test/test.d.ts vendored
View File

@ -1,8 +0,0 @@
/// <reference path="ts/typings/main.d.ts" />
declare var gulp: any;
declare var gulpFunction: any;
declare var beautylog: any;
declare var Q: any;
declare var myFunction: () => any;
declare var myFunction2: () => any;
declare var myFunction3: () => any;

File diff suppressed because one or more lines are too long

1
test/test.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"test.js","sourceRoot":"","sources":["test.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,IAAI,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC3B,IAAI,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAC/C,IAAI,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AACrC,IAAI,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;AAErB,IAAI,UAAU,GAAG;IACb,IAAI,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,CAAA;IACpB,SAAS,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACnC,IAAI,CAAC,OAAO,EAAE,CAAC;IACf,MAAM,CAAC,IA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,IAAI,CAAC,YAAY,CAAC,eAAe,EAAC,OAAO,CAAC,CAAC,CAAC;QAEjD,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAC;YACf,SAAS,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;YAClC,IAAI,EAAE,CAAC;QACX,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}

118
test/test.ts Normal file
View File

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

View File

@ -11,21 +11,21 @@ var plugins = {
module.exports = function (functionsToExecuteArg:any|any[],executionModeArg:string = 'forEach') {
//important vars
var executionMode = executionModeArg; //can be forEach or atEnd
var functionsToExecute = functionsToExecuteArg;
var promiseArray = [];
var runFunction = function(functionArg){
var returnValue = functionArg();
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);
}
};
var checkAndRunFunction = function () {
let checkAndRunFunction = function () {
if (typeof functionsToExecute === "function" ) {
runFunction(functionsToExecute);
} else if (Array.isArray(functionsToExecute)) {
for (var anyFunction in functionsToExecute) {
for (let anyFunction in functionsToExecute) {
runFunction(functionsToExecute[anyFunction]);
}
} else {
@ -34,20 +34,29 @@ module.exports = function (functionsToExecuteArg:any|any[],executionModeArg:stri
return plugins.Q.all(promiseArray);
};
var forEach = function (file, enc, cb) {
if (executionMode === 'forEach') {
checkAndRunFunction().then(function(){
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);
});
} else {
cb(null, file);
break;
default:
break;
}
//tell gulp that we are complete
};
var atEnd = function(cb) {
let atEnd = function(cb) {
if (executionMode === "atEnd") {
checkAndRunFunction().then(function(){
cb();

View File

@ -1,50 +0,0 @@
/// <reference path="typings/main.d.ts" />
var gulp = require("gulp");
var gulpFunction = require("../index.js");
var beautylog = require("beautylog");
var Q = require("q");
var myFunction = function () {
var done = Q.defer()
beautylog.log("Function executed");
done.resolve();
return done.promise;
};
var myFunction2 = function () {
var done = Q.defer();
beautylog.ok("Function2 executed");
done.resolve();
return done.promise;
};
var myFunction3 = function () {
var done = Q.defer();
beautylog.success("Function3 executed");
done.resolve();
return done.promise;
};
describe("gulpFunction",function(){
it("should run through smoothly with " + "'forEach'".blue,function(done){
gulp.src('./test/*.md')
.pipe(gulpFunction(myFunction,'forEach'))
.pipe(gulp.dest("./test/result/"));
gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'forEach'))
.pipe(gulp.dest("./test/result/"))
.pipe(gulpFunction(done,"atEnd"));
});
it("should run through smoothly with " + "'atEnd'".blue,function(done){
gulp.src('./test/*.md')
.pipe(gulpFunction(myFunction,'atEnd'))
.pipe(gulp.dest("./test/result/"));
gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'atEnd'))
.pipe(gulp.dest("./test/result/"))
.pipe(gulpFunction(done,"atEnd"));
});
});