2026-03-13 09:49:25 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 11:33:23 +02:00
|
|
|
function isModuleCliExecution(
|
|
|
|
|
entryPath = process.argv[1],
|
|
|
|
|
moduleUrl,
|
|
|
|
|
realpath = fs.realpathSync,
|
|
|
|
|
expectedBinName,
|
|
|
|
|
) {
|
2026-03-13 09:49:25 +02:00
|
|
|
if (typeof entryPath !== 'string' || entryPath.trim().length === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-13 11:33:23 +02:00
|
|
|
if (typeof moduleUrl !== 'string' || moduleUrl.trim().length === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-13 09:49:25 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const normalizedEntryPath = normalizeCliEntryPath(entryPath, realpath);
|
|
|
|
|
const normalizedModulePath = normalizeCliEntryPath(fileURLToPath(moduleUrl), realpath);
|
|
|
|
|
if (!normalizedEntryPath || !normalizedModulePath) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-13 11:33:23 +02:00
|
|
|
if (pathToFileURL(normalizedEntryPath).href === pathToFileURL(normalizedModulePath).href) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof expectedBinName === 'string' && expectedBinName.trim().length > 0) {
|
|
|
|
|
const parsedEntryName = path.parse(normalizedEntryPath).name.toLowerCase();
|
|
|
|
|
return parsedEntryName === expectedBinName.trim().toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2026-03-13 09:49:25 +02:00
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
normalizeCliEntryPath,
|
|
|
|
|
isModuleCliExecution,
|
|
|
|
|
};
|