Skip to content

Commit 0cd7787

Browse files
authored
fix(vscode): adapt cli task logic to handle affected & other commands correctly (#2127)
1 parent 7832af9 commit 0cd7787

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

libs/vscode/nx-cli-quickpicks/src/lib/select-affected-flags.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export async function selectAffectedFlags(target: string): Promise<{
1717
};
1818
}
1919
default: {
20-
const positional = `--target=${target}`;
2120
return {
2221
command: 'affected',
23-
positional,
24-
flags: await selectFlags(`affected ${positional}`, AFFECTED_OPTIONS),
22+
flags: await selectFlags(`affected`, AFFECTED_OPTIONS, {
23+
target,
24+
}),
2525
};
2626
}
2727
}

libs/vscode/nx-commands-view/src/lib/nx-commands-provider.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import {
2-
getNxWorkspacePath,
3-
GlobalConfigurationStore,
4-
} from '@nx-console/vscode/configuration';
5-
import { getNxWorkspace } from '@nx-console/vscode/nx-workspace';
1+
import { GlobalConfigurationStore } from '@nx-console/vscode/configuration';
2+
import { onWorkspaceRefreshed } from '@nx-console/vscode/lsp-client';
3+
import { CliTaskProvider } from '@nx-console/vscode/tasks';
64
import {
75
AbstractTreeProvider,
8-
getShellExecutionForConfig,
96
logAndShowTaskCreationError,
107
} from '@nx-console/vscode/utils';
11-
import { commands, ExtensionContext, Task, tasks, TaskScope } from 'vscode';
8+
import { commands, ExtensionContext } from 'vscode';
129
import { NxCommandConfig, NxCommandsTreeItem } from './nx-commands-tree-item';
13-
import { onWorkspaceRefreshed } from '@nx-console/vscode/lsp-client';
14-
import { importNxPackagePath } from '@nx-console/shared/npm';
1510

1611
export const EXECUTE_ARBITRARY_COMMAND = 'nxConsole.executeArbitraryCommand';
1712

@@ -77,31 +72,29 @@ export class NxCommandsTreeProvider extends AbstractTreeProvider<NxCommandsTreeI
7772
}
7873

7974
async executeArbitraryCommand(command: string) {
80-
const prefixedCommand = command.startsWith('nx ')
81-
? command
82-
: `nx ${command}`;
83-
const workspacePath = getNxWorkspacePath();
84-
const isEncapsulatedNx =
85-
(await getNxWorkspace())?.isEncapsulatedNx ?? false;
86-
const { detectPackageManager } = await importNxPackagePath<
87-
typeof import('nx/src/devkit-exports')
88-
>(workspacePath, 'src/devkit-exports');
89-
const pkgManager = detectPackageManager(workspacePath);
75+
let _command: string;
76+
let positional: string | undefined;
77+
78+
const commandBeforeFlags = command.split(' -')[0];
79+
if (commandBeforeFlags.includes(' ')) {
80+
_command = commandBeforeFlags.split(' ')[0];
81+
positional = commandBeforeFlags.replace(_command, '').trim();
82+
} else {
83+
_command = commandBeforeFlags;
84+
positional = undefined;
85+
}
86+
87+
const flags = command
88+
.replace(commandBeforeFlags, '')
89+
.split(' ')
90+
.filter(Boolean);
9091

9192
try {
92-
const task = new Task(
93-
{ type: 'nx' },
94-
TaskScope.Workspace,
95-
prefixedCommand,
96-
pkgManager,
97-
await getShellExecutionForConfig({
98-
cwd: workspacePath,
99-
displayCommand: prefixedCommand,
100-
encapsulatedNx: isEncapsulatedNx,
101-
workspacePath,
102-
})
103-
);
104-
tasks.executeTask(task);
93+
CliTaskProvider.instance.executeTask({
94+
command: _command,
95+
positional,
96+
flags,
97+
});
10598
} catch (e) {
10699
logAndShowTaskCreationError(e);
107100
return;

libs/vscode/tasks/src/lib/cli-task-definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface CliTaskDefinition {
2-
positional: string;
2+
positional?: string;
33
command: string;
44
flags: Array<string>;
55
cwd?: string;

libs/vscode/tasks/src/lib/cli-task-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class CliTaskProvider implements TaskProvider {
8282
}
8383

8484
let task;
85-
const positionals = definition.positional.match(
85+
const positionals = definition.positional?.match(
8686
WORKSPACE_GENERATOR_NAME_REGEX
8787
);
8888
try {

libs/vscode/tasks/src/lib/cli-task.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ export class CliTask extends Task {
8181

8282
function getArgs(definition: CliTaskDefinition) {
8383
const { positional, command, flags } = definition;
84-
switch (command) {
85-
case 'add':
86-
case 'build':
87-
case 'lint':
88-
case 'generate':
89-
case 'run':
90-
case 'serve':
91-
case 'test':
92-
return [command, positional, ...flags];
93-
default:
94-
return ['run', `${positional}:${command}`, ...flags];
95-
}
84+
const args = [command, positional, ...flags];
85+
return args.filter((v) => v !== undefined && v !== null);
9686
}

0 commit comments

Comments
 (0)