Files
zyt/admin/scripts/prepare-hospital-preview.mjs
T
2026-11-11 13:53:14 +08:00

30 lines
2.1 KiB
JavaScript

import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const project = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const source = path.join(project, 'server/public/admin')
const runtime = path.join(project, '.test-runtime')
const output = path.join(runtime, 'hospital-preview')
const target = path.join(output, 'admin')
const sourceHtml = fs.readFileSync(path.join(source, 'index.html'), 'utf8')
const entryPath = sourceHtml.match(/<script\b[^>]*\bsrc="\/admin\/assets\/([^"/]+\.js)"/i)?.[1]
if (!entryPath) throw new Error('未找到原医院构建入口')
const entry = fs.readFileSync(path.join(source, 'assets', entryPath), 'utf8')
const original = 'baseUrl:"https://admin.zhenyangtang.com.cn//"'
if (entry.split(original).length !== 2) throw new Error('医院公开 API baseUrl 字面量必须恰好出现一次')
fs.mkdirSync(runtime, { recursive: true })
// 只忽略生成物,不修改根 .gitignore 或原发布目录。
const ignoreFile = path.join(runtime, '.gitignore')
if (!fs.existsSync(ignoreFile)) fs.writeFileSync(ignoreFile, '*\n')
fs.cpSync(source, target, { recursive: true, force: true })
fs.writeFileSync(path.join(target, 'assets', entryPath), entry.replace(original, 'baseUrl:"/"'))
// 预览只加载原本地模块;不启动原 HTML 附带的远程客服/音视频脚本及其内联初始化。
const previewHtml = sourceHtml.replace(/<script\b([^>]*)>[\s\S]*?<\/script>/gi, (tag, attrs) =>
/\bsrc="\/admin\/assets\/[^"/]+\.js"/.test(attrs) ? tag : '')
fs.writeFileSync(path.join(target, 'index.html'), previewHtml.replace('<title>后台管理系统</title>', '<title>互联网医院</title>'))
fs.copyFileSync(path.join(project, 'admin/src/utils/groupSso.mjs'), path.join(target, 'group-sso.mjs'))
fs.copyFileSync(path.join(project, 'admin/public/group-login.html'), path.join(target, 'group-login.html'))
if (fs.readFileSync(path.join(target, 'assets', entryPath), 'utf8').includes(original)) throw new Error('本地 API 适配未完成')
process.stdout.write(JSON.stringify({ path: output, entry: entryPath }) + '\n')