Skip to content

Commit

Permalink
execute arbitrary commands
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless committed Nov 7, 2022
1 parent 19b7b83 commit 8fed994
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 13 deletions.
7 changes: 6 additions & 1 deletion apps/vscode-e2e/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*", ".wdio-vscode-service/**/*"],
"ignorePatterns": [
"!**/*",
".wdio-vscode-service/**/*",
"./testworkspaces/**/*",
"**/node_modules/**/*"
],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
Expand Down
16 changes: 13 additions & 3 deletions apps/vscode/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"command": "nxConsole.refreshWorkspace",
"when": "view == nxRunTarget",
"group": "navigation"
},
{
"command": "nxConsole.editCommonCommands",
"when": "view == nxCommands",
"group": "navigation"
}
],
"view/item/context": [
Expand Down Expand Up @@ -376,6 +381,11 @@
"category": "Nx",
"icon": "$(refresh)"
},
{
"command": "nxConsole.editCommonCommands",
"title": "Edit Common Commands",
"icon": "$(pencil)"
},
{
"command": "nxConsole.editWorkspaceJson",
"title": "Edit workspace definition",
Expand Down Expand Up @@ -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",
Expand Down
13 changes: 12 additions & 1 deletion libs/vscode/configuration/src/lib/global-configuration-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ConfigurationTarget,
ExtensionContext,
EventEmitter,
workspace,
Memento,
} from 'vscode';
Expand Down Expand Up @@ -30,7 +31,15 @@ export class GlobalConfigurationStore implements Store {
return CONFIG_STORE;
}

private constructor(private readonly state: Memento) {}
private readonly _onConfigurationChange: EventEmitter<void> =
new EventEmitter();
readonly onConfigurationChange = this._onConfigurationChange.event;

private constructor(private readonly state: Memento) {
workspace.onDidChangeConfiguration(() => {
this._onConfigurationChange.fire();
});
}

get<T extends keyof GlobalConfig>(key: T): GlobalConfig[T] | null;
get<T>(key: GlobalConfigKeys): T | null;
Expand All @@ -42,10 +51,12 @@ export class GlobalConfigurationStore implements Store {

set<T>(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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
47 changes: 41 additions & 6 deletions libs/vscode/nx-commands-view/src/lib/nx-commands-provider.ts
Original file line number Diff line number Diff line change
@@ -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<NxCommandsTreeItem> {
constructor(private readonly context: ExtensionContext) {
super();
commands.registerCommand(
EXECUTE_ARBITRARY_COMMAND,
this.executeArbirtraryCommand
);
GlobalConfigurationStore.instance.onConfigurationChange(() =>
this.refresh()
);
}

getParent(_: NxCommandsTreeItem) {
Expand All @@ -25,17 +40,17 @@ export class NxCommandsTreeProvider extends AbstractTreeProvider<NxCommandsTreeI
return {
command: transformedCommand,
type: 'vscode-command',
label: transformedCommand,
label: command,
};
}
if (command === 'Add Dependency') {
if (command === 'add-dependency') {
return {
type: 'add-dependency',
command: 'nxConsole.addDependency',
label: 'Add Dependency',
};
}
if (command === 'Add Dev Dependency') {
if (command === 'add-dev-dependency') {
return {
type: 'add-dev-dependency',
command: 'nxConsole.addDevDependency',
Expand All @@ -54,4 +69,24 @@ export class NxCommandsTreeProvider extends AbstractTreeProvider<NxCommandsTreeI
(c) => 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);
}
}
10 changes: 9 additions & 1 deletion libs/vscode/nx-commands-view/src/lib/nx-commands-tree-item.ts
Original file line number Diff line number Diff line change
@@ -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 =
| {
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 8fed994

Please sign in to comment.