(patterns: { reg: string | RegExp, text: string }[], filepath: string)
| 278 | } |
| 279 | |
| 280 | static async replaceInFile(patterns: { reg: string | RegExp, text: string }[], filepath: string) { |
| 281 | filepath = this.replaceEnvVariables(filepath); |
| 282 | if (!fs.existsSync(filepath)) { |
| 283 | console.log(`While replace template content, file ${filepath}`); |
| 284 | return; |
| 285 | } |
| 286 | // console.log(`replace ${filepath} with ${JSON.stringify(patterns)}`); |
| 287 | const lines = (await fs.readFile(filepath)).toString('utf8').split('\n'); |
| 288 | |
| 289 | const newContent = lines.map((l) => { |
| 290 | patterns.forEach((p) => { |
| 291 | l = l.replace(new RegExp(p.reg), this.replaceEnvVariables(p.text)); |
| 292 | }); |
| 293 | return l; |
| 294 | }).join('\n'); |
| 295 | |
| 296 | await fs.writeFile(filepath, newContent); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | static exactValueFromFile(regexp: RegExp, filename: string, idx: number): string | undefined { |
no test coverage detected