Files
reverse-engineering-anything/src/process/CurrentEntrypointPath.ts
T
6136c6f3ba fix(cli): refuse silent direct command-registry execution (#11204)
Fail visibly with a dispatcher hint and exit status 1 when the compiled command registry is executed directly. Reuse the existing argv/module identity owner for both entry modules and preserve normal imports and supported dispatch.

Fixes #10762.
Builds on #10766.

Co-authored-by: 2SinceJuly <202121119+2SinceJuly@users.noreply.github.com>
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-15 22:23:49 +08:00

39 lines
1.3 KiB
TypeScript

import { realpathSync } from "node:fs";
import { resolve } from "node:path";
import { pathToFileURL } from "node:url";
import {
javaScriptRuntime,
type JavaScriptRuntime,
} from "../domain/runtimeVersion.js";
import { absoluteCallerPath } from "../filesystem/callerPath.js";
/** Runtime-specific absolute argv script selection, or undefined when none was supplied.
* Native executable paths have different traversal semantics and must not use this.
*/
export const normalizeEntrypointPath = (
entrypoint: string | undefined,
runtime: JavaScriptRuntime["id"] = javaScriptRuntime(process.versions).id,
): string | undefined =>
entrypoint === undefined || entrypoint.length === 0
? undefined
: runtime === "bun"
? absoluteCallerPath(entrypoint)
: resolve(entrypoint);
export const CURRENT_ENTRYPOINT_PATH = normalizeEntrypointPath(process.argv[1]);
/** True when the runtime executed `moduleUrl` directly rather than importing it.
* A synthetic argv path that no longer resolves is not a direct run.
*/
export const isDirectEntrypoint = (
entryPath: string | undefined,
moduleUrl: string,
): boolean => {
if (entryPath === undefined) return false;
try {
return pathToFileURL(realpathSync(entryPath)).href === moduleUrl;
} catch {
return false;
}
};