selfCloseInputTag.js 968 B

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * @file: selfCloseInputTag.js
  3. * @desc: 遍历指定目录下 .ux 文件,将其中 input 标签由 <input **></input> 转换为 <input ** />
  4. * @date: 2019-01-23
  5. */
  6. const fs = require('fs')
  7. const path = require('path')
  8. const quickappCodePath = './src/'
  9. const main = codePath => {
  10. const traversing = cpath => {
  11. const files = fs.readdirSync(cpath)
  12. files.forEach(fileName => {
  13. const fPath = path.join(cpath, fileName)
  14. const stats = fs.statSync(fPath)
  15. stats.isDirectory() && traversing(fPath)
  16. stats.isFile() && fPath.endsWith('.ux') && matchAndReplace(fPath)
  17. })
  18. }
  19. traversing(codePath)
  20. }
  21. const matchAndReplace = path => {
  22. const pageContent = fs.readFileSync(path, 'UTF-8')
  23. const newContent = pageContent.replace(
  24. /(<)([\s]*?)(input\b[^\/]*?)>[\s\S]*?<\/input>/gm,
  25. '$1$3 />'
  26. )
  27. fs.writeFile(path, newContent, error => {
  28. if (error) throw `Something went wrong: ${error}`
  29. })
  30. }
  31. main(quickappCodePath)