Skip to content

Commit

Permalink
feat: solved vulnerabilities, added types definition and updated read…
Browse files Browse the repository at this point in the history
…me (#111)

* 🎉 solved vulnerabilities, added types definition and updated readme
* replaced new Buffer(string) (DEPRECATED) with Buffer.from(string)
* replaced 'var' with 'let/const'
* updated dependencies to solve vulnerabilities
* migrated from 'readable-stream' to stream(node-core) and remove the dependency
* updated readme for gulp 4
* added types definition
* updated code snippet
* adjusted example code
* ⛄ updated readme
* added link in README.md

Co-authored-by: jonz <[email protected]>
  • Loading branch information
jon2180 and jonz authored Apr 22, 2021
1 parent 015a90d commit 87e5403
Show file tree
Hide file tree
Showing 12 changed files with 4,470 additions and 128 deletions.
148 changes: 148 additions & 0 deletions README-gulp3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# gulp-replace [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url]

> A string replace plugin for gulp
## Usage

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

```shell
npm install --save-dev gulp-replace
# or
yarn add --dev gulp-replace
```

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

### Simple string replace

```javascript
const replace = require('gulp-replace');

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

### Simple regex replace

```javascript
const replace = require('gulp-replace');

gulp.task('templates', function(){
gulp.src(['file.txt'])
// See https://mdn.io/string.replace#Specifying_a_string_as_a_parameter
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(gulp.dest('build/'));
});
```

### String replace with function callback

```javascript
const replace = require('gulp-replace');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace('foo', function(match) {
// Replaces instances of "foo" with "oof"
return match.reverse();
}))
.pipe(gulp.dest('build/'));
});
```

### Regex replace with function callback

```javascript
const replace = require('gulp-replace');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace(/foo(.{3})/g, function(match, p1, offset, string) {
// Replace foobaz with barbaz and log a ton of information
// See https://mdn.io/string.replace#Specifying_a_function_as_a_parameter
console.log('Found ' + match + ' with param ' + p1 + ' at ' + offset + ' inside of ' + string);
return 'bar' + p1;
}))
.pipe(gulp.dest('build/'));
});
```

### Function callback with file object

```javascript
const replace = require('gulp-replace');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace('filename', function() {
// Replaces instances of "filename" with "file.txt"
// this.file is also available for regex replace
// See https://github.com/gulpjs/vinyl#instance-properties for details on available properties
return this.file.relative;
}))
.pipe(gulp.dest('build/'));
});
```

## API

`gulp-replace` can be called with a string or regex.

### replace(string, replacement[, options])

#### string

Type: `String`

The string to search for.

#### replacement

Type: `String` or `Function`

The replacement string or function. If `replacement` is a function, it will be called once for each match and will be passed the string that is to be replaced.

The value of `this.file` will be equal to the [vinyl instance](https://github.com/gulpjs/vinyl#instance-properties) for the file being processed.

### replace(regex, replacement[, options])

#### regex

Type: `RegExp`

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

#### replacement

Type: `String` or `Function`

The replacement string or function. See the [MDN documentation for String.replace] for details on special replacement string patterns and arguments to the replacement function.

The value of `this.file` will be equal to the [vinyl instance](https://github.com/gulpjs/vinyl#instance-properties) for the file being processed.

### gulp-replace options

An optional third argument, `options`, can be passed.

#### options

Type: `Object`

##### options.skipBinary

Type: `boolean`
Default: `true`

Skip binary files. This option is `true` by default. If you want to replace content in binary files, you must explicitly set it to `false`.

[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]: https://travis-ci.org/lazd/gulp-replace
[travis-image]: https://secure.travis-ci.org/lazd/gulp-replace.svg?branch=master
[npm-url]: https://npmjs.org/package/gulp-replace
[npm-image]: https://badge.fury.io/js/gulp-replace.svg
100 changes: 64 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,98 +1,123 @@
# gulp-replace [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url]

> A string replace plugin for gulp
[Read me for gulp 3](README-gulp3.md)

## Usage

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

```shell
npm install --save-dev gulp-replace
# or
yarn add --dev gulp-replace
```

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

### Simple string replace

```javascript
const replace = require('gulp-replace');
const { src, dest } = require('gulp');

gulp.task('templates', function(){
gulp.src(['file.txt'])
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('bar', 'foo'))
.pipe(gulp.dest('build/'));
});
.pipe(dest('build/'));
};

exports.replaceTemplate = replaceTemplate;
```

### Simple regex replace

```javascript
const replace = require('gulp-replace');
const { src, dest } = require('gulp');

function replaceTemplate() {
return src(['file.txt'])
// See https://mdn.io/string.replace#Specifying_a_string_as_a_parameter
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(dest('build/'));
};

