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
5 changes: 5 additions & 0 deletions .changeset/cuddly-clubs-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bomb.sh/tab': patch
---

npm exec support
86 changes: 54 additions & 32 deletions bin/package-manager-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ async function checkCliHasCompletions(
packageManager: string
): Promise<boolean> {
try {
debugLog(`Checking if ${cliName} has completions via ${packageManager}`);
const command = `${packageManager} ${cliName} complete --`;
const result = execSync(command, {
const result = execSync(`${cliName} complete --`, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
timeout: 1000,
});
const hasCompletions = !!result.trim();
debugLog(`${cliName} supports completions: ${hasCompletions}`);
return hasCompletions;
} catch (error) {
debugLog(`Error checking completions for ${cliName}:`, error);
if (result.trim()) return true;
} catch {}

try {
const result = execSync(`${packageManager} ${cliName} complete --`, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
timeout: 1000,
});
return !!result.trim();
} catch {
return false;
}
}
Expand All @@ -33,24 +37,35 @@ async function getCliCompletions(
packageManager: string,
args: string[]
): Promise<string[]> {
const completeArgs = args.map((arg) =>
arg.includes(' ') ? `"${arg}"` : arg
);

try {
const completeArgs = args.map((arg) =>
arg.includes(' ') ? `"${arg}"` : arg
const result = execSync(
`${cliName} complete -- ${completeArgs.join(' ')}`,
{
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
timeout: 1000,
}
);
const completeCommand = `${packageManager} ${cliName} complete -- ${completeArgs.join(' ')}`;
debugLog(`Getting completions with command: ${completeCommand}`);

const result = execSync(completeCommand, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
timeout: 1000,
});
if (result.trim()) {
return result.trim().split('\n').filter(Boolean);
}
} catch {}

const completions = result.trim().split('\n').filter(Boolean);
debugLog(`Got ${completions.length} completions from ${cliName}`);
return completions;
} catch (error) {
debugLog(`Error getting completions from ${cliName}:`, error);
try {
const result = execSync(
`${packageManager} ${cliName} complete -- ${completeArgs.join(' ')}`,
{
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
timeout: 1000,
}
);
return result.trim().split('\n').filter(Boolean);
} catch {
return [];
}
}
Expand All @@ -69,11 +84,18 @@ export class PackageManagerCompletion extends RootCommand {
this.packageManager = packageManager;
}

// Enhanced parse method with package manager logic
private stripPackageManagerCommands(args: string[]): string[] {
if (args.length === 0) return args;
const execCommands = ['exec', 'x', 'run', 'dlx'];
if (execCommands.includes(args[0])) return args.slice(1);
return args;
}

async parse(args: string[]) {
// Handle package manager completions first
if (args.length >= 1 && args[0].trim() !== '') {
const potentialCliName = args[0];
const normalizedArgs = this.stripPackageManagerCommands(args);

if (normalizedArgs.length >= 1 && normalizedArgs[0].trim() !== '') {
const potentialCliName = normalizedArgs[0];
const knownCommands = [...this.commands.keys()];

if (!knownCommands.includes(potentialCliName)) {
Expand All @@ -83,33 +105,33 @@ export class PackageManagerCompletion extends RootCommand {
);

if (hasCompletions) {
const cliArgs = args.slice(1);
const cliArgs = normalizedArgs.slice(1);
const suggestions = await getCliCompletions(
potentialCliName,
this.packageManager,
cliArgs
);

if (suggestions.length > 0) {
// Print completions directly in the same format as the core library
debugLog(
`Returning ${suggestions.length} completions for ${potentialCliName}`
);
for (const suggestion of suggestions) {
if (suggestion.startsWith(':')) continue;

if (suggestion.includes('\t')) {
const [value, description] = suggestion.split('\t');
console.log(`${value}\t${description}`);
} else {
console.log(suggestion);
}
}
console.log(':4'); // Shell completion directive (NoFileComp)
console.log(':4');
return;
}
}
}
}

// Fall back to regular completion logic (shows basic package manager commands)
return super.parse(args);
}
}
Loading