|
| 1 | +// TODO: i do not see any completion functionality in this file. nothing is being provided for the defined commands of these package managers. this is a blocker for release. every each of them should be handled. |
| 2 | +import { Completion } from '../src/index.js'; |
| 3 | +import { execSync } from 'child_process'; |
| 4 | + |
| 5 | +const DEBUG = false; // for debugging purposes |
| 6 | + |
| 7 | +function debugLog(...args: any[]) { |
| 8 | + if (DEBUG) { |
| 9 | + console.error('[DEBUG]', ...args); |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +async function checkCliHasCompletions( |
| 14 | + cliName: string, |
| 15 | + packageManager: string |
| 16 | +): Promise<boolean> { |
| 17 | + try { |
| 18 | + debugLog(`Checking if ${cliName} has completions via ${packageManager}`); |
| 19 | + const command = `${packageManager} ${cliName} complete --`; |
| 20 | + const result = execSync(command, { |
| 21 | + encoding: 'utf8', |
| 22 | + stdio: ['pipe', 'pipe', 'ignore'], |
| 23 | + timeout: 1000, // AMIR: we still havin issues with this, it still hangs if a cli doesn't have completions. longer timeout needed for shell completion system (shell → node → package manager → cli) |
| 24 | + }); |
| 25 | + const hasCompletions = !!result.trim(); |
| 26 | + debugLog(`${cliName} supports completions: ${hasCompletions}`); |
| 27 | + return hasCompletions; |
| 28 | + } catch (error) { |
| 29 | + debugLog(`Error checking completions for ${cliName}:`, error); |
| 30 | + return false; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function getCliCompletions( |
| 35 | + cliName: string, |
| 36 | + packageManager: string, |
| 37 | + args: string[] |
| 38 | +): Promise<string[]> { |
| 39 | + try { |
| 40 | + const completeArgs = args.map((arg) => |
| 41 | + arg.includes(' ') ? `"${arg}"` : arg |
| 42 | + ); |
| 43 | + const completeCommand = `${packageManager} ${cliName} complete -- ${completeArgs.join(' ')}`; |
| 44 | + debugLog(`Getting completions with command: ${completeCommand}`); |
| 45 | + |
| 46 | + const result = execSync(completeCommand, { |
| 47 | + encoding: 'utf8', |
| 48 | + stdio: ['pipe', 'pipe', 'ignore'], |
| 49 | + timeout: 1000, // same: longer timeout needed for shell completion system (shell → node → package manager → cli) |
| 50 | + }); |
| 51 | + |
| 52 | + const completions = result.trim().split('\n').filter(Boolean); |
| 53 | + debugLog(`Got ${completions.length} completions from ${cliName}`); |
| 54 | + return completions; |
| 55 | + } catch (error) { |
| 56 | + debugLog(`Error getting completions from ${cliName}:`, error); |
| 57 | + return []; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export function setupCompletionForPackageManager( |
| 62 | + packageManager: string, |
| 63 | + completion: Completion |
| 64 | +) { |
| 65 | + if (packageManager === 'pnpm') { |
| 66 | + setupPnpmCompletions(completion); |
| 67 | + } else if (packageManager === 'npm') { |
| 68 | + setupNpmCompletions(completion); |
| 69 | + } else if (packageManager === 'yarn') { |
| 70 | + setupYarnCompletions(completion); |
| 71 | + } else if (packageManager === 'bun') { |
| 72 | + setupBunCompletions(completion); |
| 73 | + } |
| 74 | + |
| 75 | + // TODO: the core functionality of tab should have nothing related to package managers. even though completion is not there anymore, but this is something to consider. |
| 76 | + completion.setPackageManager(packageManager); |
| 77 | +} |
| 78 | + |
| 79 | +export function setupPnpmCompletions(completion: Completion) { |
| 80 | + completion.addCommand('add', 'Install a package', [], async () => []); |
| 81 | + completion.addCommand('remove', 'Remove a package', [], async () => []); |
| 82 | + completion.addCommand( |
| 83 | + 'install', |
| 84 | + 'Install all dependencies', |
| 85 | + [], |
| 86 | + async () => [] |
| 87 | + ); |
| 88 | + // TODO: empty functions should be replaced with noop functions rather than creating that many empty functions |
| 89 | + completion.addCommand('update', 'Update packages', [], async () => []); |
| 90 | + completion.addCommand('exec', 'Execute a command', [], async () => []); |
| 91 | + completion.addCommand('run', 'Run a script', [], async () => []); |
| 92 | + completion.addCommand('publish', 'Publish package', [], async () => []); |
| 93 | + completion.addCommand('test', 'Run tests', [], async () => []); |
| 94 | + completion.addCommand('build', 'Build project', [], async () => []); |
| 95 | +} |
| 96 | + |
| 97 | +export function setupNpmCompletions(completion: Completion) { |
| 98 | + completion.addCommand('install', 'Install a package', [], async () => []); |
| 99 | + completion.addCommand('uninstall', 'Uninstall a package', [], async () => []); |
| 100 | + completion.addCommand('run', 'Run a script', [], async () => []); |
| 101 | + completion.addCommand('test', 'Run tests', [], async () => []); |
| 102 | + completion.addCommand('publish', 'Publish package', [], async () => []); |
| 103 | + completion.addCommand('update', 'Update packages', [], async () => []); |
| 104 | + completion.addCommand('start', 'Start the application', [], async () => []); |
| 105 | + completion.addCommand('build', 'Build project', [], async () => []); |
| 106 | +} |
| 107 | + |
| 108 | +export function setupYarnCompletions(completion: Completion) { |
| 109 | + completion.addCommand('add', 'Add a package', [], async () => []); |
| 110 | + completion.addCommand('remove', 'Remove a package', [], async () => []); |
| 111 | + completion.addCommand('run', 'Run a script', [], async () => []); |
| 112 | + completion.addCommand('test', 'Run tests', [], async () => []); |
| 113 | + completion.addCommand('publish', 'Publish package', [], async () => []); |
| 114 | + completion.addCommand('install', 'Install dependencies', [], async () => []); |
| 115 | + completion.addCommand('build', 'Build project', [], async () => []); |
| 116 | +} |
| 117 | + |
| 118 | +export function setupBunCompletions(completion: Completion) { |
| 119 | + completion.addCommand('add', 'Add a package', [], async () => []); |
| 120 | + completion.addCommand('remove', 'Remove a package', [], async () => []); |
| 121 | + completion.addCommand('run', 'Run a script', [], async () => []); |
| 122 | + completion.addCommand('test', 'Run tests', [], async () => []); |
| 123 | + completion.addCommand('install', 'Install dependencies', [], async () => []); |
| 124 | + completion.addCommand('update', 'Update packages', [], async () => []); |
| 125 | + completion.addCommand('build', 'Build project', [], async () => []); |
| 126 | +} |
0 commit comments