add possibility to modify the file object
This commit is contained in:
parent
04d7f9cf7e
commit
63cf7091a1
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,6 +3,8 @@ node_modules/
|
|||||||
.idea/
|
.idea/
|
||||||
coverage/
|
coverage/
|
||||||
docs/
|
docs/
|
||||||
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
ts/*.js
|
ts/*.js
|
||||||
ts/*.js.map
|
ts/*.js.map
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
image: hosttoday/ht-docker-node:npmts
|
image: hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- page
|
- trigger
|
||||||
|
- pages
|
||||||
testLEGACY:
|
testLEGACY:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test legacy
|
- npmci test legacy
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
allow_failure: true
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
@ -25,13 +24,30 @@ testSTABLE:
|
|||||||
- npmci test stable
|
- npmci test stable
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci publish npm
|
- npmci publish
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
trigger:
|
||||||
|
stage: trigger
|
||||||
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
pages:
|
||||||
|
image: hosttoday/ht-docker-node:npmpage
|
||||||
|
stage: pages
|
||||||
|
script:
|
||||||
|
- npmci command npmpage --host gitlab
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
artifacts:
|
||||||
|
expire_in: 1 week
|
||||||
|
paths:
|
||||||
|
- public
|
22
README.md
22
README.md
@ -1,16 +1,21 @@
|
|||||||
# gulp-function
|
# gulp-function
|
||||||
accepts call to execute in gulp pipeline.
|
accepts call to execute in gulp pipeline.
|
||||||
|
|
||||||
### Status
|
## Availabililty
|
||||||
|
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/gulp-function)
|
||||||
|
[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/gulp-function)
|
||||||
|
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/gulp-function)
|
||||||
|
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/gulp-function/)
|
||||||
|
|
||||||
|
## Status for master
|
||||||
[![build status](https://gitlab.com/pushrocks/gulp-function/badges/master/build.svg)](https://gitlab.com/pushrocks/gulp-function/commits/master)
|
[![build status](https://gitlab.com/pushrocks/gulp-function/badges/master/build.svg)](https://gitlab.com/pushrocks/gulp-function/commits/master)
|
||||||
|
[![coverage report](https://gitlab.com/pushrocks/gulp-function/badges/master/coverage.svg)](https://gitlab.com/pushrocks/gulp-function/commits/master)
|
||||||
[![Dependency Status](https://david-dm.org/pushrocks/gulp-function.svg)](https://david-dm.org/pushrocks/gulp-function)
|
[![Dependency Status](https://david-dm.org/pushrocks/gulp-function.svg)](https://david-dm.org/pushrocks/gulp-function)
|
||||||
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/gulp-function/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/gulp-function/master/dependencies/npm)
|
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/gulp-function/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/gulp-function/master/dependencies/npm)
|
||||||
[![bitHound Code](https://www.bithound.io/github/pushrocks/gulp-function/badges/code.svg)](https://www.bithound.io/github/pushrocks/gulp-function)
|
[![bitHound Code](https://www.bithound.io/github/pushrocks/gulp-function/badges/code.svg)](https://www.bithound.io/github/pushrocks/gulp-function)
|
||||||
[![codecov.io](https://codecov.io/github/pushrocks/gulp-function/coverage.svg?branch=master)](https://codecov.io/github/pushrocks/gulp-function?branch=master)
|
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
|
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
### Version
|
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
|
||||||
[![GitHub version](https://badge.fury.io/gh/pushrocks%2Fgulp-function.svg)](https://badge.fury.io/gh/pushrocks%2Fgulp-function)
|
|
||||||
[![npm version](https://badge.fury.io/js/gulp-function.svg)](https://badge.fury.io/js/gulp-function)
|
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
```javascript
|
```javascript
|
||||||
@ -18,7 +23,7 @@ var gulp = require("gulp");
|
|||||||
var gulpFunction = require("gulp-function");
|
var gulpFunction = require("gulp-function");
|
||||||
var Q = require("q");
|
var Q = require("q");
|
||||||
|
|
||||||
var myFunction = function () {
|
var myFunction = function (file, enc) { // file and enc are optional in case you want to modify the file object
|
||||||
var done = Q.defer();
|
var done = Q.defer();
|
||||||
console.log("Hello World!")
|
console.log("Hello World!")
|
||||||
|
|
||||||
@ -42,6 +47,7 @@ gulp.task('gulpTest',function() {
|
|||||||
|
|
||||||
* The first argument of gulpFunction can also be an **array of multiple functionnames**.
|
* 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.
|
Each function can return a promise. The pipe stop will finish when every promise is fullfilled.
|
||||||
|
When providing an array of functions be careful with modifying the file object -> race condition
|
||||||
* The second argument can be empty, it defaults to "forEach"
|
* The second argument can be empty, it defaults to "forEach"
|
||||||
* The following options are available:
|
* The following options are available:
|
||||||
* "forFirst" - executes when first chunk/vinylfile of the stream reaches the pipestop.
|
* "forFirst" - executes when first chunk/vinylfile of the stream reaches the pipestop.
|
||||||
@ -49,3 +55,5 @@ Each function can return a promise. The pipe stop will finish when every promise
|
|||||||
* "forEach" - executes like "forFirst" but with every chunk/vinylfile in the stream;
|
* "forEach" - executes like "forFirst" but with every chunk/vinylfile in the stream;
|
||||||
* "atEnd" - executes after all chunks have passed and are processed in full.
|
* "atEnd" - executes after all chunks have passed and are processed in full.
|
||||||
That means the stream's "finish" event fires **before "atLast" is executed**!!!
|
That means the stream's "finish" event fires **before "atLast" is executed**!!!
|
||||||
|
|
||||||
|
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)
|
||||||
|
3
dist/gulpfunction.plugins.d.ts
vendored
3
dist/gulpfunction.plugins.d.ts
vendored
@ -1,3 +0,0 @@
|
|||||||
import "typings-global";
|
|
||||||
export declare let Q: any;
|
|
||||||
export declare let through2: any;
|
|
6
dist/gulpfunction.plugins.js
vendored
6
dist/gulpfunction.plugins.js
vendored
@ -1,6 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
exports.Q = require("q");
|
|
||||||
exports.through2 = require("through2");
|
|
||||||
|
|
||||||
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImd1bHBmdW5jdGlvbi5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxRQUFPLGdCQUFnQixDQUFDLENBQUE7QUFFYixTQUFDLEdBQUcsT0FBTyxDQUFDLEdBQUcsQ0FBQyxDQUFDO0FBQ2pCLGdCQUFRLEdBQUcsT0FBTyxDQUFDLFVBQVUsQ0FBQyxDQUFDIiwiZmlsZSI6Imd1bHBmdW5jdGlvbi5wbHVnaW5zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IFwidHlwaW5ncy1nbG9iYWxcIjtcclxuXHJcbmV4cG9ydCBsZXQgUSA9IHJlcXVpcmUoXCJxXCIpO1xyXG5leHBvcnQgbGV0IHRocm91Z2gyID0gcmVxdWlyZShcInRocm91Z2gyXCIpO1xyXG4iXX0=
|
|
6
dist/index.d.ts
vendored
6
dist/index.d.ts
vendored
@ -1 +1,5 @@
|
|||||||
import "typings-global";
|
import 'typings-global';
|
||||||
|
export declare type TExecutionMode = 'forEach' | 'forFirst' | 'atEnd';
|
||||||
|
export interface IPromiseFunction {
|
||||||
|
(file?: any, enc?: any): PromiseLike<any>;
|
||||||
|
}
|
||||||
|
68
dist/index.js
vendored
68
dist/index.js
vendored
File diff suppressed because one or more lines are too long
5
npmextra.json
Normal file
5
npmextra.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"npmts": {
|
||||||
|
"mode": "default"
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"mode":"default",
|
|
||||||
"coveralls":true
|
|
||||||
}
|
|
10
package.json
10
package.json
@ -26,14 +26,16 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/gulp-function",
|
"homepage": "https://gitlab.com/pushrocks/gulp-function",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@types/q": "0.0.32",
|
||||||
|
"@types/through2": "^2.0.31",
|
||||||
"q": "^1.4.1",
|
"q": "^1.4.1",
|
||||||
"through2": "^2.0.1",
|
"through2": "^2.0.1",
|
||||||
"typings-global": "^1.0.3"
|
"typings-global": "^1.0.14"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"beautylog": "^5.0.8",
|
"beautylog": "^6.0.0",
|
||||||
"gulp": "^3.9.1",
|
"gulp": "^3.9.1",
|
||||||
"npmts-g": "^5.2.6",
|
"npmts-g": "^5.2.8",
|
||||||
"typings-test": "^1.0.1"
|
"typings-test": "^1.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
@ -1,4 +0,0 @@
|
|||||||
import "typings-global";
|
|
||||||
|
|
||||||
export let Q = require("q");
|
|
||||||
export let through2 = require("through2");
|
|
112
ts/index.ts
112
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 = 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 checkAndRunFunction = function () {
|
module.exports = (
|
||||||
if (typeof functionsToExecute === "function" ) {
|
functionsToExecuteArg: IPromiseFunction | IPromiseFunction[],
|
||||||
runFunction(functionsToExecute);
|
executionModeArg: TExecutionMode = 'forEach'
|
||||||
} else if (Array.isArray(functionsToExecute)) {
|
): Transform => {
|
||||||
for (let anyFunction in functionsToExecute) {
|
|
||||||
runFunction(functionsToExecute[anyFunction]);
|
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 (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 {
|
} 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 q.all(promiseArray)
|
||||||
}
|
}
|
||||||
return plugins.Q.all(promiseArray);
|
|
||||||
};
|
|
||||||
|
|
||||||
let hasExecutedOnce = false;
|
let hasExecutedOnce = false
|
||||||
let forEach = function (file, enc, cb) { // the forEach function is called for every chunk
|
let forEach = function (file, enc, cb) { // the forEach function is called for every chunk
|
||||||
switch (executionMode){
|
switch (executionModeArg) {
|
||||||
case "forEach":
|
case 'forEach':
|
||||||
checkAndRunFunction().then(function(){
|
checkAndRunFunction(file, enc).then(function () {
|
||||||
cb(null, file);
|
cb(null, file)
|
||||||
});
|
})
|
||||||
break;
|
break
|
||||||
case "forFirst":
|
case 'forFirst':
|
||||||
!hasExecutedOnce ? checkAndRunFunction().then(function(){
|
if (hasExecutedOnce) {
|
||||||
cb(null, file);
|
checkAndRunFunction(file, enc)
|
||||||
}) : cb(null, file);
|
.then(function () {
|
||||||
hasExecutedOnce = true;
|
cb(null, file)
|
||||||
break;
|
})
|
||||||
case "atEnd":
|
} else {
|
||||||
cb(null, file);
|
cb(null, file)
|
||||||
break;
|
}
|
||||||
default:
|
hasExecutedOnce = true
|
||||||
break;
|
break
|
||||||
|
case 'atEnd':
|
||||||
|
cb(null, file)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let atEnd = function (cb) {
|
let atEnd = function (cb) {
|
||||||
if (executionMode === "atEnd") {
|
if (executionModeArg === 'atEnd') {
|
||||||
checkAndRunFunction().then(function(){
|
checkAndRunFunction(null, null).then(function () {
|
||||||
cb();
|
cb()
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
cb();
|
cb()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return through2.obj(forEach, atEnd)
|
||||||
}
|
}
|
||||||
};
|
|
||||||
return plugins.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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
3
tslint.json
Normal file
3
tslint.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"extends": "tslint-config-standard"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user