Compare commits

...

6 Commits

Author SHA1 Message Date
224b39f0a6 1.0.4 2016-09-25 16:28:45 +02:00
b24b564495 improved README 2016-09-25 16:28:42 +02:00
aabfb2721d 1.0.3 2016-09-25 16:10:11 +02:00
c28e2cb8e0 now allows the handling of custom events and run returns promise 2016-09-25 16:10:06 +02:00
aaece166c0 1.0.2 2016-09-25 14:01:55 +02:00
6380998fcf fix filename for gitlab ci 2016-09-25 14:01:50 +02:00
11 changed files with 228 additions and 30 deletions

View File

@ -1,9 +1,37 @@
# 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
myStream.onCustomEvent('myeventname', (args...) => { /* Do something */ }) // emit an custom event anywhere in your stream
mySmartstream.run().then(() => {/* do something when stream is finished */})
```

40
dist/index.d.ts vendored
View File

@ -1,10 +1,42 @@
/// <reference types="q" />
import * as plugins from './smartstream.plugins';
export interface IErrorFunction { export interface IErrorFunction {
(err: any): number; (err: 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[]; private streamArray;
errorFunction: IErrorFunction; private errorFunction;
private customEventObjectArray;
private streamStartedDeferred;
/**
* constructor
*/
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
*/
streamStarted(): plugins.q.Promise<any>;
/**
* attach listener to custom event
*/
onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction): void;
/**
* run the stream
* @returns Promise
*/
run(): plugins.q.Promise<void>;
} }

72
dist/index.js vendored
View File

@ -1,21 +1,81 @@
"use strict"; "use strict";
const plugins = require("./smartstream.plugins"); const plugins = require("./smartstream.plugins");
/**
* class Smartstream handles
*/
class Smartstream { class Smartstream {
/**
* constructor
*/
constructor(streamArrayArg) { constructor(streamArrayArg) {
this.streamArray = []; this.streamArray = [];
this.errorFunction = null; this.errorFunction = null;
this.customEventObjectArray = [];
this.streamStartedDeferred = plugins.q.defer();
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;
} }
run() { /**
let combinedStream = plugins.streamCombiner2.obj(this.streamArray); * make something with the stream itself
if (this.errorFunction !== null) { */
combinedStream.on('error', this.errorFunction); streamStarted() {
return this.streamStartedDeferred.promise;
} }
return combinedStream; /**
* attach listener to custom event
*/
onCustomEvent(eventNameArg, eventFunctionArg) {
this.customEventObjectArray.push({
eventName: eventNameArg,
eventFunction: eventFunctionArg
});
}
/**
* run the stream
* @returns Promise
*/
run() {
let done = plugins.q.defer();
// clone Array
let streamExecutionArray = [];
for (let streamItem of this.streamArray) {
streamExecutionArray.push(streamItem);
}
// 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;
}
this.streamStartedDeferred.resolve();
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsaURBQWdEO0FBTWhEO0lBR0ksWUFBWSxjQUFxQjtRQUZqQyxnQkFBVyxHQUFHLEVBQUUsQ0FBQTtRQUNoQixrQkFBYS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@ -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

@ -1,6 +1,6 @@
{ {
"name": "smartstream", "name": "smartstream",
"version": "1.0.1", "version": "1.0.4",
"description": "simplifies access to node streams, TypeScript ready!", "description": "simplifies access to node streams, TypeScript ready!",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
@ -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,99 @@
import * as plugins from './smartstream.plugins' import * as plugins from './smartstream.plugins'
export interface IErrorFunction { export interface IErrorFunction {
(err): number (err): any
} }
export interface ICustomEventFunction {
(): any
}
export interface ICustomEventObject {
eventName: string
eventFunction: ICustomEventFunction
}
/**
* class Smartstream handles
*/
export class Smartstream { export class Smartstream {
streamArray = [] private streamArray = []
errorFunction: IErrorFunction = null private errorFunction: IErrorFunction = null
private customEventObjectArray: ICustomEventObject[] = []
private streamStartedDeferred = plugins.q.defer()
/**
* constructor
*/
constructor(streamArrayArg: any[]) { 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) /**
* make something with the stream itself
*/
streamStarted(): plugins.q.Promise<any> {
return this.streamStartedDeferred.promise
}
/**
* attach listener to custom event
*/
onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction) {
this.customEventObjectArray.push({
eventName: eventNameArg,
eventFunction: eventFunctionArg
})
}
/**
* 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) { if (this.errorFunction !== null) {
combinedStream.on('error', this.errorFunction) stream.on('error', this.errorFunction)
} }
return combinedStream for (let customEventObject of this.customEventObjectArray) {
stream.on(customEventObject.eventName, customEventObject.eventFunction)
}
if (!firstIteration) {
finalStream = finalStream.pipe(stream)
}
firstIteration = false
}
this.streamStartedDeferred.resolve()
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')