Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passes full file path to the replacement function #76

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ gulp.task('templates', function(){
.pipe(gulp.dest('build/file.txt'));
});
```
### Use FilePath
```javascript
var replace = require('gulp-replace');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace(/foo(.{3})/g, function($0, str){
return str + 'foo' + this.filePath;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 'bar in' + this.filePath;

It is customary to replace foo with bar :) In this case, we can just tack the path onto the end.

}, {passFileName: true}))
.pipe(gulp.dest('build/file.txt'));
});
```

### String Replace
```javascript
var replace = require('gulp-replace');
Expand Down Expand Up @@ -61,6 +74,8 @@ Type: `String` or `Function`

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

if `Function` and `options.passFileName` is true, then Function within the filePath is equal to the current file `file.path`.

### gulp-replace options

An optional third argument, `options`, can be passed.
Expand All @@ -69,11 +84,17 @@ An optional third argument, `options`, can be passed.
Type: `Object`

##### options.skipBinary
Type: `boolean`
Type: `boolean`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These actually need two spaces or a line break after them, otherwise they appear on the same line

Default: `false`

Skip binary files

##### options.passFileName
Type: `boolean`
Default: `false`

Passes full file path to the replacement function
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes the full file path available inside the replacement function as this.filePath



[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
Expand Down
45 changes: 33 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var rs = require('replacestream');
var istextorbinary = require('istextorbinary');

module.exports = function(search, replacement, options) {
var userReplacement = replacement;

return new Transform({
objectMode: true,
transform: function(file, enc, callback) {
Expand Down Expand Up @@ -55,20 +57,39 @@ module.exports = function(search, replacement, options) {
callback(null, file);
}

if (options && options.skipBinary) {
istextorbinary.isText(file.path, file.contents, function(err, result) {
if (err) {
return callback(err, file);
}
if (options) {
if (options.passFileName && typeof replacement === 'function') {
var context = {
filePath: file.path
};
replacement = userReplacement.bind(context);

if (!result) {
callback(null, file);
} else {
doReplace();
}
});
// replacement = (function(path){
// return function () {
// var context = {
// filePath: path
// };
// return userReplacement.apply(context, arguments);
// };
// })(file.path);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented out code please.

}

if(options.skipBinary) {
istextorbinary.isText(file.path, file.contents, function(err, result) {
if (err) {
return callback(err, file);
}

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

return;
}

return;
}

doReplace();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-replace",
"version": "0.5.4",
"name": "gulp-replace-fix",
"version": "0.6.4",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the package name change and the version increment please.

"description": "A string replace plugin for gulp",
"dependencies": {
"istextorbinary": "1.0.2",
Expand Down