Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

entrypoint.ts

33 lines1.2 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeURL from "node:url";
4​
5/**
6 * `import.meta.main` answers this directly, but it only exists on Node 22.18+
7 * and 24.2+. This package's `engines.node` range also accepts 22.16, 22.17 and
8 * 23.11, where it is `undefined`: an `if (import.meta.main)` guard never runs,
9 * so the process loads every module and exits 0 without output.
10 */
11export const isEntrypoint = (input: {
12 readonly moduleUrl: string;
13 readonly entryPath: string | undefined;
14 readonly runtimeMain: boolean | undefined;
15}): boolean => {
16 if (input.runtimeMain !== undefined) {
17 return input.runtimeMain;
18 }
19 if (input.entryPath === undefined || input.entryPath === "") {
20 return false;
21 }
22 if (input.moduleUrl === NodeURL.pathToFileURL(input.entryPath).href) {
23 return true;
24 }
25 // npm and npx install the CLI as a symlink. Without `--preserve-symlinks` the
26 // module URL is the resolved real path while `process.argv[1]` keeps the link
27 // path, so the comparison above misses.
28 try {
29 return input.moduleUrl === NodeURL.pathToFileURL(NodeFS.realpathSync(input.entryPath)).href;
30 } catch {
31 return false;
32 }
33};