|
1 | 1 | import { OPTIONS_TYPES, Strings } from './constance' |
2 | 2 | import * as fg from 'fast-glob'; |
| 3 | +import * as fs from 'fs'; |
| 4 | + |
3 | 5 |
|
4 | 6 | /** |
5 | 7 | * init parse options |
6 | 8 | */ |
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 | + |
8 | 35 | if (!options.encoding) options.encoding = 'utf-8' |
9 | | - return options |
| 36 | + |
| 37 | + return Object.assign({}, defaults, options); |
10 | 38 | } |
11 | 39 |
|
12 | 40 |
|
@@ -90,3 +118,42 @@ export const replaceFactory = (contents, from, to, file, count: boolean) => { |
90 | 118 | newContents |
91 | 119 | } |
92 | 120 | } |
| 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