pages-tabbar-migrate.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env node
  2. /**
  3. * pages.json 中 tabBar 使用占位符 __STATIC_ASSETS_BASE_URL__,构建前替换为真实根地址,构建后还原。
  4. * - materialize:写入备份并替换(默认按「开发/测试」解析基址;见 resolveBase)
  5. * - materialize:prod:按「生产」解析基址(与发行 `build` 子命令一致)
  6. * - restore:从备份还原并删除备份
  7. * - build:materialize(prod) → uni build → restore(finally)
  8. */
  9. import fs from 'node:fs'
  10. import path from 'node:path'
  11. import { fileURLToPath } from 'node:url'
  12. import { execSync } from 'node:child_process'
  13. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  14. const root = path.resolve(__dirname, '..')
  15. const pagesPath = path.join(root, 'src/pages.json')
  16. const bakPath = path.join(root, 'src/.pages-json.tabbar-placeholder.bak')
  17. function loadEnvFile(rel) {
  18. const p = path.join(root, rel)
  19. if (!fs.existsSync(p)) return {}
  20. const out = {}
  21. for (const line of fs.readFileSync(p, 'utf8').split('\n')) {
  22. const t = line.trim()
  23. if (!t || t.startsWith('#')) continue
  24. const eq = t.indexOf('=')
  25. if (eq === -1) continue
  26. const k = t.slice(0, eq).trim()
  27. let v = t.slice(eq + 1).trim()
  28. if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) {
  29. v = v.slice(1, -1)
  30. }
  31. out[k] = v
  32. }
  33. return out
  34. }
  35. function readConstantsFromTs() {
  36. const defPath = path.join(root, 'src/config/static-assets-defaults.ts')
  37. const raw = fs.readFileSync(defPath, 'utf8')
  38. const pick = (name) => {
  39. const m = raw.match(new RegExp(`export const ${name}\\s*=\\s*'([^']*)'`))
  40. return (m?.[1] ?? '').trim()
  41. }
  42. return {
  43. test: pick('STATIC_ASSETS_BASE_URL_TEST'),
  44. production: pick('STATIC_ASSETS_BASE_URL_PRODUCTION')
  45. }
  46. }
  47. /**
  48. * @param {'development'|'production'} mode
  49. */
  50. function resolveBase(mode) {
  51. const explicit = process.env.VITE_STATIC_ASSETS_BASE_URL?.trim()
  52. if (explicit) {
  53. let b = explicit
  54. if (!b.endsWith('/')) b += '/'
  55. return b
  56. }
  57. const fileName = mode === 'production' ? '.env.production' : '.env.development'
  58. const fromFile = loadEnvFile(fileName).VITE_STATIC_ASSETS_BASE_URL?.trim()
  59. if (fromFile) {
  60. let b = fromFile
  61. if (!b.endsWith('/')) b += '/'
  62. return b
  63. }
  64. const { test, production } = readConstantsFromTs()
  65. const fallback = mode === 'production' ? production : test
  66. let b = fallback || test || 'https://www.chengyantek.cn/sqxp-static/'
  67. if (!b.endsWith('/')) b += '/'
  68. return b
  69. }
  70. function materialize(mode = 'development') {
  71. const raw = fs.readFileSync(pagesPath, 'utf8')
  72. if (!raw.includes('__STATIC_ASSETS_BASE_URL__')) return
  73. fs.writeFileSync(bakPath, raw, 'utf8')
  74. const base = resolveBase(mode)
  75. fs.writeFileSync(pagesPath, raw.replace(/__STATIC_ASSETS_BASE_URL__/g, base))
  76. }
  77. function restore() {
  78. if (!fs.existsSync(bakPath)) return
  79. fs.copyFileSync(bakPath, pagesPath)
  80. fs.unlinkSync(bakPath)
  81. }
  82. function main() {
  83. const cmd = process.argv[2] || 'build'
  84. if (cmd === 'materialize') {
  85. materialize('development')
  86. process.exit(0)
  87. }
  88. if (cmd === 'materialize:prod') {
  89. materialize('production')
  90. process.exit(0)
  91. }
  92. if (cmd === 'restore') {
  93. restore()
  94. process.exit(0)
  95. }
  96. if (cmd === 'build') {
  97. materialize('production')
  98. let code = 0
  99. try {
  100. execSync('npx uni build -p mp-weixin', { stdio: 'inherit', cwd: root, env: process.env })
  101. } catch (e) {
  102. code = typeof e?.status === 'number' ? e.status : 1
  103. } finally {
  104. restore()
  105. }
  106. process.exit(code)
  107. }
  108. console.error(
  109. 'Usage: pages-tabbar-migrate.mjs [materialize|materialize:prod|restore|build]'
  110. )
  111. process.exit(1)
  112. }
  113. main()