Skip to content

Commit 9cc569f

Browse files
committed
Fix CLI entry detection for npx
Ensure the CLI runs when invoked via npx binary names or paths
1 parent 4b4b550 commit 9cc569f

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

cli.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, afterEach, test, expect, vi } from 'vitest';
2-
import cli from './src/cli.js';
2+
import path from 'path';
3+
import { pathToFileURL } from 'url';
4+
import cli, { shouldRunAsCli } from './src/cli.js';
35

46
const originalArgv = process.argv;
57

@@ -78,3 +80,38 @@ describe('cli', () => {
7880
expect(errorMock).toHaveBeenCalled();
7981
});
8082
});
83+
84+
describe('shouldRunAsCli', () => {
85+
test('returns true for direct absolute entry path', () => {
86+
const entryPath = path.join(process.cwd(), 'dist', 'index.js');
87+
const importMetaUrl = pathToFileURL(entryPath).href;
88+
89+
expect(shouldRunAsCli(importMetaUrl, entryPath)).toBe(true);
90+
});
91+
92+
test('returns true for relative entry path', () => {
93+
const entryPath = path.join(process.cwd(), 'dist', 'index.js');
94+
const importMetaUrl = pathToFileURL(entryPath).href;
95+
const relativePath = path.relative(process.cwd(), entryPath);
96+
97+
expect(shouldRunAsCli(importMetaUrl, relativePath)).toBe(true);
98+
});
99+
100+
test('returns true for binary name', () => {
101+
const importMetaUrl = pathToFileURL('/tmp/fake/index.js').href;
102+
103+
expect(shouldRunAsCli(importMetaUrl, 'go-git-it')).toBe(true);
104+
});
105+
106+
test('returns false for unrelated argv1', () => {
107+
const importMetaUrl = pathToFileURL('/tmp/fake/index.js').href;
108+
109+
expect(shouldRunAsCli(importMetaUrl, '/usr/bin/node')).toBe(false);
110+
});
111+
112+
test('returns false when argv1 is missing', () => {
113+
const importMetaUrl = pathToFileURL('/tmp/fake/index.js').href;
114+
115+
expect(shouldRunAsCli(importMetaUrl, undefined)).toBe(false);
116+
});
117+
});

src/cli.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
#!/usr/bin/env node
22

3+
import path from 'path';
4+
import { fileURLToPath } from 'url';
5+
36
type GoGitIt = (gitURL: string, outputDirectory?: string) => Promise<void>;
47

8+
export function shouldRunAsCli(
9+
importMetaUrl: string,
10+
argv1: string | undefined,
11+
): boolean {
12+
if (!argv1) {
13+
return false;
14+
}
15+
16+
const entryPath = fileURLToPath(importMetaUrl);
17+
const argvPath = path.isAbsolute(argv1)
18+
? argv1
19+
: path.resolve(process.cwd(), argv1);
20+
21+
if (argvPath === entryPath) {
22+
return true;
23+
}
24+
25+
const base = path.basename(argv1);
26+
return base === 'go-git-it' || base === 'go-git-it.js' || base === 'go-git-it.cjs';
27+
}
28+
529
/**
630
* Enhanced CLI with proper error handling and help text
731
*/

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
createDirectory,
1919
cleanupTempDirectory,
2020
} from './utils/cross-platform.js';
21-
import cli from './cli.js';
21+
import cli, { shouldRunAsCli } from './cli.js';
2222
import path from 'path';
2323

2424
/**
@@ -100,8 +100,8 @@ async function goGitIt(
100100
}
101101
}
102102

103-
// Execute CLI if requested
104-
if (import.meta.url === `file://${process.argv[1]}`) {
103+
// Execute CLI when invoked as a binary
104+
if (shouldRunAsCli(import.meta.url, process.argv[1])) {
105105
cli(goGitIt);
106106
}
107107

src/types/progress.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'progress';

0 commit comments

Comments
 (0)