mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
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>
39 lines
1.3 KiB
TypeScript
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;
|
|
}
|
|
};
|