now allows the handling of custom events and run returns promise

This commit is contained in:
Philipp Kunz 2016-09-25 16:10:06 +02:00
parent aaece166c0
commit c28e2cb8e0
10 changed files with 191 additions and 25 deletions

View File

@ -1,9 +1,36 @@
# smartstream # smartstream
simplifies access to node streams, TypeScript ready! simplifies access to node streams, TypeScript ready!
## Status ## Availabililty
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartstream)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartstream)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartstream)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartstream/gitbook)
## Status for master
[![build status](https://gitlab.com/pushrocks/smartstream/badges/master/build.svg)](https://gitlab.com/pushrocks/smartstream/commits/master) [![build status](https://gitlab.com/pushrocks/smartstream/badges/master/build.svg)](https://gitlab.com/pushrocks/smartstream/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartstream/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartstream/commits/master)
[![Dependency Status](https://david-dm.org/pushrocks/smartstream.svg)](https://david-dm.org/pushrocks/smartstream)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartstream/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartstream/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartstream/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartstream)
[![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/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage ## Usage
We recommend the use of TypeScript for best in class intellisense support. We recommend the use of TypeScript for best in class intellisense support.
```typescript
import { Smartstream } from 'smartstream'
import * as gUglify from 'gulp-uglify'
let mySmartstream = new Smartstream([
gulp.src(['./file1.js','./file2.js']),
gUglify(),
gulp.dest('./some/output/path')
])
mySmartstream.onError((err) => {/* handle error */}) // handles all errors in stream
mySmartstream.run().then(() => {/* do something when stream is finished */})
```

32
dist/index.d.ts vendored
View File

@ -1,10 +1,38 @@
/// <reference types="q" />
import * as plugins from './smartstream.plugins';
export interface IErrorFunction { export interface IErrorFunction {
(err: any): number; (err: any): any;
} }
export interface IStreamStartFunction {
(stream: any): any;
}
export interface ICustomEventFunction {
(): any;
}
export interface ICustomEventObject {
eventName: string;
eventFunction: ICustomEventFunction;
}
/**
* class Smartstream handles
*/
export declare class Smartstream { export declare class Smartstream {
streamArray: any[]; streamArray: any[];
errorFunction: IErrorFunction; errorFunction: IErrorFunction;
streamStartFunction: IStreamStartFunction;
customEventObjectArray: ICustomEventObject[];
constructor(streamArrayArg: any[]); constructor(streamArrayArg: any[]);
/**
* attach an error handler to the stream to prevent throwing
*/
onError(errorFunctionArg: IErrorFunction): void; onError(errorFunctionArg: IErrorFunction): void;
run(): any; /**
* make something with the stream itself
*/
onStreamStart(): plugins.q.Promise<any>;
/**
* run the stream
* @returns Promise
*/
run(): plugins.q.Promise<void>;
} }

56
dist/index.js vendored
View File

@ -1,21 +1,67 @@
"use strict"; "use strict";
const plugins = require("./smartstream.plugins"); const plugins = require("./smartstream.plugins");
/**
* class Smartstream handles
*/
class Smartstream { class Smartstream {
constructor(streamArrayArg) { constructor(streamArrayArg) {
this.streamArray = []; this.streamArray = [];
this.errorFunction = null; this.errorFunction = null;
this.streamStartFunction = null;
this.customEventObjectArray = [];
this.streamArray = streamArrayArg; this.streamArray = streamArrayArg;
} }
/**
* attach an error handler to the stream to prevent throwing
*/
onError(errorFunctionArg) { onError(errorFunctionArg) {
this.errorFunction = errorFunctionArg; this.errorFunction = errorFunctionArg;
} }
/**
* make something with the stream itself
*/
onStreamStart() {
}
/**
* run the stream
* @returns Promise
*/
run() { run() {
let combinedStream = plugins.streamCombiner2.obj(this.streamArray); let done = plugins.q.defer();
if (this.errorFunction !== null) { // clone Array
combinedStream.on('error', this.errorFunction); let streamExecutionArray = [];
for (let streamItem of this.streamArray) {
streamExecutionArray.push(streamItem);
} }
return combinedStream; // combine the stream
let finalStream = null;
let firstIteration = true;
for (let stream of streamExecutionArray) {
if (firstIteration === true) {
finalStream = stream;
}
if (this.errorFunction !== null) {
stream.on('error', this.errorFunction);
}
for (let customEventObject of this.customEventObjectArray) {
stream.on(customEventObject.eventName, customEventObject.eventFunction);
}
if (!firstIteration) {
finalStream = finalStream.pipe(stream);
}
firstIteration = false;
}
finalStream.on('end', function () {
done.resolve();
});
finalStream.on('close', function () {
done.resolve();
});
finalStream.on('finish', function () {
done.resolve();
});
return done.promise;
} }
} }
exports.Smartstream = Smartstream; exports.Smartstream = Smartstream;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsaURBQWdEO0FBTWhEO0lBR0ksWUFBWSxjQUFxQjtRQUZqQyxnQkFBVyxHQUFHLEVBQUUsQ0FBQTtRQUNoQixrQkFBYSxHQUFtQixJQUFJLENBQUE7UUFFaEMsSUFBSSxDQUFDLFdBQVcsR0FBRyxjQUFjLENBQUE7SUFDckMsQ0FBQztJQUNELE9BQU8sQ0FBQyxnQkFBZ0M7UUFDcEMsSUFBSSxDQUFDLGFBQWEsR0FBRyxnQkFBZ0IsQ0FBQTtJQUN6QyxDQUFDO0lBQ0QsR0FBRztRQUNDLElBQUksY0FBYyxHQUFHLE9BQU8sQ0FBQyxlQUFlLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQTtRQUNsRSxFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsYUFBYSxLQUFLLElBQUksQ0FBQyxDQUF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FDLEVBQUUsQ0FBQyxPQUFPLEVBQUM7WUFDbkIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2xCLENBQUMsQ0FBQyxDQUFBO1FBQ0YsV0FBVyxDQUFDLEVBQUUsQ0FBQyxRQUFRLEVBQUM7WUFDcEIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2xCLENBQUMsQ0FBQyxDQUFBO1FBQ0YsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztDQUNKO0FBL0RELGtDQStEQyJ9

View File

@ -1,3 +1,2 @@
import 'typings-global'; import 'typings-global';
export import q = require('q'); export import q = require('q');
export declare let streamCombiner2: any;

View File

@ -1,5 +1,4 @@
"use strict"; "use strict";
require("typings-global"); require("typings-global");
exports.q = require("q"); exports.q = require("q");
exports.streamCombiner2 = require('stream-combiner2'); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJlYW0ucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c3RyZWFtLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUN2Qix5QkFBOEIifQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJlYW0ucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c3RyZWFtLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUN2Qix5QkFBOEI7QUFDbkIsUUFBQSxlQUFlLEdBQUcsT0FBTyxDQUFDLGtCQUFrQixDQUFDLENBQUEifQ==

View File

@ -26,7 +26,6 @@
"dependencies": { "dependencies": {
"@types/q": "0.x.x", "@types/q": "0.x.x",
"q": "^1.4.1", "q": "^1.4.1",
"stream-combiner2": "^1.1.1",
"typings-global": "^1.0.14" "typings-global": "^1.0.14"
} }
} }

View File

@ -4,12 +4,15 @@ const fs = require("fs");
const smartstream = require("../dist/index"); const smartstream = require("../dist/index");
let testSmartstream; let testSmartstream;
describe('smartstream', function () { describe('smartstream', function () {
it('should combine a stream', function () { it('should combine a stream', function (done) {
this.timeout(5000);
testSmartstream = new smartstream.Smartstream([ testSmartstream = new smartstream.Smartstream([
fs.createReadStream('./test/assets/test.md'), fs.createReadStream('./test/assets/test.md'),
fs.createWriteStream('./test/assets/testCopy.md') fs.createWriteStream('./test/assets/testCopy.md')
]); ]);
testSmartstream.run(); testSmartstream.run().then(() => {
done();
});
}); });
}); });
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQix5QkFBeUI7QUFHekIsNkNBQTRDO0FBRTVDLElBQUksZUFBd0MsQ0FBQTtBQUU1QyxRQUFRLENBQUMsYUFBYSxFQUFFO0lBQ3BCLEVBQUUsQ0FBQyx5QkFBeUIsRUFBRTtRQUMxQixlQUFlLEdBQUcsSUFBSSxXQUFXLENBQUMsV0FBVyxDQUFDO1lBQzFDLEVBQUUsQ0FBQyxnQkFBZ0IsQ0FBQyx1QkFBdUIsQ0FBQztZQUM1QyxFQUFFLENBQUMsaUJBQWlCLENBQUMsMkJBQTJCLENBQUM7U0FDcEQsQ0FBQyxDQUFBO1FBQ0YsZUFBZSxDQUFDLEdBQUcsRUFBRSxDQUFBO0lBQ3pCLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ== //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQix5QkFBeUI7QUFHekIsNkNBQTRDO0FBRTVDLElBQUksZUFBd0MsQ0FBQTtBQUU1QyxRQUFRLENBQUMsYUFBYSxFQUFFO0lBQ3BCLEVBQUUsQ0FBQyx5QkFBeUIsRUFBRSxVQUFTLElBQUk7UUFDdkMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUNsQixlQUFlLEdBQUcsSUFBSSxXQUFXLENBQUMsV0FBVyxDQUFDO1lBQzFDLEVBQUUsQ0FBQyxnQkFBZ0IsQ0FBQyx1QkFBdUIsQ0FBQztZQUM1QyxFQUFFLENBQUMsaUJBQWlCLENBQUMsMkJBQTJCLENBQUM7U0FDcEQsQ0FBQyxDQUFBO1FBQ0YsZUFBZSxDQUFDLEdBQUcsRUFBRSxDQUFDLElBQUksQ0FBQztZQUN2QixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9

View File

@ -7,11 +7,14 @@ import * as smartstream from '../dist/index'
let testSmartstream: smartstream.Smartstream let testSmartstream: smartstream.Smartstream
describe('smartstream', function() { describe('smartstream', function() {
it('should combine a stream', function(){ it('should combine a stream', function(done){
this.timeout(5000)
testSmartstream = new smartstream.Smartstream([ testSmartstream = new smartstream.Smartstream([
fs.createReadStream('./test/assets/test.md'), fs.createReadStream('./test/assets/test.md'),
fs.createWriteStream('./test/assets/testCopy.md') fs.createWriteStream('./test/assets/testCopy.md')
]) ])
testSmartstream.run() testSmartstream.run().then(() => {
done()
})
}) })
}) })

View File

@ -1,23 +1,86 @@
import * as plugins from './smartstream.plugins' import * as plugins from './smartstream.plugins'
export interface IErrorFunction { export interface IErrorFunction {
(err): number (err): any
} }
export interface IStreamStartFunction {
(stream): any
}
export interface ICustomEventFunction {
(): any
}
export interface ICustomEventObject {
eventName: string
eventFunction: ICustomEventFunction
}
/**
* class Smartstream handles
*/
export class Smartstream { export class Smartstream {
streamArray = [] streamArray = []
errorFunction: IErrorFunction = null errorFunction: IErrorFunction = null
constructor(streamArrayArg: any[]){ streamStartFunction: IStreamStartFunction = null
customEventObjectArray: ICustomEventObject[] = []
constructor(streamArrayArg: any[]) {
this.streamArray = streamArrayArg this.streamArray = streamArrayArg
} }
/**
* attach an error handler to the stream to prevent throwing
*/
onError(errorFunctionArg: IErrorFunction) { onError(errorFunctionArg: IErrorFunction) {
this.errorFunction = errorFunctionArg this.errorFunction = errorFunctionArg
} }
run() {
let combinedStream = plugins.streamCombiner2.obj(this.streamArray) /**
if (this.errorFunction !== null) { * make something with the stream itself
combinedStream.on('error', this.errorFunction) */
onStreamStart(): plugins.q.Promise<any> {
}
/**
* run the stream
* @returns Promise
*/
run(): plugins.q.Promise<void> {
let done = plugins.q.defer<void>()
// clone Array
let streamExecutionArray = []
for (let streamItem of this.streamArray) { streamExecutionArray.push(streamItem) }
// combine the stream
let finalStream = null
let firstIteration: boolean = true
for (let stream of streamExecutionArray) {
if (firstIteration === true) {
finalStream = stream
}
if (this.errorFunction !== null) {
stream.on('error', this.errorFunction)
}
for (let customEventObject of this.customEventObjectArray) {
stream.on(customEventObject.eventName, customEventObject.eventFunction)
}
if (!firstIteration) {
finalStream = finalStream.pipe(stream)
}
firstIteration = false
} }
return combinedStream finalStream.on('end',function(){
done.resolve()
})
finalStream.on('close',function(){
done.resolve()
})
finalStream.on('finish',function(){
done.resolve()
})
return done.promise
} }
} }

View File

@ -1,3 +1,2 @@
import 'typings-global' import 'typings-global'
export import q = require('q') export import q = require('q')
export let streamCombiner2 = require('stream-combiner2')