|
1 | 1 | import { spawnSync } from 'child_process';
|
2 |
| -import { getPackageManagerCommand } from '../../utils/package-manager'; |
| 2 | +import { |
| 3 | + detectPackageManager, |
| 4 | + getPackageManagerCommand, |
| 5 | +} from '../../utils/package-manager'; |
3 | 6 | import { workspaceRoot } from '../../utils/workspace-root';
|
4 | 7 |
|
5 | 8 | export async function mcpHandler(args: any) {
|
6 |
| - const packageManagerCommands = getPackageManagerCommand(); |
| 9 | + const packageManager = detectPackageManager(); |
| 10 | + const packageManagerCommands = getPackageManagerCommand(packageManager); |
7 | 11 |
|
8 | 12 | const passthroughArgs =
|
9 | 13 | args['_'][0] === 'mcp' ? args['_'].slice(1) : args['_'];
|
10 |
| - spawnSync( |
11 |
| - packageManagerCommands.dlx, |
12 |
| - ['-y', 'nx-mcp@latest', ...passthroughArgs], |
13 |
| - { |
14 |
| - stdio: 'inherit', |
15 |
| - cwd: workspaceRoot, |
16 |
| - } |
17 |
| - ); |
| 14 | + |
| 15 | + let dlxArgs: string[]; |
| 16 | + |
| 17 | + if (packageManager === 'npm') { |
| 18 | + dlxArgs = ['-y', 'nx-mcp@latest', ...passthroughArgs]; |
| 19 | + } else if (packageManager === 'yarn') { |
| 20 | + dlxArgs = ['--quiet', 'nx-mcp@latest', ...passthroughArgs]; |
| 21 | + } else if (packageManager === 'bun') { |
| 22 | + dlxArgs = ['--silent', 'nx-mcp@latest', ...passthroughArgs]; |
| 23 | + } else { |
| 24 | + dlxArgs = ['nx-mcp@latest', ...passthroughArgs]; |
| 25 | + } |
| 26 | + |
| 27 | + // For commands that might contain spaces like "pnpm dlx" |
| 28 | + const dlxCommand = packageManagerCommands.dlx.split(' '); |
| 29 | + const executable = dlxCommand[0]; |
| 30 | + const execArgs = [...dlxCommand.slice(1), ...dlxArgs]; |
| 31 | + |
| 32 | + spawnSync(executable, execArgs, { |
| 33 | + stdio: 'inherit', |
| 34 | + cwd: workspaceRoot, |
| 35 | + }); |
18 | 36 | }
|
19 | 37 |
|
20 | 38 | export async function showHelp() {
|
21 |
| - const packageManagerCommands = getPackageManagerCommand(); |
22 |
| - |
23 |
| - const helpOutput = spawnSync( |
24 |
| - packageManagerCommands.dlx, |
25 |
| - ['-y', 'nx-mcp@latest', '--help'], |
26 |
| - { |
27 |
| - cwd: workspaceRoot, |
28 |
| - encoding: 'utf-8', |
29 |
| - } |
30 |
| - ); |
| 39 | + const packageManager = detectPackageManager(); |
| 40 | + const packageManagerCommands = getPackageManagerCommand(packageManager); |
| 41 | + |
| 42 | + let dlxArgs: string[]; |
| 43 | + if (packageManager === 'npm') { |
| 44 | + dlxArgs = ['-y', 'nx-mcp@latest', '--help']; |
| 45 | + } else if (packageManager === 'yarn') { |
| 46 | + dlxArgs = ['--quiet', 'nx-mcp@latest', '--help']; |
| 47 | + } else if (packageManager === 'bun') { |
| 48 | + dlxArgs = ['--silent', 'nx-mcp@latest', '--help']; |
| 49 | + } else { |
| 50 | + dlxArgs = ['nx-mcp@latest', '--help']; |
| 51 | + } |
| 52 | + |
| 53 | + const dlxCommand = packageManagerCommands.dlx.split(' '); |
| 54 | + const executable = dlxCommand[0]; |
| 55 | + const execArgs = [...dlxCommand.slice(1), ...dlxArgs]; |
| 56 | + |
| 57 | + const helpOutput = spawnSync(executable, execArgs, { |
| 58 | + cwd: workspaceRoot, |
| 59 | + encoding: 'utf-8', |
| 60 | + }); |
31 | 61 |
|
32 | 62 | console.log(helpOutput.stdout?.toString().replaceAll('nx-mcp', 'nx mcp'));
|
33 | 63 | }
|
0 commit comments