update to latest standards

This commit is contained in:
Philipp Kunz 2018-01-29 23:28:03 +01:00
parent e7ccd9aec4
commit 848d9ac1e0
7 changed files with 716 additions and 438 deletions

View File

@ -1,13 +1,16 @@
# gulp-function # gulp-function
accepts a function call as parameter to execute in gulp pipeline accepts a function call as parameter to execute in gulp pipeline
## Availabililty ## Availabililty
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/gulp-function) [![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/gulp-function)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/gulp-function) [![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/gulp-function)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/gulp-function) [![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/gulp-function)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/gulp-function/) [![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/gulp-function/)
## Status for master ## 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) [![coverage report](https://GitLab.com/pushrocks/gulp-function/badges/master/coverage.svg)](https://GitLab.com/pushrocks/gulp-function/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/gulp-function.svg)](https://www.npmjs.com/package/gulp-function) [![npm downloads per month](https://img.shields.io/npm/dm/gulp-function.svg)](https://www.npmjs.com/package/gulp-function)
@ -18,12 +21,43 @@ accepts a function call as parameter to execute in gulp pipeline
[![node](https://img.shields.io/badge/node->=%206.x.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/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage ### Usage
Use TypeScript for best in class instellisense.
```typescript
import * as gulp from "gulp";
import gulpFunction from "gulp-function"; // default ES6 export
// import {forFirst, forEach, atEnd} from 'gulp-function'
let myAsyncFunction = async (file, enc) => {
// await some async stuff
};
gulp.task("gulpTest", function() {
let stream = gulp
.src("./mydir/*.something")
.pipe(gulpFunction(myFunction, "forEach")) //read the notes below
// .pipe(forEach(myFunction)) // if imported as >> import { forEach } from 'gulp-function' <<
.pipe(gulp.dest("./build/"));
return stream; // by returning the stream gulp knows when our task has finished.
});
```
### Notes
* 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.
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 following options are available:
* "forFirst" - executes when first chunk/vinylfile of the stream reaches the pipestop.
file is pushed further down the line when function's returned promise is fullfilled.
* "forEach" - executes like "forFirst" but with every chunk/vinylfile in the stream;
* "atEnd" - executes after all chunks have passed and are processed in full.
That means the stream's "finish" event fires **before "atLast" is executed**!!!
For further information read the linked docs at the top of this README. For further information read the linked docs at the top of this README.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh) > MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html) > | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks) [![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)

5
dist/index.d.ts vendored
View File

@ -1,7 +1,6 @@
/// <reference types="node" /> /// <reference types="node" />
import 'typings-global'; import { Transform } from "stream";
import { Transform } from 'stream'; export declare type TExecutionMode = "forEach" | "forFirst" | "atEnd";
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>;
} }

30
dist/index.js vendored
View File

