From 8fed994c40fc30f65ce4005cbcd8e12416e2e5fc Mon Sep 17 00:00:00 2001 From: Max Kless Date: Fri, 4 Nov 2022 11:10:36 +0100 Subject: [PATCH] execute arbitrary commands --- apps/vscode-e2e/.eslintrc.json | 7 ++- apps/vscode/src/package.json | 16 +++++-- .../src/lib/global-configuration-store.ts | 13 ++++- .../src/lib/init-nx-commands-view.ts | 9 +++- .../src/lib/nx-commands-provider.ts | 47 ++++++++++++++++--- .../src/lib/nx-commands-tree-item.ts | 10 +++- 6 files changed, 89 insertions(+), 13 deletions(-) diff --git a/apps/vscode-e2e/.eslintrc.json b/apps/vscode-e2e/.eslintrc.json index 3a5e8548f8..4d8c166bb6 100644 --- a/apps/vscode-e2e/.eslintrc.json +++ b/apps/vscode-e2e/.eslintrc.json @@ -1,6 +1,11 @@ { "extends": ["../../.eslintrc.json"], - "ignorePatterns": ["!**/*", ".wdio-vscode-service/**/*"], + "ignorePatterns": [ + "!**/*", + ".wdio-vscode-service/**/*", + "./testworkspaces/**/*", + "**/node_modules/**/*" + ], "overrides": [ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], diff --git a/apps/vscode/src/package.json b/apps/vscode/src/package.json index 1eff77064b..b3442165a1 100644 --- a/apps/vscode/src/package.json +++ b/apps/vscode/src/package.json @@ -116,6 +116,11 @@ "command": "nxConsole.refreshWorkspace", "when": "view == nxRunTarget", "group": "navigation" + }, + { + "command": "nxConsole.editCommonCommands", + "when": "view == nxCommands", + "group": "navigation" } ], "view/item/context": [ @@ -376,6 +381,11 @@ "category": "Nx", "icon": "$(refresh)" }, + { + "command": "nxConsole.editCommonCommands", + "title": "Edit Common Commands", + "icon": "$(pencil)" + }, { "command": "nxConsole.editWorkspaceJson", "title": "Edit workspace definition", @@ -712,10 +722,10 @@ "affected:test", "list", "migrate", - "Add Dependency", - "Add Dev Dependency" + "add-dependency", + "add-dev-dependency" ], - "description": "Common Nx commands that will be available in the sidebar view." + "markdownDescription": "Common Nx commands that will be available in the sidebar view. There are three categories of commands you can specify here: \n - Arbitrary Nx commands, like `build:example-app` or `nx run my-lib:test` (note that you can omit the prefixed `nx`) \n - Nx commands that are available through Nx Console, like `run-many`. They will be executed using the Nx Console UI. \n - `add-depedency` and `add-dev-dependency` commands will be executed using the Nx Console UI for adding dependencies to the workspace." }, "nxConsole.enableGenerateFromContextMenu": { "type": "boolean", diff --git a/libs/vscode/configuration/src/lib/global-configuration-store.ts b/libs/vscode/configuration/src/lib/global-configuration-store.ts index efce58048e..c0f462345a 100644 --- a/libs/vscode/configuration/src/lib/global-configuration-store.ts +++ b/libs/vscode/configuration/src/lib/global-configuration-store.ts @@ -1,6 +1,7 @@ import { ConfigurationTarget, ExtensionContext, + EventEmitter, workspace, Memento, } from 'vscode'; @@ -30,7 +31,15 @@ export class GlobalConfigurationStore implements Store { return CONFIG_STORE; } - private constructor(private readonly state: Memento) {} + private readonly _onConfigurationChange: EventEmitter = + new EventEmitter(); + readonly onConfigurationChange = this._onConfigurationChange.event; + + private constructor(private readonly state: Memento) { + workspace.onDidChangeConfiguration(() => { + this._onConfigurationChange.fire(); + }); + } get(key: T): GlobalConfig[T] | null; get(key: GlobalConfigKeys): T | null; @@ -42,10 +51,12 @@ export class GlobalConfigurationStore implements Store { set(key: GlobalConfigKeys, value: T): void { this.storage(key).update(key, value); + this._onConfigurationChange.fire(); } delete(key: GlobalConfigKeys): void { this.storage(key).update(key, undefined); + this._onConfigurationChange.fire(); } storage(key: GlobalConfigKeys): VSCState { diff --git a/libs/vscode/nx-commands-view/src/lib/init-nx-commands-view.ts b/libs/vscode/nx-commands-view/src/lib/init-nx-commands-view.ts index 5522c76e90..a038f42ece 100644 --- a/libs/vscode/nx-commands-view/src/lib/init-nx-commands-view.ts +++ b/libs/vscode/nx-commands-view/src/lib/init-nx-commands-view.ts @@ -1,9 +1,16 @@ import { NxCommandsTreeProvider } from './nx-commands-provider'; -import { ExtensionContext, window } from 'vscode'; +import { commands, ExtensionContext, window } from 'vscode'; export function initNxCommandsView(context: ExtensionContext) { const nxCommandsTreeView = window.createTreeView('nxCommands', { treeDataProvider: new NxCommandsTreeProvider(context), }); + commands.registerCommand('nxConsole.editCommonCommands', () => { + commands.executeCommand( + 'workbench.action.openSettings', + 'nxConsole.commonNxCommands' + ); + }); + context.subscriptions.push(nxCommandsTreeView); } diff --git a/libs/vscode/nx-commands-view/src/lib/nx-commands-provider.ts b/libs/vscode/nx-commands-view/src/lib/nx-commands-provider.ts index 8a17e7f730..43c596f2ff 100644 --- a/libs/vscode/nx-commands-view/src/lib/nx-commands-provider.ts +++ b/libs/vscode/nx-commands-view/src/lib/nx-commands-provider.ts @@ -1,11 +1,26 @@ -import { AbstractTreeProvider } from '@nx-console/vscode/utils'; -import { NxCommandConfig, NxCommandsTreeItem } from './nx-commands-tree-item'; -import { ExtensionContext, commands } from 'vscode'; +import { detectPackageManager } from '@nrwl/devkit'; import { GlobalConfigurationStore } from '@nx-console/vscode/configuration'; +import { getNxWorkspace } from '@nx-console/vscode/nx-workspace'; +import { + AbstractTreeProvider, + getShellExecutionForConfig, + getWorkspacePath, +} from '@nx-console/vscode/utils'; +import { commands, ExtensionContext, Task, tasks, TaskScope } from 'vscode'; +import { NxCommandConfig, NxCommandsTreeItem } from './nx-commands-tree-item'; + +export const EXECUTE_ARBITRARY_COMMAND = 'nxConsole.executeArbitraryCommand'; export class NxCommandsTreeProvider extends AbstractTreeProvider { constructor(private readonly context: ExtensionContext) { super(); + commands.registerCommand( + EXECUTE_ARBITRARY_COMMAND, + this.executeArbirtraryCommand + ); + GlobalConfigurationStore.instance.onConfigurationChange(() => + this.refresh() + ); } getParent(_: NxCommandsTreeItem) { @@ -25,17 +40,17 @@ export class NxCommandsTreeProvider extends AbstractTreeProvider new NxCommandsTreeItem(c, this.context.extensionPath) ); } + + async executeArbirtraryCommand(command: string) { + const prefixedCommand = command.startsWith('nx ') + ? command + : `nx ${command}`; + const { workspacePath, workspaceType } = await getNxWorkspace(); + const pkgManager = detectPackageManager(workspacePath); + + const task = new Task( + { type: workspaceType }, + TaskScope.Workspace, + prefixedCommand, + pkgManager, + getShellExecutionForConfig({ + cwd: workspacePath, + displayCommand: prefixedCommand, + }) + ); + tasks.executeTask(task); + } } diff --git a/libs/vscode/nx-commands-view/src/lib/nx-commands-tree-item.ts b/libs/vscode/nx-commands-view/src/lib/nx-commands-tree-item.ts index 0a35407cb9..c9900164ad 100644 --- a/libs/vscode/nx-commands-view/src/lib/nx-commands-tree-item.ts +++ b/libs/vscode/nx-commands-view/src/lib/nx-commands-tree-item.ts @@ -1,5 +1,6 @@ import { TreeItem, TreeItemCollapsibleState, Uri } from 'vscode'; import { join } from 'path'; +import { EXECUTE_ARBITRARY_COMMAND } from './nx-commands-provider'; export type NxCommandConfig = | { @@ -27,7 +28,14 @@ export class NxCommandsTreeItem extends TreeItem { this.command = { title: commandConfig.label, - command: commandConfig.command, + command: + commandConfig.type === 'arbitrary-command' + ? EXECUTE_ARBITRARY_COMMAND + : commandConfig.command, + arguments: + commandConfig.type === 'arbitrary-command' + ? [commandConfig.command] + : [], tooltip: commandConfig.type === 'add-dependency' || commandConfig.type === 'add-dev-dependency'