Skip to content
Draft
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
64 changes: 50 additions & 14 deletions libs/vscode/nx-cli-quickpicks/src/lib/select-flags.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Option } from '@nx-console/shared-schema';
import { QuickPickItem, window } from 'vscode';
import { QuickPickItem, window, env } from 'vscode';

class CliTaskFlagQuickPickItem implements QuickPickItem {
constructor(
readonly flagName: string,
readonly detail = '',
readonly option: Option,
readonly label: string,
readonly description?: string
readonly description?: string,
) {}
}

Expand All @@ -16,14 +16,31 @@ class ExecuteCommandQuickPickItem implements QuickPickItem {
picked = true;
alwaysShow = true;

constructor(readonly label: string, readonly description?: string) {}
constructor(
readonly label: string,
readonly description?: string,
) {}
}

class CopyCommandQuickPickItem implements QuickPickItem {
type = 'copy';
picked = true;
alwaysShow = true;

constructor(
readonly label: string,
readonly description?: string,
) {}
}

class CustomOptionsQuickPickItem implements QuickPickItem {
type = 'custom';
alwaysShow = true;

constructor(readonly label: string, readonly description?: string) {}
constructor(
readonly label: string,
readonly description?: string,
) {}
}

/**
Expand All @@ -35,24 +52,29 @@ export async function selectFlags(
command: string,
options: Option[],
userSetFlags: { [key: string]: string } = {},
customOptions?: string
customOptions?: string,
): Promise<string[] | undefined> {
const flagArray = Object.entries(userSetFlags).map(
([flagName, value]) => `--${flagName}=${value}`
([flagName, value]) => `--${flagName}=${value}`,
);
if (customOptions) {
flagArray.push(customOptions);
}

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

if (selection.execute !== undefined) {
return selection.execute ? flagArray : undefined;
}

if (selection.copy !== undefined) {
await copyCommandToClipboard(`nx ${command} ${flagArray.join(' ')}`);
return undefined;
}

if (selection.flag) {
const flagValue = await promptForFlagValue(selection.flag);

Expand All @@ -73,9 +95,10 @@ export async function selectFlags(

async function promptForFlagToSet(
currentCommand: string,
options: Option[]
options: Option[],
): Promise<{
execute?: boolean;
copy?: boolean;
flag?: CliTaskFlagQuickPickItem;
customOptions?: boolean;
}> {
Expand All @@ -94,12 +117,13 @@ async function promptForFlagToSet(
detail,
option,
`${option.name}`,
option.isRequired ? 'required' : undefined
option.isRequired ? 'required' : undefined,
);
}),
new CopyCommandQuickPickItem(`Copy to clipboard: ${currentCommand}`),
new CustomOptionsQuickPickItem(
'Custom Options',
'Add any additional command text.'
'Add any additional command text.',
),
];

Expand All @@ -113,10 +137,16 @@ async function promptForFlagToSet(

const flagSelected = Boolean((selection as CliTaskFlagQuickPickItem).option);
if (!flagSelected) {
if ((selection as CustomOptionsQuickPickItem).type === 'custom') {
return { customOptions: true };
} else {
return { execute: true };
switch ((selection as CustomOptionsQuickPickItem).type) {
case 'custom': {
return { customOptions: true };
}
case 'copy': {
return { copy: true };
}
default: {
return { execute: true };
}
}
} else {
return {
Expand All @@ -125,6 +155,12 @@ async function promptForFlagToSet(
}
}

async function copyCommandToClipboard(command: string) {
await env.clipboard.writeText(command);

window.showInformationMessage(`Copied command to clipboard: ${command}`);
}

function promptForFlagValue(flagToSet: CliTaskFlagQuickPickItem) {
const placeHolder = `--${flagToSet.flagName}=...`;
if (flagToSet.option.type === 'boolean') {
Expand Down
Loading