dsh-sansheng-liubu/scripts/workbench-preflight.mjs

114 lines
4.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 重启前自检:严格只读,不创建、覆盖或迁移任何用户文件。
//
// 0.13.0 起朝堂面板已并回主包 `dsh-sansheng-liubu-mode`,不再有独立的
// `dsh-sansheng-liubu-workbench` 安装包。自检只回答“当前这份包能不能在
// 宿主上正常加载”,安装/修复/迁移是另外的显式操作,不混在这里。
//
// 用法:node scripts/workbench-preflight.mjs
// 退出码:0 全通;1 有失败项(打印哪一项失败、为什么)。
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import vm from 'node:vm'
import { createWorkbenchHost } from '../workbench/host/index.mjs'
import { WorkbenchStore } from '../workbench/core/store.mjs'
const PACKAGE_NAME = 'dsh-sansheng-liubu-mode'
const PACKAGE_ROOT = path.resolve(import.meta.dirname, '..')
const results = []
function check(name, run) {
try {
const detail = run()
results.push({ name, ok: true, detail })
} catch (error) {
results.push({ name, ok: false, detail: error?.message ?? String(error) })
}
}
function dshHome(env = process.env) {
const value = typeof env.DSH_HOME === 'string' && env.DSH_HOME.trim() ? env.DSH_HOME.trim() : ''
return value === '' ? path.join(os.homedir(), '.dsh') : value
}
let packageJson = null
check('读取本包 package.json', () => {
const file = path.join(PACKAGE_ROOT, 'package.json')
packageJson = JSON.parse(fs.readFileSync(file, 'utf8'))
if (packageJson.name !== PACKAGE_NAME) {
throw new Error(`包名是 ${packageJson.name},预期 ${PACKAGE_NAME}`)
}
return `${packageJson.name} ${packageJson.version}`
})
let entryFile = null
check('面板客户端入口存在且属于主包', () => {
const exported = packageJson?.exports?.['./client']
const rel = typeof exported === 'string' ? exported : exported?.default
if (typeof rel !== 'string') throw new Error('package.json 没有 exports["./client"]')
entryFile = path.join(PACKAGE_ROOT, rel)
const size = fs.statSync(entryFile).size
if (size < 1024) throw new Error(`入口文件过小(${size} 字节),可能是空壳`)
return `${path.relative(PACKAGE_ROOT, entryFile)} · ${size} 字节`
})
check('客户端 bundle 语法可解析(语法错会让整页白屏)', () => {
if (entryFile === null) throw new Error('上一个检查没通过,跳过')
const text = fs.readFileSync(entryFile, 'utf8')
if (!text.includes('__ModuleLoader__')) throw new Error('入口不像 DSH 客户端 bundle(缺少 __ModuleLoader__)')
if (!text.includes(PACKAGE_NAME)) throw new Error(`入口里没有主包 id ${PACKAGE_NAME}`)
// 只解析不执行:能抓住未闭合括号、非法 token 这类致命错误。
new vm.Script(text, { filename: entryFile })
return `${Math.round(text.length / 1024)} KB`
})
check('Host 侧装配:内存账本上能起 16 条路由', () => {
const routes = new Map()
const fakeCtx = {
get: () => undefined,
webServer: {
register(route) {
routes.set(route.path, route)
return () => routes.delete(route.path)
},
},
}
const store = new WorkbenchStore({ file: null })
const host = createWorkbenchHost(fakeCtx, { store, startReconciler: false, load: false })
try {
if (routes.size !== 16) throw new Error(`注册了 ${routes.size} 条路由,预期 16 条`)
// bootstrap 要落在具体项目上才有席位;这里现建一个临时项目,不写磁盘。
const project = host.service.registerProject({ hostWorkspaceId: 'preflight' }).project
const boot = host.service.bootstrap(project.id)
if (!Array.isArray(boot.seats) || boot.seats.length !== 10) throw new Error('bootstrap 没有给出十席')
return `${routes.size} 条路由 · 十席就位`
} finally {
host.dispose()
}
})
check('账本落点可写(只做 access 检查,不创建文件)', () => {
const dir = path.join(dshHome(), 'sansheng-liubu')
// 从目标目录向上找第一个真实存在的父目录;只检查写权限,不创建目录或文件。
let probe = dir
for (;;) {
if (fs.existsSync(probe)) break
const parent = path.dirname(probe)
if (parent === probe) throw new Error(`找不到可检查的父目录:${dir}`)
probe = parent
}
fs.accessSync(probe, fs.constants.W_OK)
return `${dir}(自上而下检查到 ${probe})`
})
let failed = 0
for (const row of results) {
const mark = row.ok ? 'PASS' : 'FAIL'
if (!row.ok) failed += 1
process.stdout.write(`[${mark}] ${row.name}${row.detail ? ` — ${row.detail}` : ''}\n`)
}
process.stdout.write(`\n${results.length - failed}/${results.length} 通过\n`)
if (failed > 0) process.exitCode = 1