replaceEnv.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import * as fs from 'fs'
  2. import path from 'path'
  3. import getAppInfo from "./config/id.config.mjs";
  4. // 同步读取文件内容
  5. function readFileSync(filePath) {
  6. return fs.readFileSync(filePath, 'utf8');
  7. }
  8. // 同步写入文件内容
  9. function writeFileSync(filePath, data) {
  10. fs.writeFileSync(filePath, data, 'utf8');
  11. }
  12. // 替换文件中的文本
  13. export function replaceTextInFileSync(filePath, matchKey, str, templateFilePath) {
  14. return replaceCustomTextInFileSync({
  15. filePath,
  16. templateFilePath,
  17. replaceFn: function (fileContent) {
  18. return fileContent.replaceAll(matchKey, str)
  19. }
  20. })
  21. }
  22. // 自定义替换文件中的文本
  23. function replaceCustomTextInFileSync({ filePath, templateFilePath, replaceFn }) {
  24. try {
  25. // 读取文件内容
  26. let fileContent = readFileSync(templateFilePath || filePath);
  27. // 替换文本
  28. const newContent = replaceFn(fileContent);
  29. // 写入新内容到文件
  30. writeFileSync(filePath, newContent);
  31. console.log('环境文本替换:', filePath);
  32. } catch (error) {
  33. console.error('文本替换失败:', error);
  34. }
  35. }
  36. function replaceStyle (color, bgColor) {
  37. replaceCustomTextInFileSync({
  38. filePath: path.resolve('./src/assets/env_style/scss.scss'),
  39. templateFilePath: path.resolve('./script/template/env_style/scss.scss'),
  40. replaceFn: (content) => {
  41. let result = content.replaceAll('#2ABED1', color)
  42. result = result.replaceAll('#185DE4', bgColor)
  43. return result
  44. }
  45. })
  46. replaceTextInFileSync(path.resolve('./src/assets/env_style/less.less'), '#2ABED1', color, path.resolve('./script/template/env_style/less.less'))
  47. }
  48. function replaceAppConfig (data) {
  49. replaceTextInFileSync(path.resolve('./project-config/index.js'), 'const appConfig = {}', `const appConfig = ${JSON.stringify(data)}`, path.resolve('./script/template/project-config.js'))
  50. replaceTextInFileSync(path.resolve('./project-config/index.common.js'), 'const appConfig = {}', `const appConfig = ${JSON.stringify(data)}`, path.resolve('./script/template/project-config.common.js'))
  51. }
  52. function replaceAppID (data) {
  53. replaceCustomTextInFileSync({
  54. filePath: path.resolve('./project.config.json'),
  55. templateFilePath: path.resolve('./script/template/project.config.json'),
  56. replaceFn: (content) => {
  57. let result = content.replaceAll('APP_ID', data.projectInfo.appId)
  58. result = result.replaceAll('APP_CODE', data.projectInfo.appCode)
  59. return result
  60. }
  61. })
  62. }
  63. export function replaceEnvCode (appCode) {
  64. const appInfo = getAppInfo(appCode)
  65. // 替换AppId、Tabbar、图标、全部商机列表配置
  66. replaceAppID(appInfo)
  67. replaceAppConfig(appInfo)
  68. // 替换主题色
  69. replaceStyle(appInfo.projectInfo.themeColor, appInfo.projectInfo.themeBgColor)
  70. // 替换请求域名、资源域名
  71. }