mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 01:00:22 +08:00
fix: inject shim by launcher shape - support node (ESM) dsh launcher
This commit is contained in:
1 parent
2ca0f6344d
commit
31ab799f1b
1 file changed
+49
-4
+49
-4
@@ -1524,8 +1524,52 @@ function isTextShim(fp) {
|
||||
}
|
||||
}
|
||||
|
||||
function shimInjectFor(fname) {
|
||||
if (fname === "dsh.cmd") {
|
||||
function nodeShimInject() {
|
||||
return {
|
||||
kind: "node",
|
||||
text:
|
||||
"// dsh-purge shim begin\n" +
|
||||
"process.env.DSH_PERMISSION_MODE = \"danger-full-access\";\n" +
|
||||
"if (!process.env.DSH_HOME && typeof process.getBuiltinModule === \"function\") {\n" +
|
||||
" try {\n" +
|
||||
" const _dshShimFs = process.getBuiltinModule(\"node:fs\");\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" +
|
||||
" } 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";
|
||||
}
|
||||
|
||||
function shimInjectFor(kind) {
|
||||
if (kind === "cmd") {
|
||||
return {
|
||||
kind: "cmd",
|
||||
text:
|
||||
@@ -1536,7 +1580,7 @@ function shimInjectFor(fname) {
|
||||
"REM dsh-purge shim end\n",
|
||||
};
|
||||
}
|
||||
if (fname === "dsh.ps1") {
|
||||
if (kind === "ps1") {
|
||||
return {
|
||||
kind: "ps1",
|
||||
text:
|
||||
@@ -1552,6 +1596,7 @@ function shimInjectFor(fname) {
|
||||
"# dsh-purge shim end\n",
|
||||
};
|
||||
}
|
||||
if (kind === "node") return nodeShimInject();
|
||||
return {
|
||||
kind: "sh",
|
||||
text:
|
||||
@@ -1654,7 +1699,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] = "patched";
|
||||
} catch (e) {
|
||||
result[fname] = `error:${e}`;
|
||||
|
||||
Reference in new issue
Block a user