@ -1,17 +1,17 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
require("typings-global");
const through2 = require("through2"); const through2 = require("through2");
let defaultExport = (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);
if (typeof returnValue !== 'undefined' && typeof returnValue.then !== 'undefined') { if (typeof returnValue !== "undefined" &&
typeof returnValue.then !== "undefined") {
promiseArray.push(returnValue); promiseArray.push(returnValue);
} }
}; };
let checkAndRunFunction = function (file, enc) { let checkAndRunFunction = function (file, enc) {
if (typeof functionsToExecuteArg === 'function') { if (typeof functionsToExecuteArg === "function") {
runFunction(functionsToExecuteArg, file, enc); runFunction(functionsToExecuteArg, file, enc);
} }
else if (Array.isArray(functionsToExecuteArg)) { else if (Array.isArray(functionsToExecuteArg)) {
@ -20,22 +20,22 @@ let defaultExport = (functionsToExecuteArg, executionModeArg = 'forEach') => {
} }
} }
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 Promise.all(promiseArray); return Promise.all(promiseArray);
}; };
let hasExecutedOnce = false; let hasExecutedOnce = false;
let forEach = function (file, enc, cb) { let forEach = function (file, enc, cb) {
// the forEach function is called for every chunk
switch (executionModeArg) { switch (executionModeArg) {
case 'forEach': case "forEach":
checkAndRunFunction(file, enc).then(function () { checkAndRunFunction(file, enc).then(function () {
cb(null, file); cb(null, file);
}); });
break; break;
case 'forFirst': case "forFirst":
if (hasExecutedOnce) { if (hasExecutedOnce) {
checkAndRunFunction(file, enc) checkAndRunFunction(file, enc).then(function () {
.then(function () {
cb(null, file); cb(null, file);
}); });
} }
@ -44,7 +44,7 @@ let defaultExport = (functionsToExecuteArg, executionModeArg = 'forEach') => {
} }
hasExecutedOnce = true; hasExecutedOnce = true;
break; break;
case 'atEnd': case "atEnd":
cb(); cb();
break; break;
default: default:
@ -52,7 +52,7 @@ let defaultExport = (functionsToExecuteArg, executionModeArg = 'forEach') => {
} }
}; };
let atEnd = function (cb) { let atEnd = function (cb) {
if (executionModeArg === 'atEnd') { if (executionModeArg === "atEnd") {
checkAndRunFunction(null, null).then(function () { checkAndRunFunction(null, null).then(function () {
cb(); cb();
}); });
@ -64,13 +64,13 @@ let defaultExport = (functionsToExecuteArg, executionModeArg = 'forEach') => {
return through2.obj(forEach, atEnd); return through2.obj(forEach, atEnd);
}; };
exports.forEach = (funcArg) => { exports.forEach = (funcArg) => {
return defaultExport(funcArg, 'forEach'); return defaultExport(funcArg, "forEach");
}; };
exports.forFirst = (funcArg) => { exports.forFirst = (funcArg) => {
return defaultExport(funcArg, 'forFirst'); return defaultExport(funcArg, "forFirst");
}; };
exports.atEnd = (funcArg) => { exports.atEnd = (funcArg) => {
return defaultExport(funcArg, 'atEnd'); return defaultExport(funcArg, "atEnd");
}; };
exports.default = defaultExport; exports.default = defaultExport;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBCQUF1QjtBQUV2QixxQ0FBb0M7QUFTcEMs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tBQUssQ0FBQztRQUNWLENBQUM7SUFDSCxDQUFDLENBQUM7SUFFRixJQUFJLEtBQUssR0FBRyxVQUFTLEVBQUU7UUFDckIsRUFBRSxDQUFDLENBQUMsZ0JBQWdCLEtBQUssT0FBTyxDQUFDLENBQUMsQ0FBQztZQUNqQyxtQkFBbUIsQ0FBQyxJQUFJLEVBQUUsSUFBSSxDQUFDLENBQUMsSUFBSSxDQUFDO2dCQUNuQyxFQUFFLEVBQUUsQ0FBQztZQUNQLENBQUMsQ0FBQyxDQUFDO1FBQ0wsQ0FBQztRQUFDLElBQUksQ0FBQyxDQUFDO1lBQ04sRUFBRSxFQUFFLENBQUM7UUFDUCxDQUFDO0lBQ0gsQ0FBQyxDQUFDO0lBQ0YsTUFBTSxDQUFDLFFBQVEsQ0FBQyxHQUFHLENBQUMsT0FBTyxFQUFFLEtBQUssQ0FBQyxDQUFDO0FBQ3RDLENBQUMsQ0FBQztBQUVTLFFBQUEsT0FBTyxHQUFHLENBQUMsT0FBeUIsRUFBRSxFQUFFO0lBQ2pELE1BQU0sQ0FBQyxhQUFhLENBQUMsT0FBTyxFQUFFLFNBQVMsQ0FBQyxDQUFDO0FBQzNDLENBQUMsQ0FBQztBQUVTLFFBQUEsUUFBUSxHQUFHLENBQUMsT0FBeUIsRUFBRSxFQUFFO0lBQ2xELE1BQU0sQ0FBQyxhQUFhLENBQUMsT0FBTyxFQUFFLFVBQVUsQ0FBQyxDQUFDO0FBQzVDLENBQUMsQ0FBQztBQUVTLFFBQUEsS0FBSyxHQUFHLENBQUMsT0FBeUIsRUFBRSxFQUFFO0lBQy9DLE1BQU0sQ0FBQyxhQUFhLENBQUMsT0FBTyxFQUFFLE9BQU8sQ0FBQyxDQUFDO0FBQ3pDLENBQUMsQ0FBQztBQUVGLGtCQUFlLGFBQWEsQ0FBQyJ9

View File

@ -1,40 +0,0 @@
### Usage
```javascript
import * as gulp from 'gulp';
import gulpFunction from 'gulp-function' // default ES6 export
// import {forFirst, forEach, atEnd} from 'gulp-function'
let Q = require("q");
let myFunction = function (file, enc) { // file and enc are optional in case you want to modify the file object
let done = Q.defer();
console.log("Hello World!")
// NOTE:
// you can use done.resolve as callback function
// of any async tasks within this function
done.resolve();
return done.promise;
}
gulp.task('gulpTest',function() {
let stream = gulp.src('./mydir/*.something')
.pipe(gulpFunction(myFunction,'forEach')) //read the notes below
// .pipe(forEach(myFunction)) // if imported as >> import { forEach } from 'gulp-function' <<
.pipe(gulp.dest("./build/"));
return stream; // by returning the stream gulp knows when our task has finished.
});
```
### Notes:
* 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.
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 following options are available:
* "forFirst" - executes when first chunk/vinylfile of the stream reaches the pipestop.
file is pushed further down the line when function's returned promise is fullfilled.
* "forEach" - executes like "forFirst" but with every chunk/vinylfile in the stream;
* "atEnd" - executes after all chunks have passed and are processed in full.
That means the stream's "finish" event fires **before "atLast" is executed**!!!

View File

@ -14,7 +14,8 @@
"keywords": [ "keywords": [
"gulpplugin", "gulpplugin",
"gulp", "gulp",
"function" "function",
"pushrocks"
], ],
"author": "Lossless GmbH <office@lossless.com> (https://lossless.com)", "author": "Lossless GmbH <office@lossless.com> (https://lossless.com)",
"license": "MIT", "license": "MIT",
@ -25,8 +26,7 @@
"dependencies": { "dependencies": {
"@types/through2": "^2.0.32", "@types/through2": "^2.0.32",
"smartq": "^1.1.6", "smartq": "^1.1.6",
"through2": "^2.0.3", "through2": "^2.0.3"
"typings-global": "^1.0.16"
}, },
"devDependencies": { "devDependencies": {
"gulp": "^3.9.1", "gulp": "^3.9.1",

View File

@ -1,89 +1,92 @@
import 'typings-global' import * as q from "smartq";
import * as q from 'smartq' import * as through2 from "through2";
import * as through2 from 'through2' import { Transform } from "stream";
import { Transform } from 'stream'
export type TExecutionMode = 'forEach' | 'forFirst' | 'atEnd' export type TExecutionMode = "forEach" | "forFirst" | "atEnd";
export interface IPromiseFunction { export interface IPromiseFunction {
(file?, enc?): PromiseLike<any> (file?, enc?): PromiseLike<any>;
} }
let defaultExport = ( let defaultExport = (
functionsToExecuteArg: IPromiseFunction | IPromiseFunction[], functionsToExecuteArg: IPromiseFunction | IPromiseFunction[],
executionModeArg: TExecutionMode = 'forEach' executionModeArg: TExecutionMode = "forEach"
): Transform => { ): Transform => {
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);
if (typeof returnValue !== 'undefined' && typeof returnValue.then !== 'undefined') { if (
promiseArray.push(returnValue) typeof returnValue !== "undefined" &&
} typeof returnValue.then !== "undefined"
) {
promiseArray.push(returnValue);
} }
};
let checkAndRunFunction = function(file, enc) { let checkAndRunFunction = function(file, enc) {
if (typeof functionsToExecuteArg === 'function') { if (typeof functionsToExecuteArg === "function") {
runFunction(functionsToExecuteArg, file, enc) runFunction(functionsToExecuteArg, file, enc);
} else if (Array.isArray(functionsToExecuteArg)) { } else if (Array.isArray(functionsToExecuteArg)) {
for (let anyFunction in functionsToExecuteArg) { for (let anyFunction in functionsToExecuteArg) {
runFunction(functionsToExecuteArg[ anyFunction ], file, enc) 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 Promise.all(promiseArray) );
} }
return Promise.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 (executionModeArg) { switch (executionModeArg) {
case 'forEach': case "forEach":
checkAndRunFunction(file, enc).then(function() { checkAndRunFunction(file, enc).then(function() {
cb(null, file) cb(null, file);
}) });
break break;
case 'forFirst': case "forFirst":
if (hasExecutedOnce) { if (hasExecutedOnce) {
checkAndRunFunction(file, enc) checkAndRunFunction(file, enc).then(function() {
.then(function () { cb(null, file);
cb(null, file) });
})
} else { } else {
cb(null, file) cb(null, file);
} }
hasExecutedOnce = true hasExecutedOnce = true;
break break;
case 'atEnd': case "atEnd":
cb() cb();
break break;
default: default:
break break;
}
} }
};
let atEnd = function(cb) { let atEnd = function(cb) {
if (executionModeArg === 'atEnd') { if (executionModeArg === "atEnd") {
checkAndRunFunction(null, null).then(function() { checkAndRunFunction(null, null).then(function() {
cb() cb();
}) });
} else { } else {
cb() cb();
}
}
return through2.obj(forEach, atEnd)
} }
};
return through2.obj(forEach, atEnd);
};
export let forEach = (funcArg: IPromiseFunction) => { export let forEach = (funcArg: IPromiseFunction) => {
return defaultExport(funcArg, 'forEach') return defaultExport(funcArg, "forEach");
} };
export let forFirst = (funcArg: IPromiseFunction) => { export let forFirst = (funcArg: IPromiseFunction) => {
return defaultExport(funcArg, 'forFirst') return defaultExport(funcArg, "forFirst");
} };
export let atEnd = (funcArg: IPromiseFunction) => { export let atEnd = (funcArg: IPromiseFunction) => {
return defaultExport(funcArg, 'atEnd') return defaultExport(funcArg, "atEnd");
} };
export default defaultExport export default defaultExport;

916
yarn.lock

File diff suppressed because it is too large Load Diff