gulp-replace
Advanced tools
Comparing version 0.1.0 to 0.2.0
35
index.js
var es = require('event-stream'); | ||
var rs = require('replacestream'); | ||
var stream = require('stream'); | ||
module.exports = function(search, replace) { | ||
var doRegexReplace = function(file, callback) { | ||
var result = String(file.contents).replace(search, replace); | ||
module.exports = function(search, replacement) { | ||
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; | ||
file.contents = new Buffer(result); | ||
if (isRegExp && isStream) { | ||
return callback(new Error('gulp-replace: Cannot do regexp replace on a stream'), file); | ||
} | ||
if (!isRegExp && typeof replacement === 'function') { | ||
return callback(new Error('gulp-replace: Cannot do string replace with a function as replacement value'), file); | ||
} | ||
if (isStream) { | ||
file.contents = file.contents.pipe(rs(search, replacement)); | ||
return callback(null, file); | ||
} | ||
if (isBuffer) { | ||
if (isRegExp) { | ||
file.contents = new Buffer(String(file.contents).replace(search, replacement)); | ||
} | ||
else { | ||
file.contents = new Buffer(String(file.contents).split(search).join(replacement)); | ||
} | ||
return callback(null, file); | ||
} | ||
callback(null, file); | ||
}; | ||
return es.map(doRegexReplace); | ||
return es.map(doReplace); | ||
}; |
{ | ||
"name": "gulp-replace", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A string replace plugin for gulp", | ||
"main": "index.js", | ||
"dependencies": { | ||
"event-stream": "~3.0.18" | ||
"event-stream": "~3.0.18", | ||
"replacestream": "0.0.6" | ||
}, | ||
@@ -31,4 +32,4 @@ "devDependencies": { | ||
"engines": { | ||
"node": ">=0.6" | ||
"node": ">=0.8" | ||
} | ||
} |
@@ -24,7 +24,24 @@ # gulp-replace [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] | ||
## API | ||
### replace(search, replace) | ||
gulp-replace can be called with a string or regex. | ||
#### search | ||
### replace(string, replacement) | ||
#### string | ||
Type: `String` | ||
The string to search for. | ||
#### replacement | ||
Type: `String` | ||
The replacement string. | ||
### replace(regex, replace) | ||
*Note:* gulp-replace cannot perform regex replacement on streams. | ||
#### regex | ||
Type: `RegExp` | ||
@@ -34,3 +51,3 @@ | ||
#### replace | ||
#### replacement | ||
Type: `String` or `Function` | ||
@@ -37,0 +54,0 @@ |
125
test/main.js
var replacePlugin = require('../'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var es = require('event-stream'); | ||
var should = require('should'); | ||
@@ -18,21 +20,128 @@ var gutil = require('gulp-util'); | ||
describe('replacePlugin()', function() { | ||
it('should replace regular expression', function(done) { | ||
var file = makeFile('foobar foobaz'); | ||
it('should replace string on a stream', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.createReadStream('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin(/foo(.{3})/g, '$1foo'); | ||
var stream = replacePlugin('world', 'person'); | ||
stream.on('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal('barfoo bazfoo'); | ||
newFile.contents.pipe(es.wait(function(err, data) { | ||
should.not.exist(err); | ||
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8')); | ||
done(); | ||
})); | ||
}); | ||
stream.once('end', function() { | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
it('should replace string on a buffer', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.readFileSync('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin('world', 'person'); | ||
stream.on('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8')); | ||
done(); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
it('should replace regex on a buffer', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.readFileSync('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin(/world/g, 'person'); | ||
stream.on('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8')); | ||
done(); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
it('should error when searching with regex on a stream', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.createReadStream('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin(/world/, 'person'); | ||
stream.on('data', function() { | ||
throw new Error('Stream should not have emitted data event'); | ||
}); | ||
stream.on('error', function(err) { | ||
should.exist(err); | ||
done(); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
it('should error when replaceing a string with a function on a stream', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.createReadStream('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin('world', function() { return 'person'; }); | ||
stream.on('data', function() { | ||
throw new Error('Stream should not have emitted data event'); | ||
}); | ||
stream.on('error', function(err) { | ||
should.exist(err); | ||
done(); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
it('should error when replaceing a string with a function on a stream', function(done) { | ||
var file = new gutil.File({ | ||
path: 'test/fixtures/helloworld.txt', | ||
cwd: 'test/', | ||
base: 'test/fixtures', | ||
contents: fs.readFileSync('test/fixtures/helloworld.txt') | ||
}); | ||
var stream = replacePlugin('world', function() { return 'person'; }); | ||
stream.on('data', function() { | ||
throw new Error('Stream should not have emitted data event'); | ||
}); | ||
stream.on('error', function(err) { | ||
should.exist(err); | ||
done(); | ||
}); | ||
stream.write(file); | ||
stream.end(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9149
158
63
2
9
2
+ Addedreplacestream@0.0.6
+ Addedreplacestream@0.0.6(transitive)