const fs = require('node:fs') const srcs = fs.readdirSync('../src/') const babelParser = require('@babel/parser') // console.log(srcs) const root = '../src' // srcs.forEach(file => { // // if(fs.statSync(`${root}/${file}`).isDirectory()) { // // } // if(/\.vue$/.test(file)) { // console.log(file) // } // }) function getFiles(path) { const srcs = fs.readdirSync(path) let paths = [] srcs.forEach(file => { const new_path = `${path}/${file}` if (fs.statSync(new_path).isDirectory()) { paths = paths.concat(getFiles(new_path)) } if (/\.vue$/.test(new_path)) { paths.push(new_path) } }) return paths } function uni2vueBase(file_path) { // const file = fs.readFileSync('../src/pages/automatic/automaticMy.vue').toString() const file = fs.readFileSync(file_path).toString() let isAdd = false let isImage = false let newFileTxt = '' file.split('\n').forEach((elm, index) => { let line = `${elm}` // uni.navigateTo --> this.$router.push if (line.indexOf('uni.navigateTo(') > -1) { isAdd = true line = line.replace(/uni\.navigateTo/g, 'this.$router.push') } if (isAdd && line.indexOf('url') > -1) { line = line.replace(/url/g, 'path') } if (line.indexOf(')') > -1) { isAdd = false } // rpx --> px if (line.indexOf('rpx') > -1) { line = line.replace(/([0-9]*)rpx/g, '$1px') } // view --> div if (line.indexOf(' -1) { line = line.replace(/ -1) { line = line.replace(/<\/view/g, ' span if (line.indexOf(' -1) { line = line.replace(/ -1) { line = line.replace(/<\/text/g, ' -1) { line = line.replace(/<\/image/g, '/.test(line)) { isImage = false } if (/\simage\s{/.test(line)) { line = line.replace(/image/g, 'img') } newFileTxt += `${line}\n` }) // fs.writeFileSync(file_path.replace(/\.vue/g, '2.vue'), newFileTxt) fs.writeFileSync(file_path, newFileTxt) } function uniReplace2vue(file_path) { const file = fs.readFileSync(file_path).toString() let isAdd = false let newFileTxt = '' file.split('\n').forEach((elm, index) => { let line = `${elm}` // uni.navigateTo --> this.$router.push if (line.indexOf('this.$router.uni.redirectTo') > -1) { isAdd = true line = line.replace(/this\.\$router\.uni\.redirectTo/g, 'this.$router.replace') } if (isAdd && line.indexOf('url') > -1) { line = line.replace(/url/g, 'path') } if (line.indexOf(')') > -1) { isAdd = false } newFileTxt += `${line}\n` }) fs.writeFileSync(file_path, newFileTxt) } function moveLine(file_path) { let file = fs.readFileSync(file_path).toString() let newFileTxt = '' if(/\n\n\n/g.test(file)) { newFileTxt = file.replace(/\n\n\n/g, '\n'); } fs.writeFileSync(file_path, newFileTxt) } function setVue() { const files = getFiles(root) files.forEach(_filePath => { if (_filePath.indexOf('src/pages/parkingFee') > -1 || true) { // 基础转化 uni2vueBase(_filePath) // 重定向 uniReplace2vue(_filePath) // 删除多余的空行 // moveLine(_filePath) } }) } setVue()