Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Jan 6, 2014
0 parents commit 36ad7ca
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[test/fixtures/*]
insert_final_newline = false
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules/
temp/
21 changes: 21 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "double",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"white": true
}
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- '0.8'
- '0.10'
before_install:
- currentfolder=${PWD##*/}
- if [ "$currentfolder" != 'generator-gulp-plugin' ]; then cd .. && eval "mv $currentfolder generator-gulp-plugin" && cd generator-gulp-plugin; fi
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2014 Vsevolod Strukchinsky

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# gulp-plumber [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]

> A plumber for [gulp](https://github.com/wearefractal/gulp)
This plugin is fixing [issue with Node Streams piping](https://github.com/gulpjs/gulp/issues/91).

For explanations, read [this small article](https://gist.github.com/floatdrop/8269868).

## Usage

First, install `gulp-plumber` as a development dependency:

```shell
npm install --save gulp-plumber
```

Then, add it to your `gulpfile.js`:

```javascript
var plumber = require('gulp-plumber');
var coffee = require('gulp-coffee');

gulp.src('./src/*.ext')
.pipe(plumber())
.pipe(coffee())
.pipe(gulp.dest("./dist"));
```

## API

### plumber(options)

Returns Stream, that fixes `pipe` methods on Streams that are next in pipeline.

## License

[MIT License](http://en.wikipedia.org/wiki/MIT_License)

[npm-url]: https://npmjs.org/package/gulp-plumber
[npm-image]: https://badge.fury.io/js/gulp-plumber.png

[travis-url]: http://travis-ci.org/floatdrop/gulp-plumber
[travis-image]: https://secure.travis-ci.org/floatdrop/gulp-plumber.png?branch=master

[depstat-url]: https://david-dm.org/floatdrop/gulp-plumber
[depstat-image]: https://david-dm.org/floatdrop/gulp-plumber.png
109 changes: 109 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use strict';

var PassThrough = require('stream').PassThrough;

module.exports = function (param) {

var through = new PassThrough();

through.pipe = pipe2;

function preventDefaultErrorHandler(stream) {
stream.listeners('error').forEach(function(item) {
if(item.name == 'onerror') this.removeListener('error', item);
}, stream);
}

through.on('pipe', function (source) {
preventDefaultErrorHandler(source);
};

function pipe2(dest, options) {
var source = this;

function ondata(chunk) {
if (dest.writable) {
if (false === dest.write(chunk) && source.pause) {
source.pause();
}
}
}

source.on('data', ondata);

function ondrain() {
if (source.readable && source.resume) {
source.resume();
}
}

dest.on('drain', ondrain);

// If the 'end' option is not supplied, dest.end() will be called when
// source gets the 'end' or 'close' events. Only dest.end() once.
if (!dest._isStdio && (!options || options.end !== false)) {
source.on('end', onend);
source.on('close', onclose);
}

var didOnEnd = false;
function onend() {
if (didOnEnd) return;
didOnEnd = true;

dest.end();
}


function onclose() {
if (didOnEnd) return;
didOnEnd = true;

if (util.isFunction(dest.destroy)) dest.destroy();
}

// don't leave dangling pipes when there are errors.
function onerror(er) {
if (EE.listenerCount(this, 'error') === 0) {
cleanup();
throw er; // Unhandled stream error in pipe.
}
}

source.on('error', onerror);
dest.on('error', onerror);

// remove all the event listeners that were added.
function cleanup() {
source.removeListener('data', ondata);
dest.removeListener('drain', ondrain);

source.removeListener('end', onend);
source.removeListener('close', onclose);

source.removeListener('error', onerror);
dest.removeListener('error', onerror);

source.removeListener('end', cleanup);
source.removeListener('close', cleanup);

dest.removeListener('close', cleanup);
}

source.on('end', cleanup);
source.on('close', cleanup);

dest.on('close', cleanup);

dest.emit('pipe', source);

// Allow for unix-like usage: A.pipe(B).pipe(C)

dest.pipe = pipe2;

return dest;
};

return through;

};
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

{
"name": "gulp-plumber",
"version": "0.0.0",
"description": "A plumber, for gulp pipeline",
"keywords": [
"gulpplugin"
],
"homepage": "https://github.com/floatdrop/gulp-plumber",
"bugs": "https://github.com/floatdrop/gulp-plumber/issues",
"author": {
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com",
"url": "https://github.com/floatdrop"
},
"main": "./index.js",
"repository": {
"type": "git",
"url": "git://github.com/floatdrop/gulp-plumber.git"
},
"scripts": {
"test": "mocha"
},
"dependencies": {},
"devDependencies": {
"mocha": "~1.14.0",
"should": "~2.1.0",
"gulp-util": "~2.2.0"
},
"engines": {
"node": ">=0.10",
"npm": ">=1.2.10"
},
"licenses": [
{
"type": "MIT"
}
]
}
2 changes: 2 additions & 0 deletions test/expected/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello
World
1 change: 1 addition & 0 deletions test/fixtures/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
109 changes: 109 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*global describe, it*/
"use strict";

var fs = require("fs"),
es = require("event-stream"),
should = require("should");
require("mocha");

var gutil = require("gulp-util"),
plumber = require("../");

describe("gulp-plumber", function () {

var expectedFile = new gutil.File({
path: "test/expected/hello.txt",
cwd: "test/",
base: "test/expected",
contents: fs.readFileSync("test/expected/hello.txt")
});

it("should produce expected file via buffer", function (done) {

var srcFile = new gutil.File({
path: "test/fixtures/hello.txt",
cwd: "test/",
base: "test/fixtures",
contents: fs.readFileSync("test/fixtures/hello.txt")
});

var stream = plumber("World");

stream.on("error", function(err) {
should.exist(err);
done(err);
});

stream.on("data", function (newFile) {

should.exist(newFile);
should.exist(newFile.contents);

String(newFile.contents).should.equal(String(expectedFile.contents));
done();
});

stream.write(srcFile);
stream.end();
});

it("should error on stream", function (done) {

var srcFile = new gutil.File({
path: "test/fixtures/hello.txt",
cwd: "test/",
base: "test/fixtures",
contents: fs.createReadStream("test/fixtures/hello.txt")
});

var stream = plumber("World");

stream.on("error", function(err) {
should.exist(err);
done();
});

stream.on("data", function (newFile) {
newFile.contents.pipe(es.wait(function(err, data) {
done(err);
}));
});

stream.write(srcFile);
stream.end();
});

/*
it("should produce expected file via stream", function (done) {
var srcFile = new gutil.File({
path: "test/fixtures/hello.txt",
cwd: "test/",
base: "test/fixtures",
contents: fs.createReadStream("test/fixtures/hello.txt")
});
var stream = plumber("World");
stream.on("error", function(err) {
should.exist(err);
done();
});
stream.on("data", function (newFile) {
should.exist(newFile);
should.exist(newFile.contents);
newFile.contents.pipe(es.wait(function(err, data) {
should.not.exist(err);
data.should.equal(String(expectedFile.contents));
done();
}));
});
stream.write(srcFile);
stream.end();
});
*/
});

0 comments on commit 36ad7ca

Please sign in to comment.