Skip to content

Commit

Permalink
Merge pull request #38 from shinnn/through2
Browse files Browse the repository at this point in the history
Use through2 instead of event-stream
  • Loading branch information
lazd committed Feb 18, 2015
2 parents 3ad3594 + 0767b53 commit b495331
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,3 +1,4 @@
sudo: false
language: node_js
node_js:
- 0.10
30 changes: 15 additions & 15 deletions index.js
@@ -1,22 +1,23 @@
var es = require('event-stream');
'use strict';

var through = require('through2');
var rs = require('replacestream');
var stream = require('stream');
var istextorbinary = require('istextorbinary');

module.exports = function(search, replacement, options) {
var doReplace = function(file, callback) {
var isRegExp = search instanceof RegExp;
var isStream = file.contents && typeof file.contents.on === 'function' && typeof file.contents.pipe === 'function';
var isBuffer = file.contents instanceof Buffer;
var doReplace = function(file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
}

function doReplace() {
if (isStream) {
if (file.isStream()) {
file.contents = file.contents.pipe(rs(search, replacement));
return callback(null, file);
}

if (isBuffer) {
if (isRegExp) {
if (file.isBuffer()) {
if (search instanceof RegExp) {
file.contents = new Buffer(String(file.contents).replace(search, replacement));
}
else {
Expand Down Expand Up @@ -50,7 +51,6 @@ module.exports = function(search, replacement, options) {
}

callback(null, file);

}

if (options && options.skipBinary) {
Expand All @@ -60,17 +60,17 @@ module.exports = function(search, replacement, options) {
}

if (!result) {
return callback(null, file);
callback(null, file);
} else {
doReplace();
}
});

return;
}
else {
doReplace();
}

doReplace();
};

return es.map(doReplace);
return through.obj(doReplace);
};
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -2,19 +2,19 @@
"name": "gulp-replace",
"version": "0.5.2",
"description": "A string replace plugin for gulp",
"main": "index.js",
"dependencies": {
"event-stream": "3.2.1",
"replacestream": "2.0.0",
"istextorbinary": "1.0.2"
"istextorbinary": "1.0.2",
"replacestream": "^2.0.0",
"through2": "^0.6.3"
},
"devDependencies": {
"should": "^4.6.0",
"event-stream": "^3.2.1",
"mocha": "^2.1.0",
"gulp-util": "^3.0.2"
"should": "^4.6.0",
"vinyl": "^0.4.6"
},
"scripts": {
"test": "./node_modules/.bin/mocha"
"test": "mocha"
},
"repository": {
"type": "git",
Expand Down
37 changes: 19 additions & 18 deletions test/main.js
@@ -1,15 +1,16 @@
'use strict';

var replacePlugin = require('../');
var fs = require('fs');
var path = require('path');
var es = require('event-stream');
var should = require('should');
var gutil = require('gulp-util');
var File = require('vinyl');
require('mocha');

describe('gulp-replace', function() {
describe('replacePlugin()', function() {
it('should replace string on a stream', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -33,7 +34,7 @@ describe('gulp-replace', function() {
});

it('should replace string on a buffer', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -54,7 +55,7 @@ describe('gulp-replace', function() {
});

it('should replace regex on a stream', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -78,7 +79,7 @@ describe('gulp-replace', function() {
});

it('should replace regex on a buffer', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -99,7 +100,7 @@ describe('gulp-replace', function() {
});

it('should replace regex on a stream with a function', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -123,7 +124,7 @@ describe('gulp-replace', function() {
});

it('should replace regex on a buffer with a function', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -144,7 +145,7 @@ describe('gulp-replace', function() {
});

it('should replace string on a stream with a function', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -168,7 +169,7 @@ describe('gulp-replace', function() {
});

it('should replace string on a buffer with a function', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -189,7 +190,7 @@ describe('gulp-replace', function() {
});

it('should call function once for each replacement when replacing a string on a stream', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand Down Expand Up @@ -219,7 +220,7 @@ describe('gulp-replace', function() {
});

it('should call function once for each replacement when replacing a regex on a stream', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand Down Expand Up @@ -249,7 +250,7 @@ describe('gulp-replace', function() {
});

it('should call function once for each replacement when replacing a string on a buffer', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -276,7 +277,7 @@ describe('gulp-replace', function() {
});

it('should call function once for each replacement when replacing a regex on a buffer', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -303,7 +304,7 @@ describe('gulp-replace', function() {
});

it('should ignore binary files when skipBinary is enabled', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/binary.png',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -324,7 +325,7 @@ describe('gulp-replace', function() {
});

it('should replace string on non binary files when skipBinary is enabled', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -348,15 +349,15 @@ describe('gulp-replace', function() {
});

it('should trigger events on a stream', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/helloworld.txt',
cwd: 'test/',
base: 'test/fixtures',
contents: fs.readFileSync('test/fixtures/helloworld.txt')
});

var stream = replacePlugin('world', 'elephant')
.on('end', function() {
.on('finish', function() {
// No assertion required, we should end up here, if we don't the test will time out
done();
});
Expand Down
10 changes: 5 additions & 5 deletions test/realworld.js
@@ -1,15 +1,15 @@
'use strict';

var replacePlugin = require('../');
var fs = require('fs');
var path = require('path');
var es = require('event-stream');
var should = require('should');
var gutil = require('gulp-util');
var File = require('vinyl');
require('mocha');

describe('gulp-replace', function() {
describe('real world use cases', function() {
it('drop use strict on a buffer', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/strict.js',
cwd: 'test/',
base: 'test/fixtures',
Expand All @@ -30,7 +30,7 @@ describe('gulp-replace', function() {
});

it('replace script versions in HTML', function(done) {
var file = new gutil.File({
var file = new File({
path: 'test/fixtures/scriptpage.html',
cwd: 'test/',
base: 'test/fixtures',
Expand Down

0 comments on commit b495331

Please sign in to comment.