diff --git a/lib/core.js b/lib/core.js index faa35d9..10dd5a8 100644 --- a/lib/core.js +++ b/lib/core.js @@ -1909,9 +1909,63 @@ function isTextShim(fp) { } } -function shimInjectFor(fname) { - // Locate DSH_HOME only — never set DSH_PERMISSION_MODE (that locked the UI mode switcher). - if (fname === "dsh.cmd") { +/** + * Node/ESM launcher shim: locate DSH_HOME only (never pin DSH_PERMISSION_MODE). + * Sync only — injected at file top before any async boundary. + */ +function nodeShimInject() { + return { + kind: "node", + text: + "// dsh-purge shim begin\n" + + "if (!process.env.DSH_HOME) {\n" + + " try {\n" + + " const _dshShimFs =\n" + + " typeof process.getBuiltinModule === \"function\"\n" + + " ? process.getBuiltinModule(\"node:fs\")\n" + + " : typeof require === \"function\"\n" + + " ? require(\"node:fs\")\n" + + " : null;\n" + + " if (_dshShimFs) {\n" + + " const _dshShimArgv = process.argv[1] || \"\";\n" + + " const _dshShimDir = _dshShimArgv.replace(/\\\\/g, \"/\").replace(/\\/[^/]*$/, \"\");\n" + + " for (const _dshShimRel of [\"\", \"/..\", \"/../..\"]) {\n" + + " const _dshShimCand = _dshShimDir + _dshShimRel + \"/.dsh\";\n" + + " try {\n" + + " if (_dshShimFs.statSync(_dshShimCand).isDirectory()) { process.env.DSH_HOME = _dshShimCand; break; }\n" + + " } catch {}\n" + + " }\n" + + " }\n" + + " } catch {}\n" + + "}\n" + + "// dsh-purge shim end\n", + }; +} + +/** + * 判定启动器形态:cmd 批处理 / ps1(含 pwsh shebang)/ node 启动器(ESM/CJS 均兼容)/ 其余按 sh。 + * 修复:Linux/macOS 的 `dsh` 常是 → lib/bin.js 的符号链接(#!/usr/bin/env node), + * 若按 shell 注入 `#` 注释 + export 会让 ESM 直接 SyntaxError。 + */ +function shimKindOf(fp) { + const base = path.basename(fp).toLowerCase(); + if (base === "dsh.cmd" || base.endsWith(".cmd")) return "cmd"; + if (base === "dsh.ps1" || base.endsWith(".ps1")) return "ps1"; + if (/\.(m?js|cjs|mts|cts)$/i.test(base)) return "node"; + let firstLine = ""; + try { + firstLine = (fs.readFileSync(fp, "utf8").split(/\r?\n/, 1)[0] || "").trim(); + } catch {} + if (firstLine.startsWith("#!")) { + if (/\bnode(js)?\b/i.test(firstLine)) return "node"; + if (/\b(pwsh|powershell)\b/i.test(firstLine)) return "ps1"; + } + return "sh"; +} + +/** Inject by launcher kind. Only sets DSH_HOME — never locks DSH_PERMISSION_MODE. */ +function shimInjectFor(kind) { + if (kind === "cmd") { return { kind: "cmd", text: @@ -1921,7 +1975,7 @@ function shimInjectFor(fname) { "REM dsh-purge shim end\n", }; } - if (fname === "dsh.ps1") { + if (kind === "ps1") { return { kind: "ps1", text: @@ -1936,6 +1990,7 @@ function shimInjectFor(fname) { "# dsh-purge shim end\n", }; } + if (kind === "node") return nodeShimInject(); return { kind: "sh", text: @@ -2123,7 +2178,7 @@ export async function patchShim(shimDir) { } catch {} } try { - await saveText(fp, insertShimInject(text, shimInjectFor(fname))); + await saveText(fp, insertShimInject(text, shimInjectFor(shimKindOf(fp)))); result[fname] = isDesktopCommandRuntimeDir(shimDir) ? "patched_desktop" : "patched"; } catch (e) { result[fname] = `error:${e}`; @@ -2220,16 +2275,10 @@ export async function installOverride(dshHome, force = false) { await fsp.mkdir(dshHome, { recursive: true }); if (fs.existsSync(fp) && !force) { const existing = await loadText(fp); - if (existing.trim().length > 0) return "exists"; - // Empty file = user cleared the prompt on purpose → keep empty (cleaned official only). - return "empty"; - } - if (!force) { - // First install (file missing): ship default-prompt-inject.md so any fresh - // machine gets a working jailbreak prompt for 0.1.1-rc.2 without hand-copy. - await fsp.writeFile(fp, defaultOverrideText(), "utf8"); - return "wrote"; + // Non-empty = keep user text. Empty = user cleared on purpose (cleaned official only). + return existing.trim().length > 0 ? "exists" : "empty"; } + // Missing file (or force): ship default-prompt-inject.md for fresh installs. await fsp.writeFile(fp, defaultOverrideText(), "utf8"); return "wrote"; }