Skip to content

Commit 21a2f8b

Browse files
authored
fix(vscode): enable command quickpicks to specify arbitrary options (#2129)
1 parent 0cd7787 commit 21a2f8b

File tree

1 file changed

+58
-16
lines changed

1 file changed

+58
-16
lines changed

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

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Option } from '@nx-console/shared/schema';
22
import { QuickPickItem, window } from 'vscode';
33

4-
export class CliTaskFlagQuickPickItem implements QuickPickItem {
4+
class CliTaskFlagQuickPickItem implements QuickPickItem {
55
constructor(
66
readonly flagName: string,
77
readonly detail = '',
@@ -11,6 +11,21 @@ export class CliTaskFlagQuickPickItem implements QuickPickItem {
1111
) {}
1212
}
1313

14+
class ExecuteCommandQuickPickItem implements QuickPickItem {
15+
type = 'execute';
16+
picked = true;
17+
alwaysShow = true;
18+
19+
constructor(readonly label: string, readonly description?: string) {}
20+
}
21+
22+
class CustomOptionsQuickPickItem implements QuickPickItem {
23+
type = 'custom';
24+
alwaysShow = true;
25+
26+
constructor(readonly label: string, readonly description?: string) {}
27+
}
28+
1429
/**
1530
* Returns undefined if the user wants to cancel the command.
1631
* Returns an empty array to run the command without flags.
@@ -19,30 +34,41 @@ export class CliTaskFlagQuickPickItem implements QuickPickItem {
1934
export async function selectFlags(
2035
command: string,
2136
options: Option[],
22-
userSetFlags: { [key: string]: string } = {}
37+
userSetFlags: { [key: string]: string } = {},
38+
customOptions?: string
2339
): Promise<string[] | undefined> {
2440
const flagArray = Object.entries(userSetFlags).map(
2541
([flagName, value]) => `--${flagName}=${value}`
2642
);
43+
if (customOptions) {
44+
flagArray.push(customOptions);
45+
}
2746

2847
const selection = await promptForFlagToSet(
2948
`nx ${command} ${flagArray.join(' ')}`,
3049
options.filter((option) => !userSetFlags[option.name])
3150
);
3251

33-
if (!selection.flag) {
52+
if (selection.execute !== undefined) {
3453
return selection.execute ? flagArray : undefined;
3554
}
3655

37-
const flagValue = await promptForFlagValue(selection.flag);
56+
if (selection.flag) {
57+
const flagValue = await promptForFlagValue(selection.flag);
3858

39-
if (flagValue && flagValue.length > 0) {
40-
userSetFlags[selection.flag.flagName] = flagValue;
41-
} else {
42-
delete userSetFlags[selection.flag.flagName];
59+
if (flagValue && flagValue.length > 0) {
60+
userSetFlags[selection.flag.flagName] = flagValue;
61+
} else {
62+
delete userSetFlags[selection.flag.flagName];
63+
}
64+
} else if (selection.customOptions) {
65+
const customOptionResult = await promptForCustomOptions(customOptions);
66+
if (customOptionResult) {
67+
customOptions = customOptionResult;
68+
}
4369
}
4470

45-
return selectFlags(command, options, userSetFlags);
71+
return selectFlags(command, options, userSetFlags, customOptions);
4672
}
4773

4874
async function promptForFlagToSet(
@@ -51,13 +77,14 @@ async function promptForFlagToSet(
5177
): Promise<{
5278
execute?: boolean;
5379
flag?: CliTaskFlagQuickPickItem;
80+
customOptions?: boolean;
5481
}> {
55-
const flagItems: Array<QuickPickItem | CliTaskFlagQuickPickItem> = [
56-
{
57-
picked: true,
58-
alwaysShow: true,
59-
label: `Execute: ${currentCommand}`,
60-
},
82+
const flagItems: Array<
83+
| CliTaskFlagQuickPickItem
84+
| ExecuteCommandQuickPickItem
85+
| CustomOptionsQuickPickItem
86+
> = [
87+
new ExecuteCommandQuickPickItem(`Execute: ${currentCommand}`),
6188
...options.map((option) => {
6289
const detail =
6390
option.description ??
@@ -70,6 +97,10 @@ async function promptForFlagToSet(
7097
option.isRequired ? 'required' : undefined
7198
);
7299
}),
100+
new CustomOptionsQuickPickItem(
101+
'Custom Options',
102+
'Add any additional command text.'
103+
),
73104
];
74105

75106
const selection = await window.showQuickPick(flagItems, {
@@ -82,7 +113,11 @@ async function promptForFlagToSet(
82113

83114
const flagSelected = Boolean((selection as CliTaskFlagQuickPickItem).option);
84115
if (!flagSelected) {
85-
return { execute: true };
116+
if ((selection as CustomOptionsQuickPickItem).type === 'custom') {
117+
return { customOptions: true };
118+
} else {
119+
return { execute: true };
120+
}
86121
} else {
87122
return {
88123
flag: selection as CliTaskFlagQuickPickItem,
@@ -107,3 +142,10 @@ function promptForFlagValue(flagToSet: CliTaskFlagQuickPickItem) {
107142
});
108143
}
109144
}
145+
146+
function promptForCustomOptions(oldCustomOptions?: string) {
147+
return window.showInputBox({
148+
value: oldCustomOptions,
149+
placeHolder: 'Enter any additional command text.',
150+
});
151+
}

0 commit comments

Comments
 (0)