mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 06:10:22 +08:00
fix: #13 按 npm/yarn/pnpm 实际布局找插件根,无效 DSH_BASE 写明原因
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
75b088c324
commit
6c88f4d075
3 files changed
+210
-10
No files matched your search
+3
-3
@@ -22,7 +22,7 @@ function printStatus(state) {
|
||||
out.push(` ${key.padEnd(22)} ${fp}`);
|
||||
}
|
||||
} else {
|
||||
out.push(" 插件根 / root NOT FOUND (set DSH_BASE)");
|
||||
out.push(" 插件根 / root " + core.missingAiBaseMessage());
|
||||
}
|
||||
out.push(` shim 目录 ${state.shim_dir || "未定位 / not found"}`);
|
||||
out.push(` 备份 / backup ${state.has_backup ? "有 / yes" : "无 / no"}`);
|
||||
@@ -59,8 +59,8 @@ async function main() {
|
||||
|
||||
if (mode === "--apply") {
|
||||
if (!state.ai_base) {
|
||||
console.log("[ERROR] 未找到 @deepseek-ai 插件根");
|
||||
console.log(" fix: 设置 DSH_BASE=完整路径");
|
||||
console.log("[ERROR] " + core.missingAiBaseMessage());
|
||||
console.log(" fix: 把 DSH_BASE 指到含 dsh-agent-instructions/lib 的 @deepseek-ai 目录");
|
||||
process.exit(1);
|
||||
}
|
||||
const { made, errors } = await core.backupAll(state.ai_base);
|
||||
|
||||
+203
-3
@@ -10,6 +10,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { execFileSync, execSync, spawn } from "node:child_process";
|
||||
import { createRequire } from "node:module";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import {
|
||||
installHideConsole,
|
||||
@@ -140,6 +141,125 @@ function isAiBase(p) {
|
||||
return p && isDir(path.join(p, "dsh-agent-instructions", "lib"));
|
||||
}
|
||||
|
||||
const PLUGIN_DIR = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const AI_RESOLVE_IDS = [
|
||||
"@deepseek-ai/dsh-agent-instructions/package.json",
|
||||
"@deepseek-ai/dsh-agent-instructions",
|
||||
"@deepseek-ai/dsh/package.json",
|
||||
"@deepseek-ai/dsh-base/package.json",
|
||||
"@deepseek-ai/dsh-web-app/package.json",
|
||||
];
|
||||
|
||||
function firstAiBase(cands) {
|
||||
for (const cand of cands) {
|
||||
if (cand && isAiBase(cand)) return path.normalize(cand);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Flat `node_modules/@deepseek-ai` or nested `dsh/node_modules/@deepseek-ai`. */
|
||||
function aiBaseGuessesAround(dir) {
|
||||
if (!dir) return [];
|
||||
const nested = nestedAiPath();
|
||||
return [
|
||||
dir,
|
||||
path.join(dir, "@deepseek-ai"),
|
||||
path.join(dir, "node_modules", "@deepseek-ai"),
|
||||
path.join(dir, "node_modules", nested),
|
||||
path.join(dir, "..", "node_modules", "@deepseek-ai"),
|
||||
path.join(dir, "..", "node_modules", nested),
|
||||
path.join(dir, "..", "..", "node_modules", "@deepseek-ai"),
|
||||
path.join(dir, "..", "..", "node_modules", nested),
|
||||
...pnpmHoistAiBases(dir),
|
||||
];
|
||||
}
|
||||
|
||||
function walkAncestorsForAiBase(start, maxUp = 10) {
|
||||
let dir = path.normalize(start);
|
||||
for (let i = 0; i < maxUp; i++) {
|
||||
const hit = firstAiBase(aiBaseGuessesAround(dir));
|
||||
if (hit) return hit;
|
||||
const parent = path.dirname(dir);
|
||||
if (parent === dir) break;
|
||||
dir = parent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function requireFrom(fromFile) {
|
||||
if (!fromFile) return null;
|
||||
try {
|
||||
let spec = fromFile;
|
||||
if (isDir(fromFile)) {
|
||||
const pkg = path.join(fromFile, "package.json");
|
||||
spec = isFile(pkg) ? pkg : path.join(fromFile, "index.js");
|
||||
}
|
||||
return createRequire(spec);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function findAiBaseFromRequire(fromFile) {
|
||||
const req = requireFrom(fromFile);
|
||||
if (!req) return null;
|
||||
for (const id of AI_RESOLVE_IDS) {
|
||||
try {
|
||||
const resolved = req.resolve(id);
|
||||
const hit = walkAncestorsForAiBase(path.dirname(resolved), 8);
|
||||
if (hit) return hit;
|
||||
} catch {
|
||||
// try next specifier
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Running `dsh web` / plugin Apply: resolve from this process, not PATH. */
|
||||
function findAiBaseFromRunningProcess() {
|
||||
const starts = [];
|
||||
if (process.argv[1]) {
|
||||
try { starts.push(path.resolve(process.argv[1])); } catch {}
|
||||
}
|
||||
starts.push(PLUGIN_DIR);
|
||||
try { starts.push(path.resolve(process.cwd())); } catch {}
|
||||
for (const start of starts) {
|
||||
const fromReq = findAiBaseFromRequire(start);
|
||||
if (fromReq) return fromReq;
|
||||
const fromWalk = walkAncestorsForAiBase(isDir(start) ? start : path.dirname(start));
|
||||
if (fromWalk) return fromWalk;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** `dsh plugin add` installs into a profile; official packages may sit there or only be resolvable from its package.json. */
|
||||
function findAiBaseFromDshHome(dshHome) {
|
||||
if (!dshHome || !isDir(dshHome)) return null;
|
||||
const profiles = path.join(dshHome, "profiles");
|
||||
const names = [];
|
||||
if (isDir(profiles)) {
|
||||
try { names.push(...fs.readdirSync(profiles)); } catch {}
|
||||
}
|
||||
const direct = firstAiBase([
|
||||
path.join(dshHome, "node_modules", "@deepseek-ai"),
|
||||
path.join(dshHome, "node_modules", nestedAiPath()),
|
||||
...names.map((n) => path.join(profiles, n, "node_modules", "@deepseek-ai")),
|
||||
...names.map((n) => path.join(profiles, n, "node_modules", nestedAiPath())),
|
||||
]);
|
||||
if (direct) return direct;
|
||||
const fromPnpm = firstAiBase([
|
||||
...pnpmHoistAiBases(dshHome),
|
||||
...names.flatMap((n) => pnpmHoistAiBases(path.join(profiles, n))),
|
||||
]);
|
||||
if (fromPnpm) return fromPnpm;
|
||||
for (const n of names) {
|
||||
const hit = findAiBaseFromRequire(path.join(profiles, n, "package.json"));
|
||||
if (hit) return hit;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** npm 全局 bin 在 Windows 落在 prefix,在 macOS/Linux 落在 prefix/bin。 */
|
||||
export function dirHasDshLauncher(dir) {
|
||||
if (!dir || !isDir(dir)) return false;
|
||||
@@ -952,6 +1072,8 @@ export function applyRuntimeEnv() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Startup may probe before the host finishes composing; retry on Apply.
|
||||
if (memo.ai.ready && !memo.ai.value) memo.ai = { ready: false, value: null };
|
||||
if (process.env.DSH_PERMISSION_MODE === "danger-full-access") {
|
||||
delete process.env.DSH_PERMISSION_MODE;
|
||||
}
|
||||
@@ -1004,6 +1126,55 @@ function nestedAiPath() {
|
||||
return path.join("@deepseek-ai", "dsh", "node_modules", "@deepseek-ai");
|
||||
}
|
||||
|
||||
/** pnpm 才有:`.modules.yaml` 或 `node_modules/.pnpm`。npm / yarn / bun 不要走这条。 */
|
||||
function isPnpmNodeModules(nmDir) {
|
||||
if (!nmDir || !isDir(nmDir)) return false;
|
||||
return isFile(path.join(nmDir, ".modules.yaml")) || isDir(path.join(nmDir, ".pnpm"));
|
||||
}
|
||||
|
||||
function pnpmVirtualStoreDir(root) {
|
||||
if (!root) return null;
|
||||
const nm = path.join(root, "node_modules");
|
||||
if (!isPnpmNodeModules(nm)) return null;
|
||||
const yaml = path.join(nm, ".modules.yaml");
|
||||
if (isFile(yaml)) {
|
||||
try {
|
||||
const text = fs.readFileSync(yaml, "utf8");
|
||||
const m = text.match(/^virtualStoreDir:\s*(.+)$/m);
|
||||
if (m) {
|
||||
const raw = m[1].trim().replace(/^['"]|['"]$/g, "");
|
||||
if (raw) return path.isAbsolute(raw) ? path.normalize(raw) : path.normalize(path.join(root, raw));
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
const fallback = path.join(nm, ".pnpm");
|
||||
return isDir(fallback) ? fallback : null;
|
||||
}
|
||||
|
||||
function pnpmProjectRootNear(base) {
|
||||
if (!base) return null;
|
||||
let dir = path.normalize(base);
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const nm = path.basename(dir) === "node_modules" ? dir : path.join(dir, "node_modules");
|
||||
if (isPnpmNodeModules(nm)) {
|
||||
return path.basename(dir) === "node_modules" ? path.dirname(dir) : dir;
|
||||
}
|
||||
const parent = path.dirname(dir);
|
||||
if (parent === dir) break;
|
||||
dir = parent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 仅当旁边真是 pnpm 树才返回 hoist 路径;否则空数组,不影响 npm / yarn / bun。 */
|
||||
function pnpmHoistAiBases(base) {
|
||||
const project = pnpmProjectRootNear(base);
|
||||
if (!project) return [];
|
||||
const store = pnpmVirtualStoreDir(project);
|
||||
if (!store) return [];
|
||||
return [path.join(store, "node_modules", "@deepseek-ai")];
|
||||
}
|
||||
|
||||
function aiBaseGuessesFor(base) {
|
||||
if (!base) return [];
|
||||
const nested = nestedAiPath();
|
||||
@@ -1014,6 +1185,7 @@ function aiBaseGuessesFor(base) {
|
||||
path.join(base, "lib", "node_modules", "@deepseek-ai"),
|
||||
path.join(base, "..", "lib", "node_modules", nested),
|
||||
path.join(base, "..", "node_modules", nested),
|
||||
...pnpmHoistAiBases(base),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1156,6 +1328,22 @@ function findDesktopAiBase() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Why Apply still says set DSH_BASE when the env is already set (issue #13). */
|
||||
export function dshBaseEnvStatus() {
|
||||
const raw = process.env.DSH_BASE;
|
||||
if (!raw || !String(raw).trim()) return { set: false, path: "", valid: false };
|
||||
const p = path.normalize(String(raw).trim());
|
||||
return { set: true, path: p, valid: isAiBase(p) };
|
||||
}
|
||||
|
||||
export function missingAiBaseMessage() {
|
||||
const env = dshBaseEnvStatus();
|
||||
if (env.set && !env.valid) {
|
||||
return `DSH_BASE 已设置但不是有效的插件根(需要其下有 dsh-agent-instructions/lib): ${env.path}`;
|
||||
}
|
||||
return "插件根未找到 / set DSH_BASE";
|
||||
}
|
||||
|
||||
export function findAiBase() {
|
||||
return remember("ai", () => {
|
||||
const env = process.env.DSH_BASE;
|
||||
@@ -1164,6 +1352,12 @@ export function findAiBase() {
|
||||
if (isAiBase(p)) return p;
|
||||
}
|
||||
|
||||
const fromProcess = findAiBaseFromRunningProcess();
|
||||
if (fromProcess) return fromProcess;
|
||||
|
||||
const fromHome = findAiBaseFromDshHome(findDshHome());
|
||||
if (fromHome) return fromHome;
|
||||
|
||||
const desktopBase = findDesktopAiBase();
|
||||
if (desktopBase) return desktopBase;
|
||||
|
||||
@@ -1183,7 +1377,7 @@ export function findAiBase() {
|
||||
}
|
||||
|
||||
const needle = path.join("node_modules", nested);
|
||||
for (const sr of [path.join(findDshHome(), ".."), os.homedir()]) {
|
||||
for (const sr of [findDshHome(), path.join(findDshHome(), ".."), os.homedir()]) {
|
||||
const found = findAiBaseRecursive(sr, needle, 0);
|
||||
if (found) return found;
|
||||
}
|
||||
@@ -1203,8 +1397,14 @@ function findAiBaseRecursive(root, needle, depth) {
|
||||
if (!e.isDirectory()) continue;
|
||||
if (e.name === "node_modules") {
|
||||
// needle already starts with node_modules/… — do not rely on Dirent.parentPath (Node 20.12+ only).
|
||||
const cand = path.join(root, needle);
|
||||
if (fs.existsSync(path.join(cand, "dsh-agent-instructions", "lib"))) return path.normalize(cand);
|
||||
const nestedHit = path.join(root, needle);
|
||||
if (isAiBase(nestedHit)) return path.normalize(nestedHit);
|
||||
const flatHit = path.join(root, "node_modules", "@deepseek-ai");
|
||||
if (isAiBase(flatHit)) return path.normalize(flatHit);
|
||||
if (isPnpmNodeModules(path.join(root, "node_modules"))) {
|
||||
const pnpmHit = firstAiBase(pnpmHoistAiBases(root));
|
||||
if (pnpmHit) return pnpmHit;
|
||||
}
|
||||
}
|
||||
const sub = findAiBaseRecursive(path.join(root, e.name), needle, depth + 1);
|
||||
if (sub) return sub;
|
||||
|
||||
+4
-4
@@ -56,7 +56,7 @@ function renderStatus(state) {
|
||||
out.push(` ${fsExists(fp) ? "✓" : "✗"} ${key.padEnd(22)} ${fp}`);
|
||||
}
|
||||
} else {
|
||||
out.push(line("插件根 / plugin root", "NOT FOUND (set DSH_BASE)", false));
|
||||
out.push(line("插件根 / plugin root", core.missingAiBaseMessage(), false));
|
||||
}
|
||||
out.push(line("shim 目录", state.shim_dir || "未定位 / not found", !!state.shim_dir));
|
||||
if (state.desktop_runtimes?.length) {
|
||||
@@ -258,7 +258,7 @@ async function handlePurgeCommand(rawInput, config) {
|
||||
const scrubbed = core.sanitizeDesktopCommandRuntimes();
|
||||
const state = await core.gatherState();
|
||||
if (!state.ai_base) {
|
||||
return { kind: "error", text: "插件根未找到 / plugin root NOT FOUND — 请设置 DSH_BASE" };
|
||||
return { kind: "error", text: core.missingAiBaseMessage() };
|
||||
}
|
||||
await core.backupAll(state.ai_base);
|
||||
const report = await core.applyPatches(state.ai_base);
|
||||
@@ -538,7 +538,7 @@ export function apply(ctx, config) {
|
||||
core.applyRuntimeEnv();
|
||||
const scrubbed = core.sanitizeDesktopCommandRuntimes();
|
||||
const state = await core.gatherState();
|
||||
if (!state.ai_base) return { text: "ERROR: 插件根未找到 / set DSH_BASE" };
|
||||
if (!state.ai_base) return { text: "ERROR: " + core.missingAiBaseMessage() };
|
||||
await core.backupAll(state.ai_base);
|
||||
const report = await core.applyPatches(state.ai_base);
|
||||
await core.installOverride(state.dsh_home, false);
|
||||
@@ -707,7 +707,7 @@ function installWebServer(ctx, cfg) {
|
||||
const scrubbed = core.sanitizeDesktopCommandRuntimes();
|
||||
const state = await core.gatherState();
|
||||
if (!state.ai_base) {
|
||||
sendJson(response, 500, { ok: false, error: "插件根未找到 / set DSH_BASE" });
|
||||
sendJson(response, 500, { ok: false, error: core.missingAiBaseMessage() });
|
||||
return;
|
||||
}
|
||||
await core.backupAll(state.ai_base);
|
||||
|
||||
Reference in new issue
Block a user