🌳
pt0/serverF/isDirectlyRunF.mts
1import { fileURLToPath } from 'url'
2import { resolve } from 'path'
5/** Get process.argv fresh (not cached at import time). Always returns array (slice semantics). */
6export function getProcArgv(): string[]
7export function getProcArgv(start: number): string[]
8export function getProcArgv(start: number, end: number): string[]
9export function getProcArgv(start?: number, end?: number): string[] {
10 if (start === undefined) return process.argv
11 return process.argv.slice(start, end)
14export const getImportMetaUrlPath = (importMetaUrl: string): string => {
15 return resolve(fileURLToPath(importMetaUrl))
18export const isDirectlyRun = (importMetaUrl: string): boolean => {
19 if (process.env.IS_BUNDLE) return false // esbuild bundles share one import.meta.url → all self-test blocks would fire; disable in bundles
20 assertDefined(importMetaUrl)
21 const argv = getProcArgv()
22 if (!argv[1]) return false // e.g. ptnode -e or node --eval
23 const nodePath = resolve(argv[1])
24 const modulePath = getImportMetaUrlPath(importMetaUrl)
25 return nodePath === modulePath