index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const fs = require('node:fs')
  2. const srcs = fs.readdirSync('../src/')
  3. const babelParser = require('@babel/parser')
  4. // console.log(srcs)
  5. const root = '../src'
  6. // srcs.forEach(file => {
  7. //
  8. // if(fs.statSync(`${root}/${file}`).isDirectory()) {
  9. //
  10. // }
  11. // if(/\.vue$/.test(file)) {
  12. // console.log(file)
  13. // }
  14. // })
  15. function getFiles(path) {
  16. const srcs = fs.readdirSync(path)
  17. let paths = []
  18. srcs.forEach(file => {
  19. const new_path = `${path}/${file}`
  20. if (fs.statSync(new_path).isDirectory()) {
  21. paths = paths.concat(getFiles(new_path))
  22. }
  23. if (/\.vue$/.test(new_path)) {
  24. paths.push(new_path)
  25. }
  26. })
  27. return paths
  28. }
  29. function uni2vueBase(file_path) {
  30. // const file = fs.readFileSync('../src/pages/automatic/automaticMy.vue').toString()
  31. const file = fs.readFileSync(file_path).toString()
  32. let isAdd = false
  33. let isImage = false
  34. let newFileTxt = ''
  35. file.split('\n').forEach((elm, index) => {
  36. let line = `${elm}`
  37. // uni.navigateTo --> this.$router.push
  38. if (line.indexOf('uni.navigateTo(') > -1) {
  39. isAdd = true
  40. line = line.replace(/uni\.navigateTo/g, 'this.$router.push')
  41. }
  42. if (isAdd && line.indexOf('url') > -1) {
  43. line = line.replace(/url/g, 'path')
  44. }
  45. if (line.indexOf(')') > -1) {
  46. isAdd = false
  47. }
  48. // rpx --> px
  49. if (line.indexOf('rpx') > -1) {
  50. line = line.replace(/([0-9]*)rpx/g, '$1px')
  51. }
  52. // view --> div
  53. if (line.indexOf('<view') > -1) {
  54. line = line.replace(/<view/g, '<div')
  55. }
  56. if (line.indexOf('</view') > -1) {
  57. line = line.replace(/<\/view/g, '</div')
  58. }
  59. if (/\sview\s{/.test(line)) {
  60. line = line.replace(/view/g, 'div')
  61. }
  62. // text --> span
  63. if (line.indexOf('<text') > -1) {
  64. line = line.replace(/<text/g, '<span')
  65. }
  66. if (line.indexOf('</text') > -1) {
  67. line = line.replace(/<\/text/g, '</span')
  68. }
  69. if (/\stext\s{/.test(line)) {
  70. line = line.replace(/text/g, 'span')
  71. }
  72. // 处理图片
  73. if (/<image/g.test(line)) {
  74. line = line.replace(/<image/g, '<img')
  75. isImage = true
  76. }
  77. if(isImage) {
  78. // 处理 src 内容
  79. // line = line.replace(/src="(.*)/g, '<image')
  80. }
  81. if (line.indexOf('</image') > -1) {
  82. line = line.replace(/<\/image/g, '</img')
  83. }
  84. if(/\/()>/.test(line)) {
  85. isImage = false
  86. }
  87. if (/\simage\s{/.test(line)) {
  88. line = line.replace(/image/g, 'img')
  89. }
  90. newFileTxt += `${line}\n`
  91. })
  92. // fs.writeFileSync(file_path.replace(/\.vue/g, '2.vue'), newFileTxt)
  93. fs.writeFileSync(file_path, newFileTxt)
  94. }
  95. function uniReplace2vue(file_path) {
  96. const file = fs.readFileSync(file_path).toString()
  97. let isAdd = false
  98. let newFileTxt = ''
  99. file.split('\n').forEach((elm, index) => {
  100. let line = `${elm}`
  101. // uni.navigateTo --> this.$router.push
  102. if (line.indexOf('this.$router.uni.redirectTo') > -1) {
  103. isAdd = true
  104. line = line.replace(/this\.\$router\.uni\.redirectTo/g, 'this.$router.replace')
  105. }
  106. if (isAdd && line.indexOf('url') > -1) {
  107. line = line.replace(/url/g, 'path')
  108. }
  109. if (line.indexOf(')') > -1) {
  110. isAdd = false
  111. }
  112. newFileTxt += `${line}\n`
  113. })
  114. fs.writeFileSync(file_path, newFileTxt)
  115. }
  116. function moveLine(file_path) {
  117. let file = fs.readFileSync(file_path).toString()
  118. let newFileTxt = ''
  119. if(/\n\n\n/g.test(file)) {
  120. newFileTxt = file.replace(/\n\n\n/g, '\n');
  121. }
  122. fs.writeFileSync(file_path, newFileTxt)
  123. }
  124. function setVue() {
  125. const files = getFiles(root)
  126. files.forEach(_filePath => {
  127. if (_filePath.indexOf('src/pages/parkingFee') > -1 || true) {
  128. // 基础转化
  129. uni2vueBase(_filePath)
  130. // 重定向
  131. uniReplace2vue(_filePath)
  132. // 删除多余的空行
  133. // moveLine(_filePath)
  134. }
  135. })
  136. }
  137. setVue()