12345678910111213141516171819202122232425262728293031323334353637 |
- const fs = require('fs');
- const path = require('path');
- // 同步读取文件内容
- function readFileSync(filePath) {
- return fs.readFileSync(filePath, 'utf8');
- }
- // 同步写入文件内容
- function writeFileSync(filePath, data) {
- fs.writeFileSync(filePath, data, 'utf8');
- }
- // 替换文件中的文本
- function replaceTextInFileSync(filePath, matchKey, str) {
- try {
- // 读取文件内容
- let fileContent = readFileSync(filePath);
- // 替换文本
- const newContent = fileContent.replace(matchKey, str);
- // 写入新内容到文件
- writeFileSync(filePath, newContent);
- console.log('文本替换完成。');
- } catch (error) {
- console.error('文本替换失败:', error);
- }
- }
- // 使用示例
- const filePath = path.resolve('./src/assets/style/_variables.scss'); // 替换为你的文件路径
- const matchKey = '$main: #2ABED1;'; // 替换为需要查找并替换的文本
- const str = '$main: #111;'; // 替换为新的文本
- // replaceTextInFileSync(filePath, matchKey, str);
|