mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 07:50:22 +08:00
fix: 命令撞名只丢那一条;主插件 insert 自愈(#162 #159)
This commit is contained in:
1 parent
2a3c7cef6a
commit
add692e491
7 files changed
+179
-53
No files matched your search
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* 用户层 profile/cordis.patch.yml 的本体 insert(#154 / #159)。
|
||||
*
|
||||
* 包内 overlay 只改已入队行的 inject/config,不能再 insert 一次(Hub 已入队会
|
||||
* already queued)。postinstall 和演练台子行都走这里:行不存在就写一条。
|
||||
*/
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
export const PURGE_NAME = "dsh-purge";
|
||||
|
||||
export const INSERT_BLOCK = [
|
||||
"- insert:",
|
||||
" - id: dsh-purge",
|
||||
" name: dsh-purge",
|
||||
" inject: [systemPrompt, tools]",
|
||||
" config:",
|
||||
" enabled: true",
|
||||
" stripMnemon: true",
|
||||
" capabilityProtocol: true",
|
||||
" hooksDenyBypass: true",
|
||||
"",
|
||||
].join("\n");
|
||||
|
||||
export function profileHasPurgeBodyInsert(text) {
|
||||
const lines = String(text || "").split(/\r?\n/);
|
||||
let insertIndent = -1;
|
||||
for (const line of lines) {
|
||||
if (/^-\s*insert:\s*$/.test(line) || /^\s+-\s*insert:\s*$/.test(line)) {
|
||||
insertIndent = (line.match(/^[ \t]*/) || [""])[0].length;
|
||||
continue;
|
||||
}
|
||||
if (insertIndent < 0) continue;
|
||||
const indent = (line.match(/^[ \t]*/) || [""])[0].length;
|
||||
const trimmed = line.trim();
|
||||
if (trimmed && indent <= insertIndent && /^-\s/.test(trimmed)) {
|
||||
insertIndent = -1;
|
||||
if (/^-\s*insert:\s*$/.test(trimmed)) {
|
||||
insertIndent = indent;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// 本体行:id: dsh-purge,或用户按 #159 临时绕过写的 dsh-purge-main / name: dsh-purge
|
||||
if (/^-\s*id:\s*dsh-purge(-main)?\s*$/.test(trimmed)) return true;
|
||||
if (/^-\s*name:\s*dsh-purge\s*$/.test(trimmed)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function ensureProfileBodyInsert(profileDir) {
|
||||
const fp = path.join(profileDir, "cordis.patch.yml");
|
||||
let text = "";
|
||||
try { text = fs.readFileSync(fp, "utf8"); } catch { text = ""; }
|
||||
if (profileHasPurgeBodyInsert(text)) {
|
||||
return { ok: true, wrote: false, path: fp };
|
||||
}
|
||||
const prefix = text && !text.endsWith("\n") ? "\n" : "";
|
||||
fs.writeFileSync(fp, `${text}${prefix}${text ? "\n" : ""}${INSERT_BLOCK}`, "utf8");
|
||||
return { ok: true, wrote: true, path: fp };
|
||||
}
|
||||
|
||||
function readJson(fp) {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(fp, "utf8"));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function profileOwnsPurge(dir) {
|
||||
const pkg = readJson(path.join(dir, "package.json"));
|
||||
const bundles = pkg?.dsh?.profile?.bundles;
|
||||
if (Array.isArray(bundles) && bundles.includes(PURGE_NAME)) return true;
|
||||
try {
|
||||
return fs.existsSync(path.join(dir, "node_modules", PURGE_NAME, "package.json"));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function listProfileDirs(dshHome) {
|
||||
const root = path.join(dshHome || "", "profiles");
|
||||
const out = [];
|
||||
let names = [];
|
||||
try { names = fs.readdirSync(root); } catch { return out; }
|
||||
for (const name of names) {
|
||||
const dir = path.join(root, name);
|
||||
try {
|
||||
if (fs.statSync(dir).isDirectory()) out.push(dir);
|
||||
} catch { /* skip */ }
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 已装进 bundles / node_modules 但缺 insert 的 profile,补上本体行。 */
|
||||
export function ensureAllProfileBodyInserts(dshHome) {
|
||||
const results = [];
|
||||
for (const dir of listProfileDirs(dshHome)) {
|
||||
if (!profileOwnsPurge(dir)) continue;
|
||||
try {
|
||||
results.push({ dir, ...ensureProfileBodyInsert(dir) });
|
||||
} catch (error) {
|
||||
results.push({
|
||||
dir,
|
||||
ok: false,
|
||||
wrote: false,
|
||||
path: path.join(dir, "cordis.patch.yml"),
|
||||
error: error && error.message ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
Reference in new issue
Block a user