fix(cli): detect symlinked entrypoints (#652)

This commit is contained in:
shekohex
2026-03-13 09:49:25 +02:00
committed by GitHub
parent 8eecf1c0e9
commit 9fb7c90dbd
3 changed files with 87 additions and 11 deletions
+38
View File
@@ -0,0 +1,38 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
function normalizeCliEntryPath(filePath, realpath = fs.realpathSync) {
if (typeof filePath !== 'string' || filePath.trim().length === 0) {
return null;
}
const resolvedPath = path.resolve(filePath);
try {
return realpath(resolvedPath);
} catch {
return resolvedPath;
}
}
function isModuleCliExecution(entryPath = process.argv[1], moduleUrl = import.meta.url, realpath = fs.realpathSync) {
if (typeof entryPath !== 'string' || entryPath.trim().length === 0) {
return false;
}
try {
const normalizedEntryPath = normalizeCliEntryPath(entryPath, realpath);
const normalizedModulePath = normalizeCliEntryPath(fileURLToPath(moduleUrl), realpath);
if (!normalizedEntryPath || !normalizedModulePath) {
return false;
}
return pathToFileURL(normalizedEntryPath).href === pathToFileURL(normalizedModulePath).href;
} catch {
return false;
}
}
export {
normalizeCliEntryPath,
isModuleCliExecution,
};