#!/usr/bin/env node /** * pages.json 中 tabBar 使用占位符 __STATIC_ASSETS_BASE_URL__,构建前替换为真实根地址,构建后还原。 * - materialize:写入备份并替换(默认按「开发/测试」解析基址;见 resolveBase) * - materialize:prod:按「生产」解析基址(与发行 `build` 子命令一致) * - restore:从备份还原并删除备份 * - build:materialize(prod) → uni build → restore(finally) */ import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import { execSync } from 'node:child_process' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const root = path.resolve(__dirname, '..') const pagesPath = path.join(root, 'src/pages.json') const bakPath = path.join(root, 'src/.pages-json.tabbar-placeholder.bak') function loadEnvFile(rel) { const p = path.join(root, rel) if (!fs.existsSync(p)) return {} const out = {} for (const line of fs.readFileSync(p, 'utf8').split('\n')) { const t = line.trim() if (!t || t.startsWith('#')) continue const eq = t.indexOf('=') if (eq === -1) continue const k = t.slice(0, eq).trim() let v = t.slice(eq + 1).trim() if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) { v = v.slice(1, -1) } out[k] = v } return out } function readConstantsFromTs() { const defPath = path.join(root, 'src/config/static-assets-defaults.ts') const raw = fs.readFileSync(defPath, 'utf8') const pick = (name) => { const m = raw.match(new RegExp(`export const ${name}\\s*=\\s*'([^']*)'`)) return (m?.[1] ?? '').trim() } return { test: pick('STATIC_ASSETS_BASE_URL_TEST'), production: pick('STATIC_ASSETS_BASE_URL_PRODUCTION') } } /** * @param {'development'|'production'} mode */ function resolveBase(mode) { const explicit = process.env.VITE_STATIC_ASSETS_BASE_URL?.trim() if (explicit) { let b = explicit if (!b.endsWith('/')) b += '/' return b } const fileName = mode === 'production' ? '.env.production' : '.env.development' const fromFile = loadEnvFile(fileName).VITE_STATIC_ASSETS_BASE_URL?.trim() if (fromFile) { let b = fromFile if (!b.endsWith('/')) b += '/' return b } const { test, production } = readConstantsFromTs() const fallback = mode === 'production' ? production : test let b = fallback || test || 'https://www.chengyantek.cn/sqxp-static/' if (!b.endsWith('/')) b += '/' return b } function materialize(mode = 'development') { const raw = fs.readFileSync(pagesPath, 'utf8') if (!raw.includes('__STATIC_ASSETS_BASE_URL__')) return fs.writeFileSync(bakPath, raw, 'utf8') const base = resolveBase(mode) fs.writeFileSync(pagesPath, raw.replace(/__STATIC_ASSETS_BASE_URL__/g, base)) } function restore() { if (!fs.existsSync(bakPath)) return fs.copyFileSync(bakPath, pagesPath) fs.unlinkSync(bakPath) } function main() { const cmd = process.argv[2] || 'build' if (cmd === 'materialize') { materialize('development') process.exit(0) } if (cmd === 'materialize:prod') { materialize('production') process.exit(0) } if (cmd === 'restore') { restore() process.exit(0) } if (cmd === 'build') { materialize('production') let code = 0 try { execSync('npx uni build -p mp-weixin', { stdio: 'inherit', cwd: root, env: process.env }) } catch (e) { code = typeof e?.status === 'number' ? e.status : 1 } finally { restore() } process.exit(code) } console.error( 'Usage: pages-tabbar-migrate.mjs [materialize|materialize:prod|restore|build]' ) process.exit(1) } main()