Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lazd committed Dec 11, 2013
1 parent 11159eb commit 67a1a7f
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/*.log
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.6
- 0.7
- 0.8
- 0.9
- 0.10
47 changes: 44 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
gulp-replacer
=============
# gulp-replace [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url]
> A string replace plugin for gulp 3
A Gulp plugin that replaces strings
## Usage

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

```shell
npm install --save-dev gulp-replace
```

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

```javascript
var jshint = require('gulp-replace');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(gulp.dest('build/file.txt'));
});
```

## API

### replace(search, replace)

#### search
Type: `RegExp`

The regex pattern to search for. See the [MDN documentation for RegExp] for details.

#### replace
Type: `String` or `Function`

The replacement string or function. See the [MDN documentation for String.replace] for details.


[MDN documentation for RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
[MDN documentation for String.replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter

[travis-url]: http://travis-ci.org/lazd/gulp-replace
[travis-image]: https://secure.travis-ci.org/lazd/gulp-replace.png?branch=master
[npm-url]: https://npmjs.org/package/gulp-replace
[npm-image]: https://badge.fury.io/js/gulp-replace.png
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var es = require('event-stream');

module.exports = function(search, replace) {
var doRegexReplace = function(file, callback) {
var result = String(file.contents).replace(search, replace);

file.contents = new Buffer(result);

callback(null, file);
};

return es.map(doRegexReplace);
};
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "gulp-replace",
"version": "0.1.0",
"description": "A string replace plugin for gulp",
"main": "index.js",
"dependencies": {
"event-stream": "~3.0.18"
},
"devDependencies": {
"should": "~2.1.1",
"mocha": "~1.15.1",
"gulp-util": "~2.0.0"
},
"scripts": {
"test": "./node_modules/.bin/mocha"
},
"repository": {
"type": "git",
"url": "git://github.com/lazd/gulp-replace.git"
},
"keywords": [
"gulpplugin",
"replace"
],
"author": "Larry Davis <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/lazd/gulp-replace/issues"
},
"engines": {
"node": ">=0.8"
}
}
8 changes: 8 additions & 0 deletions test/expected/Basic_amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
define(['handlebars'], function(Handlebars) {return Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "Basic template";
});});
8 changes: 8 additions & 0 deletions test/expected/Basic_bare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "Basic template";
})
8 changes: 8 additions & 0 deletions test/expected/Basic_bare_unwrapped.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "Basic template";
}
8 changes: 8 additions & 0 deletions test/expected/Basic_browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
this["Basic"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "Basic template";
});
8 changes: 8 additions & 0 deletions test/expected/Basic_commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(Handlebars) {return Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "Basic template";
});};
8 changes: 8 additions & 0 deletions test/expected/Basic_hybrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function(g) {var Handlebars = g.Handlebars || require('handlebars');var template = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "Basic template";
});if (typeof exports === 'object' && exports) module.exports = template;this["Basic"] = template;}(typeof window !== 'undefined' ? window : global));
8 changes: 8 additions & 0 deletions test/expected/Basic_node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Handlebars = global.Handlebars || require('handlebars');module.exports = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "Basic template";
});
1 change: 1 addition & 0 deletions test/fixtures/Basic.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basic template
37 changes: 37 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var replacePlugin = require('../');
var path = require('path');
var should = require('should');
var gutil = require('gulp-util');
require('mocha');

var makeFile = function(contents) {
return new gutil.File({
path: 'test/file.txt',
cwd: 'test/',
base: 'test/',
contents: contents
});
};

describe('gulp-replace', function() {
describe('replacePlugin()', function() {
it('should replace regular expression', function(done) {
var file = makeFile('foobar foobaz');

var stream = replacePlugin(/foo(.{3})/g, '$1foo');
stream.on('data', function(newFile) {
should.exist(newFile);
should.exist(newFile.contents);
String(newFile.contents).should.equal('barfoo bazfoo');
});

stream.once('end', function() {
done();
});

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

});
});

0 comments on commit 67a1a7f

Please sign in to comment.