Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nx run in command palette and context menu #1065

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions apps/vscode/src/package.json
Expand Up @@ -50,6 +50,11 @@
"when": "isGenerateFromContextMenuEnabled",
"command": "nx.generate.ui.fileexplorer",
"group": "explorerContext"
},
{
"when": "isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "nx.run.fileexplorer",
"group": "explorerContext"
}
],
"view/title": [
Expand Down Expand Up @@ -81,6 +86,10 @@
"command": "nx.generate.ui.fileexplorer",
"when": "false"
},
{
"command": "nx.run.fileexplorer",
"when": "false"
},
{
"command": "ng.lint",
"when": "isAngularWorkspace"
Expand Down Expand Up @@ -165,6 +174,10 @@
"command": "nx.run-many",
"when": "isNxWorkspace"
},
{
"command": "nx.run",
"when": "isNxWorkspace"
},
{
"command": "nx.affected",
"when": "isNxWorkspace"
Expand Down Expand Up @@ -362,6 +375,11 @@
"title": "run-many",
"command": "nx.run-many"
},
{
"category": "Nx",
"title": "run",
"command": "nx.run"
},
{
"category": "Nx",
"title": "affected",
Expand Down Expand Up @@ -451,6 +469,11 @@
"category": "Nx",
"title": "Nx generate (ui)",
"command": "nx.generate.ui.fileexplorer"
},
{
"category": "Nx",
"title": "Nx run",
"command": "nx.run.fileexplorer"
}
],
"configuration": {
Expand Down
61 changes: 50 additions & 11 deletions libs/vscode/tasks/src/lib/cli-task-commands.ts
Expand Up @@ -49,6 +49,13 @@ export function registerCliTaskCommands(
);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really sure of the distinction between nx-task-commands and cli-task-commands. It feels like this cli-task-commands was originally angular CLI support vs Nx CLI. I started out putting the run command support in nx-task-commands, but the flow fit in a little better here with the other CLI commands and it has all the support for getting the executor/builder definition and prompting for flags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, I noticed that as well. We'll eventually consolidate the two and just use Nx.

commands.registerCommand('nx.run', () =>
selectCliCommandAndPromptForFlags('run')
);
commands.registerCommand(`nx.run.fileexplorer`, (uri: Uri) =>
selectCliCommandAndPromptForFlags('run', getCliProjectFromUri(uri))
);

commands.registerCommand(`ng.generate`, () =>
selectSchematicAndPromptForFlags()
);
Expand Down Expand Up @@ -98,19 +105,31 @@ function selectCliCommandAndShowUi(
);
}

async function selectCliCommandAndPromptForFlags(command: string) {
async function selectCliCommandAndPromptForFlags(command: string, projectName?: string) {
const { validWorkspaceJson, json, workspaceType } = verifyWorkspace();

const selection = validWorkspaceJson
? await selectCliProject(command, json)
: undefined;
if (!selection) {
return; // Do not execute a command if user clicks out of VSCode UI.
if (!projectName) {
const selection = validWorkspaceJson
? await selectCliProject(command, json)
: undefined;
if (!selection) {
return; // Do not execute a command if user clicks out of VSCode UI.
}
projectName = selection.projectName;
}

let target = command;
const isRunCommand = command === 'run';
if (isRunCommand) {
target = (await selectCliTarget(Object.keys(json.projects[projectName].architect || {}))) as string;
if (!target) {
return;
}
}

const builderDefinition = await verifyBuilderDefinition(
selection.projectName,
command,
projectName,
target,
json
);
const {
Expand All @@ -136,14 +155,18 @@ async function selectCliCommandAndPromptForFlags(command: string) {
}

const flags = await selectFlags(
`${command} ${selection.projectName}`,
isRunCommand
? `${command} ${projectName}:${target}`
: `${command} ${projectName}`,
options,
workspaceType
);

if (flags !== undefined) {
cliTaskProvider.executeTask({
positional: selection.projectName,
positional: isRunCommand
? `${projectName}:${target}`
: projectName,
command,
flags,
});
Expand Down Expand Up @@ -181,12 +204,20 @@ async function selectSchematicAndPromptForFlags() {
}
}

export function getCliProjectFromUri(uri: Uri): string | undefined {
const project = cliTaskProvider.projectForPath(uri.fsPath);
return project?.name;
}

export function selectCliProject(command: string, json: any) {
const items = cliTaskProvider
.getProjectEntries(json)
.filter(([_, { architect }]) => Boolean(architect))
.flatMap(([project, { architect }]) => ({ project, architect }))
.filter(({ architect }) => Boolean(architect && architect[command]))
.filter(
({ architect }) =>
Boolean(architect && architect[command]) || command === 'run'
)
.map(
({ project, architect }) =>
new CliTaskQuickPickItem(
Expand All @@ -209,3 +240,11 @@ export function selectCliProject(command: string, json: any) {
placeHolder: `Project to ${command}`,
});
}

async function selectCliTarget(
targets: string[]
): Promise<string | undefined> {
return window.showQuickPick(targets, {
placeHolder: 'Target to run',
});
}