dsh-sansheng-liubu/scripts/mode.mjs

107 lines
3.8 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.

#!/usr/bin/env node
// 上朝的启用开关:只改 profile 的 cordis.patch.yml,不碰任何别的插件行。
//
// node scripts/mode.mjs status # 现在开没开
// node scripts/mode.mjs on # 让「上朝」出现在模式选择器里(仍需你在新会话中点选)
// node scripts/mode.mjs off # 彻底移除这个选项
//
// 改动是一段带标记的 YAML,重复执行结果一致;不开模式时 profile 里不留任何痕迹。
import { readFile, writeFile } from 'node:fs/promises'
import { homedir } from 'node:os'
import { join, resolve } from 'node:path'
const BEGIN = '# >>> 上朝 preset(scripts/mode.mjs 管理,勿手改)'
const END = '# <<< 上朝 preset'
const MARK = 'preset-sansheng-liubu'
const profileDir = resolve(process.env.DSH_PROFILE_DIR ?? join(homedir(), '.dsh', 'profiles', 'desktop'))
const patchPath = join(profileDir, 'cordis.patch.yml')
const presetTemplatePath = resolve(import.meta.dirname, '..', 'preset.patch.yml')
async function readOrEmpty(path) {
try {
return await readFile(path, 'utf8')
} catch (error) {
if (error.code === 'ENOENT') return ''
throw error
}
}
/** preset.patch.yml 里真正要被合并的那几行(去掉说明注释)。 */
async function presetBlock() {
const template = await readOrEmpty(presetTemplatePath)
const lines = template.split(/\r?\n/)
const start = lines.findIndex((line) => /^- insert:/.test(line))
if (start < 0) throw new Error(`${presetTemplatePath} 里找不到 "- insert:" 起始行`)
return lines.slice(start).join('\n').trimEnd()
}
/** 顶层若是空数组占位 `[]`,合并前要先去掉,否则 YAML 不再是单个数组。 */
function dropEmptyRoot(text) {
const kept = text.split(/\r?\n/).filter((line) => line.trim() !== '[]')
return kept.join('\n').trimEnd()
}
function isEnabled(text) {
return text.includes(MARK)
}
function isManaged(text) {
return text.includes(BEGIN)
}
function stripBlock(text) {
const lines = text.split(/\r?\n/)
const begin = lines.findIndex((line) => line.trim() === BEGIN)
const end = lines.findIndex((line) => line.trim() === END)
if (begin < 0 || end < begin) return null
return [...lines.slice(0, begin), ...lines.slice(end + 1)].join('\n')
}
async function status() {
const text = await readOrEmpty(patchPath)
return {
profilePatch: patchPath,
enabled: isEnabled(text),
managed: isManaged(text),
}
}
async function enable() {
const text = await readOrEmpty(patchPath)
if (isEnabled(text)) return { ...(await status()), changed: false, note: '已经启用,未改动' }
const body = dropEmptyRoot(text)
const parts = [body, BEGIN, await presetBlock(), END].filter((part) => part !== '')
await writeFile(patchPath, `${parts.join('\n')}\n`, 'utf8')
return { ...(await status()), changed: true, note: '已启用;彻底重启 DSH 后,新会话可选「上朝」' }
}
async function disable() {
const text = await readOrEmpty(patchPath)
if (!isEnabled(text)) return { ...(await status()), changed: false, note: '本来就没启用,未改动' }
const stripped = stripBlock(text)
if (stripped === null) {
throw new Error(`${patchPath} 里的预设行不是本脚本写入的(缺少标记行),请手动删除后重试`)
}
const body = stripped.trimEnd()
const next = body === '' ? '[]\n' : `${body}\n`
await writeFile(patchPath, next, 'utf8')
return { ...(await status()), changed: true, note: '已关闭;彻底重启 DSH 后生效' }
}
const action = (process.argv[2] ?? 'status').toLowerCase()
const handlers = { status, on: enable, off: disable }
const handler = handlers[action]
if (!handler) {
console.error(`未知动作:${action}(可用:status / on / off)`)
process.exit(2)
}
try {
console.log(JSON.stringify(await handler(), null, 2))
} catch (error) {
console.error(String(error?.message ?? error))
process.exit(1)
}