gulp.task('templates', function(){
gulp.src(['file.txt'])
// See https://mdn.io/string.replace#Specifying_a_string_as_a_parameter
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(gulp.dest('build/'));
});
exports.replaceTemplate = replaceTemplate;
```

### String replace with function callback

```javascript
const replace = require('gulp-replace');
const { src, dest } = require('gulp');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace('foo', function(match) {
// Replaces instances of "foo" with "oof"
return match.reverse();
}))
.pipe(gulp.dest('build/'));
});
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('foo', function handleReplace(match){ return match.reverse(); })
.pipe(dest('build/'))
};

exports.replaceTemplate = replaceTemplate;
```
### Regex replace with function callback
```javascript
const replace = require('gulp-replace');
const { src, dest } = require('gulp');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace(/foo(.{3})/g, function(match, p1, offset, string) {
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace(/foo(.{3})/g, function handleReplace(match, p1, offset, string) {
// Replace foobaz with barbaz and log a ton of information
// See https://mdn.io/string.replace#Specifying_a_function_as_a_parameter
console.log('Found ' + match + ' with param ' + p1 + ' at ' + offset + ' inside of ' + string);
return 'bar' + p1;
}))
.pipe(gulp.dest('build/'));
});
.pipe(dest('build/'));
};

exports.replaceTemplate = replaceTemplate;
```
### Function callback with file object
```javascript
const replace = require('gulp-replace');

gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace('filename', function() {
// Replaces instances of "filename" with "file.txt"
// this.file is also available for regex replace
// See https://github.com/gulpjs/vinyl#instance-properties for details on available properties
return this.file.relative;
}))
.pipe(gulp.dest('build/'));
});
const { src, dest } = require('gulp');

function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('filename', function handleReplace() {
// Replaces instances of "filename" with "file.txt"
// this.file is also available for regex replace
// See https://github.com/gulpjs/vinyl#instance-properties for details on available properties
return this.file.relative;
}))
.pipe(dest('build/'));
};

exports.replaceTemplate = replaceTemplate;
```

## API
`gulp-replace` can be called with a string or regex.
### replace(string, replacement[, options])
> CAUTION: `replacement` could **NOT be arrow function**, because arrow function could not bind `this`
#### string
Type: `String`
The string to search for.
#### replacement
Type: `String` or `Function`
The replacement string or function. If `replacement` is a function, it will be called once for each match and will be passed the string that is to be replaced.
Expand All @@ -102,11 +127,13 @@ The value of `this.file` will be equal to the [vinyl instance](https://github.co
### replace(regex, replacement[, options])
#### regex
Type: `RegExp`
The regex pattern to search for. See the [MDN documentation for RegExp] for details.
#### replacement
Type: `String` or `Function`
The replacement string or function. See the [MDN documentation for String.replace] for details on special replacement string patterns and arguments to the replacement function.
Expand All @@ -118,15 +145,16 @@ The value of `this.file` will be equal to the [vinyl instance](https://github.co
An optional third argument, `options`, can be passed.
#### options
Type: `Object`
##### options.skipBinary
Type: `boolean`
Default: `true`
Skip binary files. This option is `true` by default. If you want to replace content in binary files, you must explicitly set it to `false`.

[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
18 changes: 10 additions & 8 deletions examples/inplace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ This example shows you how to replace file contents in place.

Type the following commands from the root of this repository:

```
```bash
npm install # install the plugin's dependencies
cd examples/inplace
npm install # install the example's dependencies
cat file.txt # See original file contentes
gulp
npx gulp
cat file.txt # See changed file contents
```

You should see something like this:

```js
```bash
$ cat file.txt
The roof is on fire!
$ gulp
[gulp] Starting 'replace'...
[gulp] Finished 'replace' after 19 ms
[gulp] Starting 'default'...
[gulp] Finished 'default' after 8.01 μs

$ npx gulp
23:03:34] Using gulpfile ~/gulp-replace/examples/inplace/gulpfile.js
[23:03:34] Starting 'default'...
[23:03:34] Finished 'default' after 31 ms

$ cat file.txt
The world is on fire!
```
6 changes: 3 additions & 3 deletions examples/inplace/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var gulp = require('gulp');
/** REMOVE ME **/ var replace = require('../../');
/** USE ME **/ // var replace = require('gulp-replace');

gulp.task('replace', function() {
function replaceText() {
// Do an in-place replace on file.txt
return gulp.src('file.txt', { base : './' } )
.pipe(replace('roof', 'world'))
.pipe(gulp.dest('./'));
});
}

gulp.task('default', ['replace']);
module.exports.default = replaceText;
Loading

0 comments on commit 87e5403

Please sign in to comment.