now accepts promises as return of supplied functions
This commit is contained in:
parent
ac9305c480
commit
6e55ed3162
8
index.d.ts
vendored
8
index.d.ts
vendored
@ -1,4 +1,6 @@
|
|||||||
/// <reference path="ts/typings/main.d.ts" />
|
/// <reference path="ts/typings/main.d.ts" />
|
||||||
declare var through: any;
|
declare var plugins: {
|
||||||
declare var path: any;
|
beautylog: any;
|
||||||
declare var beautylog: any;
|
Q: any;
|
||||||
|
through: any;
|
||||||
|
};
|
||||||
|
58
index.js
58
index.js
@ -1,41 +1,57 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
/// <reference path="typings/main.d.ts" />
|
/// <reference path="typings/main.d.ts" />
|
||||||
var through = require("through2");
|
var plugins = {
|
||||||
var path = require("path");
|
beautylog: require("beautylog"),
|
||||||
var beautylog = require("beautylog");
|
Q: require("q"),
|
||||||
|
through: require("through2")
|
||||||
|
};
|
||||||
module.exports = function (functionsToExecuteArg, executionModeArg) {
|
module.exports = function (functionsToExecuteArg, executionModeArg) {
|
||||||
if (executionModeArg === void 0) { executionModeArg = 'forEach'; }
|
if (executionModeArg === void 0) { executionModeArg = 'forEach'; }
|
||||||
//important vars
|
//important vars
|
||||||
var gulpFunction = {
|
var executionMode = executionModeArg; //can be forEach or atEnd
|
||||||
executionMode: executionModeArg,
|
var functionsToExecute = functionsToExecuteArg;
|
||||||
functionsToExecute: functionsToExecuteArg
|
var promiseArray = [];
|
||||||
};
|
var runFunction = function (functionArg) {
|
||||||
var runFunctionNames = function () {
|
var returnValue = functionArg();
|
||||||
if (typeof gulpFunction.functionsToExecute === "function") {
|
if (typeof returnValue !== "undefined" && typeof returnValue.then !== "undefined") {
|
||||||
gulpFunction.functionsToExecute();
|
promiseArray.push(returnValue);
|
||||||
}
|
}
|
||||||
else if (Array.isArray(gulpFunction.functionsToExecute)) {
|
};
|
||||||
for (var anyFunction in gulpFunction.functionsToExecute) {
|
var checkAndRunFunction = function () {
|
||||||
gulpFunction.functionsToExecute[anyFunction]();
|
if (typeof functionsToExecute === "function") {
|
||||||
|
runFunction(functionsToExecute);
|
||||||
|
}
|
||||||
|
else if (Array.isArray(functionsToExecute)) {
|
||||||
|
for (var anyFunction in functionsToExecute) {
|
||||||
|
runFunction(functionsToExecute[anyFunction]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
beautylog.error('gulp-callfunction: something is strange with the given arguments');
|
plugins.beautylog.error('gulp-callfunction: something is strange with the given arguments');
|
||||||
}
|
}
|
||||||
|
return plugins.Q.all(promiseArray);
|
||||||
};
|
};
|
||||||
var forEach = function (file, enc, cb) {
|
var forEach = function (file, enc, cb) {
|
||||||
if (gulpFunction.executionMode === 'forEach') {
|
if (executionMode === 'forEach') {
|
||||||
runFunctionNames();
|
checkAndRunFunction().then(function () {
|
||||||
|
cb(null, file);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cb(null, file);
|
||||||
}
|
}
|
||||||
//tell gulp that we are complete
|
//tell gulp that we are complete
|
||||||
return cb(null, file);
|
|
||||||
};
|
};
|
||||||
var atEnd = function (cb) {
|
var atEnd = function (cb) {
|
||||||
if (gulpFunction.executionMode === "atEnd") {
|
if (executionMode === "atEnd") {
|
||||||
runFunctionNames();
|
checkAndRunFunction().then(function () {
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
cb();
|
|
||||||
};
|
};
|
||||||
return through.obj(forEach, atEnd);
|
return plugins.through.obj(forEach, atEnd);
|
||||||
};
|
};
|
||||||
|
@ -25,8 +25,9 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/pushrocks/gulp-function",
|
"homepage": "https://github.com/pushrocks/gulp-function",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"beautylog": "2.1.1",
|
"beautylog": "^2.1.1",
|
||||||
"through2": "2.0.1"
|
"q": "^1.4.1",
|
||||||
|
"through2": "^2.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"gulp": "^3.9.1",
|
"gulp": "^3.9.1",
|
||||||
|
6
test/test.d.ts
vendored
6
test/test.d.ts
vendored
@ -2,5 +2,7 @@
|
|||||||
declare var gulp: any;
|
declare var gulp: any;
|
||||||
declare var gulpFunction: any;
|
declare var gulpFunction: any;
|
||||||
declare var beautylog: any;
|
declare var beautylog: any;
|
||||||
declare var myFunction: () => void;
|
declare var Q: any;
|
||||||
declare var myFunction2: () => void;
|
declare var myFunction: () => any;
|
||||||
|
declare var myFunction2: () => any;
|
||||||
|
declare var myFunction3: () => any;
|
||||||
|
29
test/test.js
29
test/test.js
@ -4,27 +4,42 @@
|
|||||||
var gulp = require("gulp");
|
var gulp = require("gulp");
|
||||||
var gulpFunction = require("../index.js");
|
var gulpFunction = require("../index.js");
|
||||||
var beautylog = require("beautylog");
|
var beautylog = require("beautylog");
|
||||||
|
var Q = require("q");
|
||||||
var myFunction = function () {
|
var myFunction = function () {
|
||||||
|
var done = Q.defer();
|
||||||
beautylog.log("Function executed");
|
beautylog.log("Function executed");
|
||||||
|
done.resolve();
|
||||||
|
return done.promise;
|
||||||
};
|
};
|
||||||
var myFunction2 = function () {
|
var myFunction2 = function () {
|
||||||
beautylog.log("Function2 executed");
|
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 () {
|
describe("gulpFunction", function () {
|
||||||
it("should run through smoothly with " + "'forEach'".blue, function () {
|
it("should run through smoothly with " + "'forEach'".blue, 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([myFunction, myFunction2], 'forEach'))
|
.pipe(gulpFunction([myFunction2, myFunction3], 'forEach'))
|
||||||
.pipe(gulp.dest("./test/result/"));
|
.pipe(gulp.dest("./test/result/"))
|
||||||
|
.pipe(gulpFunction(done, "atEnd"));
|
||||||
});
|
});
|
||||||
it("should run through smoothly with " + "'atEnd'".blue, function () {
|
it("should run through smoothly with " + "'atEnd'".blue, 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([myFunction, myFunction2], 'atEnd'))
|
.pipe(gulpFunction([myFunction2, myFunction3], 'atEnd'))
|
||||||
.pipe(gulp.dest("./test/result/"));
|
.pipe(gulp.dest("./test/result/"))
|
||||||
|
.pipe(gulpFunction(done, "atEnd"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
58
ts/index.ts
58
ts/index.ts
@ -1,42 +1,60 @@
|
|||||||
/// <reference path="typings/main.d.ts" />
|
/// <reference path="typings/main.d.ts" />
|
||||||
var through = require("through2");
|
|
||||||
var path = require("path");
|
var plugins = {
|
||||||
var beautylog = require("beautylog");
|
beautylog: require("beautylog"),
|
||||||
|
Q: require("q"),
|
||||||
|
through: require("through2")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = function (functionsToExecuteArg:any|any[],executionModeArg:string = 'forEach') {
|
module.exports = function (functionsToExecuteArg:any|any[],executionModeArg:string = 'forEach') {
|
||||||
//important vars
|
//important vars
|
||||||
var gulpFunction = {
|
var executionMode = executionModeArg; //can be forEach or atEnd
|
||||||
executionMode: executionModeArg, //can be forEach or atEnd
|
var functionsToExecute = functionsToExecuteArg;
|
||||||
functionsToExecute: functionsToExecuteArg
|
var promiseArray = [];
|
||||||
|
var runFunction = function(functionArg){
|
||||||
|
var returnValue = functionArg();
|
||||||
|
if (typeof returnValue !== "undefined" && typeof returnValue.then !== "undefined") {
|
||||||
|
promiseArray.push(returnValue);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var runFunctionNames = function () {
|
var checkAndRunFunction = function () {
|
||||||
if (typeof gulpFunction.functionsToExecute === "function" ) {
|
if (typeof functionsToExecute === "function" ) {
|
||||||
gulpFunction.functionsToExecute();
|
runFunction(functionsToExecute);
|
||||||
} else if (Array.isArray(gulpFunction.functionsToExecute)) {
|
} else if (Array.isArray(functionsToExecute)) {
|
||||||
for (var anyFunction in gulpFunction.functionsToExecute) {
|
for (var anyFunction in functionsToExecute) {
|
||||||
gulpFunction.functionsToExecute[anyFunction]();
|
runFunction(functionsToExecute[anyFunction]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
beautylog.error('gulp-callfunction: something is strange with the given arguments');
|
plugins.beautylog.error('gulp-callfunction: something is strange with the given arguments');
|
||||||
}
|
}
|
||||||
|
return plugins.Q.all(promiseArray);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
var forEach = function (file, enc, cb) {
|
var forEach = function (file, enc, cb) {
|
||||||
if (gulpFunction.executionMode === 'forEach') {
|
if (executionMode === 'forEach') {
|
||||||
runFunctionNames();
|
checkAndRunFunction().then(function(){
|
||||||
|
cb(null, file);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
cb(null, file);
|
||||||
}
|
}
|
||||||
//tell gulp that we are complete
|
//tell gulp that we are complete
|
||||||
return cb(null, file);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var atEnd = function(cb) {
|
var atEnd = function(cb) {
|
||||||
if (gulpFunction.executionMode === "atEnd") {
|
if (executionMode === "atEnd") {
|
||||||
runFunctionNames();
|
checkAndRunFunction().then(function(){
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
cb();
|
|
||||||
};
|
};
|
||||||
return through.obj(forEach,atEnd);
|
return plugins.through.obj(forEach,atEnd);
|
||||||
};
|
};
|
||||||
|
31
ts/test.ts
31
ts/test.ts
@ -2,32 +2,49 @@
|
|||||||
var gulp = require("gulp");
|
var gulp = require("gulp");
|
||||||
var gulpFunction = require("../index.js");
|
var gulpFunction = require("../index.js");
|
||||||
var beautylog = require("beautylog");
|
var beautylog = require("beautylog");
|
||||||
|
var Q = require("q");
|
||||||
|
|
||||||
var myFunction = function () {
|
var myFunction = function () {
|
||||||
|
var done = Q.defer()
|
||||||
beautylog.log("Function executed");
|
beautylog.log("Function executed");
|
||||||
|
done.resolve();
|
||||||
|
return done.promise;
|
||||||
};
|
};
|
||||||
var myFunction2 = function () {
|
var myFunction2 = function () {
|
||||||
beautylog.log("Function2 executed");
|
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(){
|
describe("gulpFunction",function(){
|
||||||
it("should run through smoothly with " + "'forEach'".blue,function(){
|
it("should run through smoothly with " + "'forEach'".blue,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([myFunction,myFunction2],'forEach'))
|
.pipe(gulpFunction([myFunction2,myFunction3],'forEach'))
|
||||||
.pipe(gulp.dest("./test/result/"));
|
.pipe(gulp.dest("./test/result/"))
|
||||||
|
.pipe(gulpFunction(done,"atEnd"));
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should run through smoothly with " + "'atEnd'".blue,function(){
|
it("should run through smoothly with " + "'atEnd'".blue,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([myFunction,myFunction2],'atEnd'))
|
.pipe(gulpFunction([myFunction2,myFunction3],'atEnd'))
|
||||||
.pipe(gulp.dest("./test/result/"));
|
.pipe(gulp.dest("./test/result/"))
|
||||||
|
.pipe(gulpFunction(done,"atEnd"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user