Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function booleanFlag(flags: Flags, key: string, usage: string): boolean {
export async function handleCliCommand(argv: string[]): Promise<void> {
const rawCommand = argv[0];

if (!rawCommand || rawCommand === '--help') {
if (!rawCommand || rawCommand === '--help' || rawCommand === 'help') {
printUsage();
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ const CLI_SUBCOMMANDS = [
'refs',
'cycles',
'init',
'map'
'map',
'help'
];

// Check if this module is the entry point.
Expand All @@ -108,6 +109,10 @@ const isDirectCliSubcommand =

// Resolve optional bootstrap root with validation handled later in main().
function resolveRootPath(): string | undefined {
if (isDirectRun && directSubcommand === 'help') {
return undefined;
}

const envPath = process.env.CODEBASE_ROOT;

// Walk argv starting at position 2, skip known flags and their values.
Expand Down
47 changes: 46 additions & 1 deletion tests/cli-entrypoint-runtime.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { spawnSync } from 'node:child_process';
import { resolve } from 'node:path';
import { mkdir, mkdtemp, readdir, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { describe, expect, it } from 'vitest';

const root = resolve(import.meta.dirname, '..');
Expand Down Expand Up @@ -54,4 +56,47 @@ describe('CLI entrypoint runtime', () => {
expect(parsed.rootPath).toBe(root);
expect(parsed.hint).toContain('refresh_index');
}, 120_000);

it('prints usage for help from a project containing a directory named help', async () => {
const projectRoot = await mkdtemp(join(tmpdir(), 'cbc-cli-help-'));
const helpDirectory = join(projectRoot, 'help');

const env = { ...process.env };
delete env.CODEBASE_ROOT;

try {
await mkdir(helpDirectory);

const result = spawnSync(
process.execPath,
['--import', import.meta.resolve('tsx'), entrypoint, 'help'],
{
cwd: projectRoot,
env,
encoding: 'utf8',
timeout: 10_000
}
);

expect({
errorCode: result.error?.code,
exitCode: result.status,
helpText: result.stdout.includes('codebase-context <command> [options]'),
commandsSection: result.stdout.includes('Commands:'),
stderr: result.stderr,
projectEntries: await readdir(projectRoot),
helpEntries: await readdir(helpDirectory)
}).toEqual({
errorCode: undefined,
exitCode: 0,
helpText: true,
commandsSection: true,
stderr: '',
projectEntries: ['help'],
helpEntries: []
});
} finally {
await rm(projectRoot, { recursive: true, force: true });
}
}, 70_000);
});
Loading