Skip to content

Commit 5b0d508

Browse files
committed
feat: add sync replace
1 parent 499270c commit 5b0d508

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { OPTIONS_TYPES } from './utils/constance'
2-
import { parseOptions, getPathsAsync, replaceFactory } from './utils/index'
2+
import { parseOptions, getPathsAsync, getPathsSync, replaceFactory } from './utils/index'
33
import * as fs from 'fs';
44
/**
55
* Async main
66
*/
7-
const replaceStringInFiles = (options: OPTIONS_TYPES) => {
7+
export const replaceStringInFiles = (options: OPTIONS_TYPES) => {
88
try {
99
options = parseOptions(options)
1010
} catch (error) {
1111
return Promise.reject(error)
1212
}
1313

14-
const { files, from, to, freeze } = options
14+
const { files, freeze } = options
1515

1616
// freeze mode, do not replace
1717
if (freeze) {
@@ -27,12 +27,22 @@ const replaceStringInFiles = (options: OPTIONS_TYPES) => {
2727
/**
2828
* Sync replaceStringInFiles
2929
*/
30-
const replaceStringInFilesSync = (options: OPTIONS_TYPES) => {
30+
export const replaceStringInFilesSync = (options: OPTIONS_TYPES) => {
31+
options = parseOptions(options)
3132

33+
const { files, freeze } = options
34+
const paths = getPathsSync(files, options);
35+
36+
// freeze mode, do not replace
37+
if (freeze) {
38+
console.warn('running in freeze mode, no change...')
39+
}
40+
if (!Array.isArray(paths)) return replaceFileSync(paths, options)
41+
return paths.map((path) => replaceFileSync(path, options))
3242
}
3343

3444
/**
35-
* replace string in single file
45+
* async replace string in single file
3646
*/
3747
const replaceFileAsync = (file: string, options: OPTIONS_TYPES) => {
3848
const { from, to, encoding, freeze, countMatches } = options
@@ -54,3 +64,17 @@ const replaceFileAsync = (file: string, options: OPTIONS_TYPES) => {
5464
})
5565
})
5666
}
67+
68+
/**
69+
* sync replace string in single file
70+
*/
71+
const replaceFileSync = (file: string, options: OPTIONS_TYPES) => {
72+
const { from, to, encoding, freeze, countMatches } = options
73+
const contents = fs.readFileSync(file, encoding);
74+
75+
const { result, newContents } = replaceFactory(contents, from, to, file, countMatches)
76+
77+
if (!result.changed || freeze) return result
78+
79+
fs.writeFileSync(file, newContents, encoding)
80+
}

src/utils/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const parseOptions = (options: OPTIONS_TYPES) => {
1111

1212

1313
/**
14-
* use fast-glob to get all files
14+
* async use fast-glob to get all files
1515
*/
1616
export const getPathsAsync = (patterns: Strings, options: OPTIONS_TYPES) => {
1717
const { ignore, disableGlobs, glob: cfg } = options
@@ -24,14 +24,36 @@ export const getPathsAsync = (patterns: Strings, options: OPTIONS_TYPES) => {
2424

2525

2626

27+
/**
28+
* sync use fast-glob to get all files
29+
*/
30+
export const getPathsSync = (patterns: Strings, options: OPTIONS_TYPES) => {
31+
const { ignore, disableGlobs, glob: cfg } = options
32+
33+
// disable globs, just ensure file(s) name
34+
if (disableGlobs) return patterns
35+
36+
return globFilesSync(patterns, ignore, cfg)
37+
}
38+
39+
40+
2741
/**
2842
* use fast-glob to glob file(s)
2943
*/
3044
export const globFilesAsync = (patterns: Strings, ignore: string[], cfg) => {
31-
cfg = Object.assign({ ignore, nodir: true, dot: true }, cfg)
45+
cfg = Object.assign({ ignore, nodir: true }, cfg)
3246
return fg(patterns, cfg)
3347
}
3448

49+
/**
50+
* sync use fast-glob to glob file(s)
51+
*/
52+
export const globFilesSync = (patterns: Strings, ignore: string[], cfg) => {
53+
cfg = Object.assign({ ignore, nodir: true }, cfg)
54+
return fg.sync(patterns, cfg)
55+
}
56+
3557
/**
3658
* replace main
3759
*/

0 commit comments

Comments
 (0)