Skip to content

Commit 580c308

Browse files
committed
fix: replace fix && add example
1 parent 787d131 commit 580c308

File tree

10 files changed

+194
-19
lines changed

10 files changed

+194
-19
lines changed

README.md

Lines changed: 132 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
<h2>tiny-replace-files</h2>
55
<br>
66
<blockquote>Like vscode, simple utility to quickly replace text in one or more files.</blockquote>
7-
<img alt="npm" src="https://img.shields.io/npm/dw/test">
8-
<img alt="Travis (.org)" src="https://img.shields.io/travis/rust-lang/rust">
9-
<img alt="npm" src="https://img.shields.io/npm/v/test">
10-
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors/npm-template/js-npm-template">
11-
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/test">
12-
<img alt="node-current" src="https://img.shields.io/node/v/test">
13-
<img alt="GitHub" src="https://img.shields.io/github/license/npm-template/js-npm-template">
14-
<img alt="GitHub issues" src="https://img.shields.io/github/issues-raw/npm-template/js-npm-template">
157
</div>
168

179
Scene: generate a templates, then replace initName to newName.
@@ -32,10 +24,10 @@ pnpm install --save tiny-replace-files
3224

3325
### usage
3426

35-
**Sync**
27+
#### Sync
3628

3729
```ts
38-
import { replaceSync } from 'tiny-replace-files'
30+
import { replaceStringInFiles } from 'tiny-replace-files'
3931

4032
const options = {
4133
files: 'src/targets/index.js',
@@ -44,11 +36,20 @@ const options = {
4436
}
4537

4638
# await
47-
const result = replaceSync(options)
39+
const result = replaceStringInFiles(options)
4840
console.info(result)
41+
/**
42+
[
43+
{
44+
file: './ques2.md',
45+
changed: true,
46+
matchCounts: 1,
47+
replaceCounts: 1
48+
}
49+
]
50+
*/
4951
```
50-
51-
**Async**
52+
#### Async
5253

5354
```ts
5455
import replaceStringInFiles from 'tiny-replace-files'
@@ -71,6 +72,122 @@ replaceStringInFiles(options).then((res)=>{
7172
})
7273
```
7374

