(filePath: string, replaces: Array<string[]>)
| 26 | } |
| 27 | |
| 28 | const modifyFile = (filePath: string, replaces: Array<string[]>) => { |
| 29 | // 1. 读取文件内容 |
| 30 | const oFilePath = filePath + '.txt' |
| 31 | const data = fs.readFileSync(oFilePath, 'utf8'); |
| 32 | |
| 33 | // 2. 修改文件内容 |
| 34 | let modifiedData = data |
| 35 | replaces.forEach(([o, n]) => { |
| 36 | modifiedData = modifiedData.replace(new RegExp(`{{${o}}}`, 'g'), n) |
| 37 | }) |
| 38 | // 3. 将修改后的内容写回文件 |
| 39 | fs.writeFileSync(filePath, modifiedData, 'utf8'); |
| 40 | fs.unlinkSync(oFilePath) |
| 41 | } |
| 42 | |
| 43 | ;(async () => { |
| 44 | const { name } = await inquirer.prompt([ |