Insert or replace a single line in a config file (creating it if needed).
(file: string, matcher: RegExp, line: string)
| 192 | |
| 193 | /** Insert or replace a single line in a config file (creating it if needed). */ |
| 194 | private async upsertLine(file: string, matcher: RegExp, line: string): Promise<void> { |
| 195 | let body = ''; |
| 196 | try { body = await fs.readFile(file, 'utf8'); } catch { /* new file */ } |
| 197 | if (matcher.test(body)) body = body.replace(matcher, line); |
| 198 | else body = (body && !body.endsWith('\n') ? body + '\n' : body) + line + '\n'; |
| 199 | await fs.writeFile(file, body); |
| 200 | } |
| 201 | } |