addVersion.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. /* eslint-disable */
  2. var fs = require('fs')
  3. var path = require('path')
  4. var config = {
  5. projectPath: path.resolve(__dirname),
  6. filePath: '/datareport/',
  7. templateName: 'index.html',
  8. urlLocal: '/jyapp/local',
  9. output: [
  10. {
  11. origin: 'res://app-i1.jianyu360.com',
  12. fileName: 'index_i1.html'
  13. },
  14. {
  15. origin: 'res://app2-jytest.jianyu360.cn',
  16. fileName: 'index_app2.html'
  17. }
  18. ]
  19. }
  20. fs.readFile(`${config.projectPath}${config.filePath}${config.templateName}`, 'utf8', function(err, files) {
  21. config.output.forEach(function (item, index) {
  22. let replaced = files.replace(new RegExp(`${config.filePath}`, 'g'), `${item.origin}${config.filePath}`)
  23. // 在文件中部分 标签 前加换行,避免被正则贪婪匹配全部匹配到
  24. replaced = replaced.replace(/(<[a-z])/g, `\n$1`)
  25. replaced = replaced.replace(/script(.*?)src=\/(.*)\/(.*).min.js/g, `script$1src=${item.origin}${config.urlLocal}/$3.min.js`)
  26. // 将前面添加的换行去掉
  27. replaced = replaced.replace(/\n/g, '')
  28. fs.writeFileSync(`${config.projectPath}${config.filePath}${item.fileName}`, replaced, 'utf8', function (err) {
  29. if (err) return console.log(err)
  30. })
  31. })
  32. })