处理vue3 ::v-deep 转换 ::v-deep 全自动化node脚本

90 阅读1分钟

`

const fs = require('fs'); const path = require('path');

// 定义要搜索和替换的文件路径 const directoryPath = path.join(__dirname, 'src'); // 假设你的V 文件位于src/components目录下

function fileIsDirectory(filePath) { let rej, res; let p = new Promise((resolve, reject) => { res = resolve rej = reject }); fs.stat(filePath, (e, status) => { res(status.isFile()) }) return p }

async function readFileFolder(filePath) { let isDirectory = await fileIsDirectory(filePath); if (isDirectory) { readFile(filePath); return; } fs.readdir(filePath, (err, files) => { files.forEach(async (f) => { const dirPath = path.join(filePath, f) isDirectory = await fileIsDirectory(dirPath); if (isDirectory) { readFile(dirPath); } else { readFileFolder(dirPath) } }) }) }

function readFile(p) { const fileExtension = path.extname(p.toLowerCase()); if (fileExtension === '.vue') { fs.readFile(p, 'utf8', (err, data) => { let file_data = data.replace(/::v-deep\s.[\w-]+(\s*)?/g, match => { return '::v-deep(' + match.trim().replace('::v-deep ', '') + ')' }) fs.writeFile(p, file_data, 'utf-8', (e) => { console.log(处理完成 文件夹名为: ${p}) }) }) } }

readFileFolder(directoryPath) `