Skip to content

Commit

Permalink
feat: nx run in command palette and context menu (#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandikbarr committed Apr 23, 2021
1 parent c3f2b92 commit 9b5fd5c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
23 changes: 23 additions & 0 deletions apps/vscode/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"when": "isAngularWorkspace && config.nxConsole.enableGenerateFromContextMenu || isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export function registerCliTaskCommands(
);
});

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',
});
}

0 comments on commit 9b5fd5c

Please sign in to comment.