75+
### Advanced usage
76+
77+
#### Replace a single file or glob
78+
79+
```ts
80+
const options = {
81+
files: 'file',
82+
};
83+
```
84+
85+
#### Replace multiple files or globs
86+
87+
```ts
88+
const options = {
89+
files: [
90+
'file1',
91+
'file2',
92+
'file3',
93+
],
94+
};
95+
96+
const options = {
97+
files: [
98+
'file1',
99+
'file2/**',
100+
]
101+
};
102+
```
103+
104+
#### Replace by regex
105+
106+
```ts
107+
const options = {
108+
from: /foo/g,
109+
to: 'bar',
110+
};
111+
```
112+
113+
#### `from` callback
114+
115+
```ts
116+
const options = {
117+
files: 'file',
118+
from: (file) => new RegExp(file, 'g'),
119+
to: 'bar',
120+
};
121+
```
122+
123+
#### `to` callback
124+
125+
```ts
126+
const options = {
127+
files: './ques2.md',
128+
from: 'quest 2',
129+
to: (match: string) => match.toUpperCase(),
130+
countMatches: true
131+
}
132+
```
133+
134+
#### Ignore file(s) or glob
135+
136+
```ts
137+
const options = {
138+
ignore: './ignored/file',
139+
};
140+
141+
const options = {
142+
ignore: [
143+
'path/**',
144+
'path2/index.html',
145+
],
146+
};
147+
```
148+
149+
#### Disable globs
150+
151+
```ts
152+
const options = {
153+
disableGlobs: true,
154+
};
155+
```
156+
157+
#### `glob` configuration
158+
159+
API passed to the [fast-glob](https://github.com/mrmlnc/fast-glob#api):
160+
161+
```ts
162+
const options = {
163+
glob: {
164+
//Glob settings here
165+
dot: true, //E.g. to include file names starting with a dot
166+
},
167+
};
168+
```
169+
170+
#### Character encoding
171+
172+
Use a different character encoding for reading/writing files. Defaults to utf-8.
173+
174+
```ts
175+
const options = {
176+
encoding: 'utf8',
177+
};
178+
```
179+
180+
#### `freeze` Run
181+
182+
freeze mode will do not replace & change, just run the process.
183+
184+
```ts
185+
const options = {
186+
freeze: true,
187+
};
188+
```
189+
190+
74191
## ⚙️ Changelog
75192

76193
See [CHANGELOG](./CHANGELOG.md).
@@ -84,6 +201,7 @@ See [CHANGELOG](./CHANGELOG.md).
84201
- [x] init lib
85202
- [x] 完成 replaceStringInFiles 函数开发
86203
- [x] 完成同步函数开发
87-
- [ ] 完成测试
204+
- [x] 完成测试
88205
- [ ] 生命周期??
206+
- [ ] cli???
89207

example/files/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { replaceStringInFiles, replaceStringInFilesSync } from '../../dist/index'
2+
3+
4+
const options = {
5+
files: [
6+
'./one.md',
7+
'./two.md',
8+
'./three.md',
9+
],
10+
from: 'Quest 2',
11+
to: (match) => match.toLowerCase(),
12+
countMatches: true
13+
}
14+
15+
replaceStringInFiles(options).then(res => {
16+
console.log(res)
17+
}).catch(error => {
18+
console.info(error)
19+
})

example/files/one.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
quest-2 is non-stop fun. The biggest titles and multi-player games—we have them. New ways to workout, socialize or lose track of time—we have those too. And our library keeps growing every day. Discover what’s possible on quest 2.

example/files/three.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
From multiplayer games to unique social experiences to joining up with friends at a live show or instructor-led workout, opportunities to meet and connect with others in VR are virtually everywhere you look.

example/files/two.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Think quest-2 is just for gaming? You’d be mistaken. quest 2 makes it possible to sit front row at a live concert, burn calories atop a glacier, hang out with friends all over the world, work, watch a movie and yes, even play a game.

example/singe-file/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { replaceStringInFiles, replaceStringInFilesSync } from '../../dist/index'
2+
3+
4+
const options = {
5+
files: './ques2.md',
6+
from: 'quest 2',
7+
to: (match: string) => match.toUpperCase(),
8+
countMatches: true
9+
}
10+
11+
// replaceStringInFiles(options).then(res => {
12+
// console.log(res)
13+
// }).catch(error => {
14+
// console.info(error)
15+
// })
16+
17+
18+
const result = replaceStringInFilesSync(options)
19+
console.log(result)

example/singe-file/ques2.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
QUEST 2 is non-stop fun. The biggest titles and multi-player games—we have them. New ways to workout, socialize or lose track of time—we have those too. And our library keeps growing every day. Discover what’s possible on QUEST 2.
2+
3+
4+
Think QUEST 2 is just for gaming? You’d be mistaken. QUEST 2 makes it possible to sit front row at a live concert, burn calories atop a glacier, hang out with friends all over the world, work, watch a movie and yes, even play a game.
5+
6+
7+
From multiplayer games to unique social experiences to joining up with friends at a live show or instructor-led workout, opportunities to meet and connect with others in VR are virtually everywhere you look.
8+
9+
10+
No wires means you're not stuck playing QUEST 2 at home. You’re free to take all the games, workouts, shows, experiences and more on the road with you. Which means you can go anywhere with it, and do anything in it.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"scripts": {
1212
"build-fast": "tsup src/index.ts --format cjs,esm --minify",
1313
"build": "yarn build-fast --dts",
14+
"watch": "tsup src/index.ts --watch",
1415
"test": "uvu -r sucrase/register --ignore fixture",
1516
"prepublishOnly": "yarn build-fast",
1617
"prettier": "prettier --write src/*",

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ export const replaceStringInFiles = (options: OPTIONS_TYPES) => {
1818
console.warn('running in freeze mode, no change...')
1919
}
2020

21-
return getPathsAsync(files, options).then((paths) =>
22-
Promise.all(paths.map(file => replaceFileAsync(file, options)))
23-
)
21+
return getPathsAsync(files, options).then((paths) => {
22+
return Promise.all(paths.map(file => replaceFileAsync(file, options)))
23+
})
2424
}
2525

26-
2726
/**
2827
* Sync replaceStringInFiles
2928
*/
@@ -40,3 +39,5 @@ export const replaceStringInFilesSync = (options: OPTIONS_TYPES) => {
4039
if (!Array.isArray(paths)) return replaceFileSync(paths, options)
4140
return paths.map((path) => replaceFileSync(path, options))
4241
}
42+
43+
export default replaceStringInFiles

src/utils/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const globFilesSync = (patterns: Strings, ignore: string[], cfg) => {
8686
/**
8787
* replace main
8888
*/
89-
export const replaceFactory = (contents, from, to, file, count: boolean) => {
89+
export const replaceFactory = (contents: string, from, to, file, count: boolean) => {
9090
const result: {
9191
file: any;
9292
matchCounts?: number;
@@ -100,6 +100,8 @@ export const replaceFactory = (contents, from, to, file, count: boolean) => {
100100

101101
let item = from
102102
if (typeof from === 'function') item = from(file)
103+
if (typeof item === 'string') item = new RegExp(item, 'g')
104+
103105

104106
if (count) {
105107
const matches = contents.match(item);
@@ -157,4 +159,6 @@ export const replaceFileSync = (file: string, options: OPTIONS_TYPES) => {
157159
if (!result.changed || freeze) return result
158160

159161
fs.writeFileSync(file, newContents, encoding)
162+
163+
return result
160164
}

0 commit comments

Comments
 (0)