Files
dsh-purge/bin/dsh-purge.js
T

126 lines
5.5 KiB
JavaScript
Executable File
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
// dsh-purge CLI(小杨)— 移植自 dsh_purge.py 的静默模式,功能一个不差
// 用法:
// dsh-purge # TUI 等价:默认只显示状态(这里同 --status)
// dsh-purge --status # 显示状态
// dsh-purge --apply # 应用全部清洗
// dsh-purge --revert # 回滚还原
// dsh-purge --edit # 编辑 prompt-inject.md
// dsh-purge --help # 帮助
import * as core from "../lib/core.js";
const args = process.argv.slice(2);
function printStatus(state) {
const out = [];
out.push("dsh-purge 状态 / Status");
out.push(" DSH_HOME " + state.dsh_home);
if (state.ai_base) {
out.push(" 插件根 / root " + state.ai_base);
for (const [key, fp] of Object.entries(state.files)) {
out.push(` ${key.padEnd(22)} ${fp}`);
}
} else {
out.push(" 插件根 / root NOT FOUND (set DSH_BASE)");
}
out.push(` shim 目录 ${state.shim_dir || "未定位 / not found"}`);
out.push(` 备份 / backup ${state.has_backup ? "有 / yes" : "无 / no"}`);
out.push(` 注入文件 ${state.override_path}`);
out.push(` 注入状态 ${state.override_status}`);
out.push("");
out.push(` 补丁 ${state.patches_applied}/${core.ALL_PATCHES.length} 已清洗, ${state.patches_pending} 待清洗`);
for (const p of core.ALL_PATCHES) {
const s = state.patch_status[p.id];
const mark = s === "applied" ? "✓" : s === "pending" ? "✗" : "·";
out.push(` ${mark} [#${String(p.id).padEnd(2)}] ${p.name.padEnd(30)} ${p.layer}/${p.layer_en} — ${s}`);
}
out.push("");
out.push(` shim: dsh.cmd=${state.shim_cmd} dsh.ps1=${state.shim_ps1} dsh=${state.shim_bin}`);
return out.join("\n");
}
async function main() {
const mode = args.find((a) => ["--apply", "--revert", "--status", "--edit", "--help"].includes(a));
if (mode === "--help" || args.includes("-h")) {
console.log(`dsh-purge 用法:
dsh-purge --status 显示状态
dsh-purge --apply 应用全部清洗(提示词+代码+shim+override)
dsh-purge --revert 回滚还原
dsh-purge --edit 编辑注入文件 prompt-inject.md
dsh-purge --help 帮助`);
return;
}
const state = await core.gatherState();
console.log(printStatus(state));
console.log("");
if (mode === "--apply") {
if (!state.ai_base) {
console.log("[ERROR] 未找到 @deepseek-ai 插件根");
console.log(" fix: 设置 DSH_BASE=完整路径");
process.exit(1);
}
const { made, errors } = await core.backupAll(state.ai_base);
for (const b of made) console.log(` ✓ 备份 / backup → ${b}`);
for (const [p, e] of errors) console.log(` ⚠ 备份失败 ${p}: ${e}`);
const report = await core.applyPatches(state.ai_base);
for (const r of report) {
const m = r.status === "applied" ? "✓ 已清洗" : r.status === "already" ? "- 已是最新" : r.status === "missing_file" ? "⚠ 文件缺失" : `✗ ${r.status}`;
console.log(` ${m} patch #${String(r.patch_id).padEnd(2)} ${r.name}`);
}
const ov = await core.installOverride(state.dsh_home, false);
console.log(ov === "wrote" ? ` ✓ 写入 ${state.override_path}` : " - prompt-inject.md 已存在 (你的自定义内容将保留)");
if (state.shim_dir) {
const r = await core.patchShim(state.shim_dir);
for (const [fname, st] of Object.entries(r)) {
if (st === "patched") console.log(` ✓ shim ${fname} 注入 / injected`);
else if (st === "already_patched") console.log(` - shim ${fname} 已注入 / already injected`);
else if (st === "missing") console.log(` - shim ${fname} 不存在 / missing`);
else console.log(` ⚠ shim ${fname}: ${st}`);
}
} else {
console.log(" ⚠ 未定位 shim 目录,跳过注入");
}
console.log("");
console.log("全部完成 / All done。重启 dsh 生效 / Restart dsh to take effect.");
} else if (mode === "--revert") {
if (state.ai_base) {
const { reverted, errors } = await core.revertAll(state.ai_base);
for (const p of reverted) console.log(` ✓ 已还原 / Reverted ${p}`);
for (const [p, e] of errors) console.log(` ⚠ 还原失败 ${p}: ${e}`);
if (reverted.length === 0) console.log(" - 没有补丁备份可还原 / no patch backup");
}
if (state.shim_dir) {
const r = await core.revertShim(state.shim_dir);
for (const [fname, st] of Object.entries(r)) {
if (st === "reverted" || st === "stripped_inject") console.log(` ✓ shim ${fname}: ${st}`);
else console.log(` - shim ${fname}: ${st}`);
}
}
console.log("");
console.log("回滚完成 / Revert done。重启 dsh 后恢复 / restart to restore.");
console.log("注: prompt-inject.md 保留(用户文件)/ prompt-inject.md kept (user file)");
} else if (mode === "--edit") {
const r = core.editOverride(state.dsh_home);
if (!r.ok && r.needCreate) {
await core.installOverride(state.dsh_home, true);
console.log(` ✓ 已创建默认注入文件:${state.override_path}`);
core.editOverride(state.dsh_home);
} else {
console.log(` → 已打开 ${r.editor} 编辑注入文件:${r.path}`);
}
console.log(" 编辑完成后重启 dsh 生效。");
} else {
// 默认只显示状态(对齐 py 的"默认不清洗")
console.log("默认不清洗,使用 --apply 应用清洗。");
}
}
main().catch((e) => {
console.error("[dsh-purge] error:", e);
process.exit(1);
});