diff --git a/packages/web/bin/cli-entry.js b/packages/web/bin/cli-entry.js index 49badf26..31ea8161 100644 --- a/packages/web/bin/cli-entry.js +++ b/packages/web/bin/cli-entry.js @@ -15,10 +15,18 @@ function normalizeCliEntryPath(filePath, realpath = fs.realpathSync) { } } -function isModuleCliExecution(entryPath = process.argv[1], moduleUrl = import.meta.url, realpath = fs.realpathSync) { +function isModuleCliExecution( + entryPath = process.argv[1], + moduleUrl, + realpath = fs.realpathSync, + expectedBinName, +) { if (typeof entryPath !== 'string' || entryPath.trim().length === 0) { return false; } + if (typeof moduleUrl !== 'string' || moduleUrl.trim().length === 0) { + return false; + } try { const normalizedEntryPath = normalizeCliEntryPath(entryPath, realpath); @@ -26,7 +34,16 @@ function isModuleCliExecution(entryPath = process.argv[1], moduleUrl = import.me if (!normalizedEntryPath || !normalizedModulePath) { return false; } - return pathToFileURL(normalizedEntryPath).href === pathToFileURL(normalizedModulePath).href; + 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; } catch { return false; } diff --git a/packages/web/bin/cli.js b/packages/web/bin/cli.js index 9c3e68db..99d05f11 100644 --- a/packages/web/bin/cli.js +++ b/packages/web/bin/cli.js @@ -4616,7 +4616,7 @@ async function main() { await commands[command](options); } -const isCliExecution = isModuleCliExecution(); +const isCliExecution = isModuleCliExecution(process.argv[1], import.meta.url, fs.realpathSync, 'openchamber'); if (isCliExecution) { let isHandlingSigint = false; diff --git a/packages/web/bin/cli.test.js b/packages/web/bin/cli.test.js index 19ab94ba..51715380 100644 --- a/packages/web/bin/cli.test.js +++ b/packages/web/bin/cli.test.js @@ -36,6 +36,15 @@ describe('cli entry detection', () => { expect(isModuleCliExecution('', moduleUrl)).toBe(false); }); + it('returns false when module url is not provided', () => { + expect(isModuleCliExecution(modulePath)).toBe(false); + }); + + it('accepts wrapper binary name fallback when requested', () => { + const wrapperPath = '/home/user/.local/bin/openchamber'; + expect(isModuleCliExecution(wrapperPath, moduleUrl, undefined, 'openchamber')).toBe(true); + }); + it('normalizes direct paths when realpath fails', () => { const unresolvedPath = './packages/web/bin/cli.js'; const realpath = () => {