dsh-sansheng-liubu/scripts/check-install.mjs

87 lines
4.4 KiB
JavaScript
Raw 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.

import { readFile } from 'node:fs/promises'
import { createRequire } from 'node:module'
import { homedir } from 'node:os'
import { dirname, join, resolve } from 'node:path'
import { pathToFileURL } from 'node:url'
const profileDir = resolve(process.env.DSH_PROFILE_DIR ?? join(homedir(), '.dsh', 'profiles', 'desktop'))
const profilePath = join(profileDir, 'package.json')
const profile = JSON.parse(await readFile(profilePath, 'utf8'))
const profilePatchPath = join(profileDir, 'cordis.patch.yml')
function assert(condition, message) {
if (!condition) throw new Error(message)
}
/** 去掉整行注释后剩下的 YAML 主体。 */
function withoutComments(source) {
return source.replace(/^\s*#.*$/gm, '').trim()
}
for (const name of ['dsh-sansheng-liubu-mode', 'dsh-sansheng-liubu-panel']) {
assert(profile.dependencies?.[name]?.startsWith('link:'), `${name} must be a link dependency`)
}
// 面板常驻;模式故意不要求在 bundles 里——它必须由用户显式启用。
assert(
profile.dsh?.profile?.bundles?.includes('dsh-sansheng-liubu-panel'),
'panel must be listed in dsh.profile.bundles', // 面板是常驻 UI,不参与 Agent 策略
)
const requireFromProfile = createRequire(join(profileDir, 'package.json'))
const modeManifestPath = requireFromProfile.resolve('dsh-sansheng-liubu-mode/package.json')
const panelManifestPath = requireFromProfile.resolve('dsh-sansheng-liubu-panel/package.json')
const modeDir = dirname(modeManifestPath)
const modeManifest = JSON.parse(await readFile(modeManifestPath, 'utf8'))
const panelManifest = JSON.parse(await readFile(panelManifestPath, 'utf8'))
assert(modeManifest.dsh?.bundle?.patch, 'mode package must declare dsh.bundle.patch')
assert(panelManifest.dsh?.bundle?.patch, 'panel package must declare dsh.bundle.patch')
assert(panelManifest.dsh?.client?.platform === 'web', 'panel must declare a web client')
assert(panelManifest.dsh?.client?.inject?.length > 0, 'panel must declare client injections')
// 装包 ≠ 开模式:包自带的 patch 必须是空层,绝不能注册「上朝」预设。
const bundlePatch = await readFile(join(modeDir, modeManifest.dsh.bundle.patch), 'utf8')
assert(
!/preset-sansheng-liubu/.test(bundlePatch),
'bundle patch must not register the 上朝 preset; opting in goes through preset.patch.yml',
)
assert(
['', '[]'].includes(withoutComments(bundlePatch)),
'bundle patch must stay an empty layer so installing the mode changes nothing',
)
// 可选层必须存在,并且能复现出同一个 preset id 与策略插件。
const presetPatch = await readFile(join(modeDir, 'preset.patch.yml'), 'utf8')
assert(/id: preset-sansheng-liubu/.test(presetPatch), 'preset.patch.yml must declare preset-sansheng-liubu')
assert(/^\s+id: sansheng-liubu\s*$/m.test(presetPatch), 'preset.patch.yml must declare the sansheng-liubu preset id')
assert(/name: dsh-sansheng-liubu-mode/.test(presetPatch), 'preset.patch.yml must load dsh-sansheng-liubu-mode')
let modeEnabled = false
try {
modeEnabled = /preset-sansheng-liubu/.test(await readFile(profilePatchPath, 'utf8'))
} catch {
// 没有 patch 文件 = 从未启用过
}
const mode = await import(pathToFileURL(join(modeDir, modeManifest.main)).href)
const panel = await import(pathToFileURL(join(dirname(panelManifestPath), panelManifest.main)).href)
assert(typeof mode.apply === 'function', 'mode host entry must export apply')
assert(typeof panel.apply === 'function', 'panel host must export apply')
const clientSource = await readFile(join(dirname(panelManifestPath), 'lib', 'client.js'), 'utf8')
assert(clientSource.includes('id: "dsh-sansheng-liubu-panel"'), 'client bundle id must match the panel package name')
assert(clientSource.includes('"sidebar.right.pane.tab"'), 'client bundle must register the right sidebar tab')
assert(!clientSource.includes('"sidebar.panellist"'), 'client bundle must not register the retired full-page panel entry')
assert(!clientSource.includes('name: "main"'), 'client bundle must not register a main workspace panel')
console.log(JSON.stringify({
profile: profilePath,
mode: modeManifestPath,
panel: panelManifestPath,
bundles: profile.dsh.profile.bundles.filter((name) => name.startsWith('dsh-sansheng-')),
modeEnabled,
hint: modeEnabled
? '「上朝」已启用:它只是模式选择器里的一个选项,不会自动成为新会话默认。'
: '「上朝」未启用(装包不等于开模式)。要当可选模式用:node scripts/mode.mjs on,再彻底重启 DSH。',
}, null, 2))