Skip to content

Commit 9b4a4c1

Browse files
committed
feat(renew): Add gulp-renew plugin for batch string replacement
1 parent 0483d81 commit 9b4a4c1

File tree

8 files changed

+748
-0
lines changed

8 files changed

+748
-0
lines changed

plugins/renew/README.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# gulp-renew
2+
3+
[![version](https://img.shields.io/npm/v/gulp-renew?style=flat-square&logo=npm)](https://www.npmjs.com/package/gulp-renew)
4+
[![Codecov](https://img.shields.io/codecov/c/github/meqn/pipflow?token=5TYW2Z1S4C&style=flat-square&logo=codecov)](https://codecov.io/gh/Meqn/pipflow)
5+
[![release](https://img.shields.io/github/actions/workflow/status/meqn/pipflow/release.yml?style=flat-square)](https://github.com/Meqn/pipflow/releases)
6+
[![node.js](https://img.shields.io/node/v/gulp-renew?style=flat-square&logo=nodedotjs)](https://nodejs.org/en/about/releases/)
7+
[![license](https://img.shields.io/npm/l/gulp-renew?style=flat-square)](https://github.com/Meqn/pipflow)
8+
9+
A plugin that supports batch string replacement for gulp.
10+
Replacement rules support regular expressions and function replacements.
11+
12+
> Based on [gulp-replace](https://www.npmjs.com/package/gulp-replace)
13+
14+
## Installation
15+
16+
```
17+
npm install --save-dev gulp-renew
18+
```
19+
20+
## Usage
21+
22+
```js
23+
const gulp = require('gulp');
24+
const renew = require('gulp-renew');
25+
26+
gulp.task('default', function() {
27+
return gulp.src('src/**/*.js')
28+
.pipe(renew([
29+
{ search: 'oldText', replacement: 'newText' },
30+
{ search: /oldPattern/g, replacement: 'newPattern' }
31+
]))
32+
.pipe(gulp.dest('dist'));
33+
});
34+
35+
gulp.task('html', function() {
36+
return gulp.src('src/**/*.html')
37+
.pipe(renew([
38+
[‘foo1’, 'bar1'],
39+
[/foo2/g, 'bar2'],
40+
[‘foo3’, 'bar3']
41+
]))
42+
.pipe(gulp.dest('dist'));
43+
});
44+
```
45+
46+
## API
47+
48+
```js
49+
renew(replacements[, options])
50+
```
51+
52+
### replacements
53+
54+
Contains the rules for replacement. Each rule is an object containing the search and replacement properties.
55+
`type`: `Array<{ search: string | RegExp, replacement: string | Function }>`
56+
57+
- `search`:The string or regular expression to search for.
58+
- `replacement`:The string or function to replace with.
59+
60+
### options
61+
62+
Options.
63+
`Type`: `Object`
64+
65+
#### `options.skipBinary`
66+
67+
Whether to skip binary files.
68+
`Type`: `boolean`
69+
`Default`: `true`
70+
71+
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.
72+
73+
**示例:**
74+
75+
- String replacement:
76+
77+
```js
78+
# Writing style 1
79+
renew([
80+
{ search: 'foo', replacement: 'bar' }
81+
])
82+
83+
# Write style 2
84+
renew([
85+
['foo', 'bar']
86+
])
87+
88+
# Write style 3
89+
renew({ 'foo1': 'bar1', 'foo2': 'bar2' })
90+
```
91+
92+
- Regular expression replacement:
93+
94+
```js
95+
renew({ search: /oldPattern/g, replacement: 'newPattern' })
96+
```
97+
98+
- Function replacement:
99+
100+
```js
101+
renew([
102+
{
103+
search: 'oldText',
104+
replacement: function (match) {
105+
return match.toUpperCase()
106+
},
107+
},
108+
{
109+
search: /foo(.{3})/g,
110+
replacement: function (match, p1, offset, string) {
111+
return 'bar' + p1
112+
},
113+
},
114+
])
115+
```

plugins/renew/README.zh_CN.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# gulp-renew
2+
3+
支持字符串批量替换插件。
4+
替换规则支持正则表达式和函数替换。
5+
6+
> Based on [gulp-replace](https://www.npmjs.com/package/gulp-replace)
7+
8+
## Installation
9+
10+
```
11+
npm install --save-dev gulp-renew
12+
```
13+
14+
## Usage
15+
16+
```js
17+
const gulp = require('gulp');
18+
const renew = require('gulp-renew');
19+
20+
gulp.task('default', function() {
21+
return gulp.src('src/**/*.js')
22+
.pipe(renew([
23+
{ search: 'oldText', replacement: 'newText' },
24+
{ search: /oldPattern/g, replacement: 'newPattern' }
25+
]))
26+
.pipe(gulp.dest('dist'));
27+
});
28+
29+
gulp.task('html', function() {
30+
return gulp.src('src/**/*.html')
31+
.pipe(renew([
32+
[‘foo1’, 'bar1'],
33+
[/foo2/g, 'bar2'],
34+
[‘foo3’, 'bar3']
35+
]))
36+
.pipe(gulp.dest('dist'));
37+
});
38+
```
39+
40+
## API
41+
42+
```js
43+
renew(replacements[, options])
44+
```
45+
46+
### replacements
47+
48+
包含要进行替换的规则。每个规则是一个对象,包含 search 和 replacement 属性
49+
`type`: `Array<{ search: string | RegExp, replacement: string | Function }>`
50+
51+
- `search`:要搜索的字符串或正则表达式。
52+
- `replacement`:要替换的字符串或函数。
53+
54+
### options
55+
56+
选项
57+
`Type`: `Object`
58+
59+
#### `options.skipBinary`
60+
61+
是否跳过二进制文件。
62+
`Type`: `boolean`
63+
`Default`: `true`
64+
65+
如果 `skipBinary` 选项设置为 `true`,插件将不会处理二进制文件。
66+
67+
**示例:**
68+
69+
- 字符串替换:
70+
71+
```js
72+
# 写法 1
73+
renew([
74+
{ search: 'foo', replacement: 'bar' }
75+
])
76+
77+
# 写法 2
78+
renew([
79+
['foo', 'bar']
80+
])
81+
82+
# 写法 3
83+
renew({ 'foo1': 'bar1', 'foo2': 'bar2' })
84+
```
85+
86+
- 正则表达式替换
87+
88+
```js
89+
renew({ search: /oldPattern/g, replacement: 'newPattern' })
90+
```
91+
92+
- 函数替换
93+
94+
```js
95+
renew([
96+
{
97+
search: 'oldText',
98+
replacement: function (match) {
99+
return match.toUpperCase()
100+
},
101+
},
102+
{
103+
search: /foo(.{3})/g,
104+
replacement: function (match, p1, offset, string) {
105+
return 'bar' + p1
106+
},
107+
},
108+
])
109+
```

0 commit comments

Comments
 (0)