Skip to content

Commit 107ca57

Browse files
committed
fix(ccusage): run launcher through package bin symlinks
Node test migration replaced the Vitest guard with a direct argv entry comparison. npm and npx execute package bins through node_modules/.bin symlinks, so the wrapper skipped runCli and produced empty output in E2E JSON checks. Compare real paths for the module URL and argv entry, and cover the symlink case in the launcher tests.
1 parent f17dd89 commit 107ca57

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎apps/ccusage/src/cli.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import assert from 'node:assert/strict';
22
import { describe, it, mock } from 'node:test';
3-
import { ensureNativeBinaryExecutable, resolveCliRuntime, resolveNativeBinary } from './cli.ts';
3+
import {
4+
ensureNativeBinaryExecutable,
5+
isMainModule,
6+
resolveCliRuntime,
7+
resolveNativeBinary,
8+
} from './cli.ts';
49

510
void describe(resolveCliRuntime.name, () => {
611
void it('resolves the native package binary for the current supported platform', () => {
@@ -104,4 +109,17 @@ void describe(resolveCliRuntime.name, () => {
104109
);
105110
assert.equal(chmodPath.mock.callCount(), 0);
106111
});
112+
113+
void it('treats package bin symlinks as the main module entry point', () => {
114+
const actual = isMainModule({
115+
argvEntry: '/project/node_modules/.bin/ccusage',
116+
moduleUrl: 'file:///project/node_modules/ccusage/dist/cli.js',
117+
realpathPath: (path) =>
118+
path === '/project/node_modules/.bin/ccusage'
119+
? '/project/node_modules/ccusage/dist/cli.js'
120+
: path,
121+
});
122+
123+
assert.equal(actual, true);
124+
});
107125
});

‎apps/ccusage/src/cli.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
import { spawn } from 'node:child_process';
3-
import { chmodSync, statSync } from 'node:fs';
3+
import { chmodSync, realpathSync, statSync } from 'node:fs';
44
import { createRequire } from 'node:module';
55
import process from 'node:process';
66
import { fileURLToPath } from 'node:url';
@@ -28,6 +28,12 @@ type NativeSpawnResult = {
2828

2929
type NativeSpawner = (command: string, args: string[]) => Promise<NativeSpawnResult>;
3030

31+
type MainModuleOptions = {
32+
argvEntry?: string;
33+
moduleUrl: string;
34+
realpathPath?: (path: string) => string;
35+
};
36+
3137
function getNativePackageName(
3238
platform: string = process.platform,
3339
arch: string = process.arch,
@@ -143,6 +149,23 @@ function ensureNativeBinaryExecutable({
143149
}
144150
}
145151

152+
function isMainModule({
153+
argvEntry = process.argv[1],
154+
moduleUrl,
155+
realpathPath = realpathSync,
156+
}: MainModuleOptions): boolean {
157+
if (argvEntry == null) {
158+
return false;
159+
}
160+
161+
const modulePath = fileURLToPath(moduleUrl);
162+
try {
163+
return realpathPath(modulePath) === realpathPath(argvEntry);
164+
} catch {
165+
return modulePath === argvEntry;
166+
}
167+
}
168+
146169
function createNativeSpawner(): NativeSpawner {
147170
return async (command, args) =>
148171
new Promise((resolve) => {
@@ -193,8 +216,8 @@ async function runCli(
193216
return result.status ?? 1;
194217
}
195218

196-
if (fileURLToPath(import.meta.url) === process.argv[1]) {
219+
if (isMainModule({ moduleUrl: import.meta.url })) {
197220
process.exitCode = await runCli(process.argv.slice(2));
198221
}
199222

200-
export { ensureNativeBinaryExecutable, resolveCliRuntime, resolveNativeBinary };
223+
export { ensureNativeBinaryExecutable, isMainModule, resolveCliRuntime, resolveNativeBinary };

0 commit comments

Comments
 (0)