Skip to content

Commit b9127cd

Browse files
committed
feat: add parse options func
1 parent 5b0d508 commit b9127cd

File tree

3 files changed

+88
-51
lines changed

3 files changed

+88
-51
lines changed

src/index.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { OPTIONS_TYPES } from './utils/constance'
2-
import { parseOptions, getPathsAsync, getPathsSync, replaceFactory } from './utils/index'
3-
import * as fs from 'fs';
2+
import { parseOptions, getPathsAsync, getPathsSync, replaceFileAsync, replaceFileSync } from './utils/index'
3+
44
/**
55
* Async main
66
*/
@@ -40,41 +40,3 @@ export const replaceStringInFilesSync = (options: OPTIONS_TYPES) => {
4040
if (!Array.isArray(paths)) return replaceFileSync(paths, options)
4141
return paths.map((path) => replaceFileSync(path, options))
4242
}
43-
44-
/**
45-
* async replace string in single file
46-
*/
47-
const replaceFileAsync = (file: string, options: OPTIONS_TYPES) => {
48-
const { from, to, encoding, freeze, countMatches } = options
49-
50-
return new Promise((resolve, reject) => {
51-
fs.readFile(file, { encoding }, (error, contents) => {
52-
if (error) return reject(error)
53-
54-
// replace action
55-
const { result, newContents } = replaceFactory(contents, from, to, file, countMatches)
56-
57-
if (!result.changed || freeze) return resolve(result)
58-
59-
// write action
60-
fs.writeFile(file, newContents, encoding, error => {
61-
if (error) return reject(error)
62-
resolve(result)
63-
})
64-
})
65-
})
66-
}
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/constance.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
export type Strings = string | string[]
22

3+
export type From = string | RegExp | FromCallback
4+
5+
export type To = string | ToCallback
6+
7+
export type FromCallback = (file: string) => string | RegExp | string[]
8+
9+
export type ToCallback = (match: string, file: string) => string | string[]
10+
311
export interface OPTIONS_TYPES {
4-
files: Strings;
5-
from: string | RegExp;
6-
to: string;
7-
freeze?: boolean;
8-
ignore?: string[];
9-
disableGlobs?: boolean;
10-
glob?: object;
11-
encoding?: BufferEncoding;
12-
countMatches?: boolean;
12+
files: Strings
13+
from: From
14+
to: To
15+
freeze?: boolean
16+
ignore?: string[]
17+
disableGlobs?: boolean
18+
glob?: object // fast-glob config info
19+
encoding?: BufferEncoding
20+
countMatches?: boolean
1321
}

src/utils/index.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
import { OPTIONS_TYPES, Strings } from './constance'
22
import * as fg from 'fast-glob';
3+
import * as fs from 'fs';
4+
35

46
/**
57
* init parse options
68
*/
7-
export const parseOptions = (options: OPTIONS_TYPES) => {
9+
export const parseOptions = (options: OPTIONS_TYPES): OPTIONS_TYPES => {
10+
const defaults = {
11+
ignore: [],
12+
encoding: 'utf-8',
13+
disableGlobs: false,
14+
countMatches: false,
15+
freeze: false,
16+
glob: {},
17+
};
18+
if (typeof options !== 'object' || options === null) {
19+
throw new Error('options type error');
20+
}
21+
22+
options.glob = options.glob ?? {};
23+
24+
const { files, from, to, ignore, encoding } = options;
25+
if (typeof files === 'undefined') throw new Error('files type error')
26+
27+
if (typeof from === 'undefined') throw new Error('from type error')
28+
29+
if (typeof to === 'undefined') throw new Error('to type error')
30+
31+
if (!Array.isArray(files)) options.files = [files]
32+
33+
if (!Array.isArray(ignore)) options.ignore = ignore ? [] : [ignore]
34+
835
if (!options.encoding) options.encoding = 'utf-8'
9-
return options
36+
37+
return Object.assign({}, defaults, options);
1038
}
1139

1240

@@ -90,3 +118,42 @@ export const replaceFactory = (contents, from, to, file, count: boolean) => {
90118
newContents
91119
}
92120
}
121+
122+
123+
/**
124+
* async replace string in single file
125+
*/
126+
export const replaceFileAsync = (file: string, options: OPTIONS_TYPES) => {
127+
const { from, to, encoding, freeze, countMatches } = options
128+
129+
return new Promise((resolve, reject) => {
130+
fs.readFile(file, { encoding }, (error, contents) => {
131+
if (error) return reject(error)
132+
133+
// replace action
134+
const { result, newContents } = replaceFactory(contents, from, to, file, countMatches)
135+
136+
if (!result.changed || freeze) return resolve(result)
137+
138+
// write action
139+
fs.writeFile(file, newContents, encoding, error => {
140+
if (error) return reject(error)
141+
resolve(result)
142+
})
143+
})
144+
})
145+
}
146+
147+
/**
148+
* sync replace string in single file
149+
*/
150+
export const replaceFileSync = (file: string, options: OPTIONS_TYPES) => {
151+
const { from, to, encoding, freeze, countMatches } = options
152+
const contents = fs.readFileSync(file, encoding);
153+
154+
const { result, newContents } = replaceFactory(contents, from, to, file, countMatches)
155+
156+
if (!result.changed || freeze) return result
157+
158+
fs.writeFileSync(file, newContents, encoding)
159+
}

0 commit comments

Comments
 (0)