diff --git a/apps/vscode/src/main.ts b/apps/vscode/src/main.ts index bb70465590..a9779d0f71 100644 --- a/apps/vscode/src/main.ts +++ b/apps/vscode/src/main.ts @@ -1,15 +1,15 @@ import { existsSync } from 'fs'; import { dirname, join, parse } from 'path'; import { - commands, ExtensionContext, ExtensionMode, FileSystemWatcher, RelativePattern, - tasks, TreeItem, TreeView, Uri, + commands, + tasks, window, workspace, } from 'vscode'; @@ -21,8 +21,8 @@ import { } from '@nx-console/vscode/configuration'; import { initNxCommandsView } from '@nx-console/vscode/nx-commands-view'; import { - initNxProjectView, NxProjectTreeProvider, + initNxProjectView, } from '@nx-console/vscode/nx-project-view'; import { LOCATE_YOUR_WORKSPACE, @@ -41,36 +41,31 @@ import { watchFile, } from '@nx-console/vscode/utils'; import { revealWebViewPanel } from '@nx-console/vscode/webview'; -import { environment } from './environments/environment'; import { fileExists } from '@nx-console/shared/file-system'; -import { enableTypeScriptPlugin } from '@nx-console/vscode/typescript-plugin'; import { AddDependencyCodelensProvider, registerVscodeAddDependency, } from '@nx-console/vscode/add-dependency'; +import { + initGenerateUiWebview, + openGenerateUi, +} from '@nx-console/vscode/generate-ui-webview'; import { configureLspClient } from '@nx-console/vscode/lsp-client'; +import { initNxCloudOnboardingView } from '@nx-console/vscode/nx-cloud-view'; +import { initNxConfigDecoration } from '@nx-console/vscode/nx-config-decoration'; +import { initNxConversion } from '@nx-console/vscode/nx-conversion'; import { NxHelpAndFeedbackProvider, NxHelpAndFeedbackTreeItem, } from '@nx-console/vscode/nx-help-and-feedback-view'; +import { getNxWorkspace, stopDaemon } from '@nx-console/vscode/nx-workspace'; import { projectGraph } from '@nx-console/vscode/project-graph'; +import { enableTypeScriptPlugin } from '@nx-console/vscode/typescript-plugin'; import { - refreshWorkspace, REFRESH_WORKSPACE, + refreshWorkspace, } from './commands/refresh-workspace'; -import { - getGenerators, - getNxWorkspace, - stopDaemon, -} from '@nx-console/vscode/nx-workspace'; -import { initNxCloudOnboardingView } from '@nx-console/vscode/nx-cloud-view'; -import { initNxConversion } from '@nx-console/vscode/nx-conversion'; -import { - initGenerateUiWebview, - openGenerateUi, -} from '@nx-console/vscode/generate-ui-webview'; -import { initNxConfigDecoration } from '@nx-console/vscode/nx-config-decoration'; let runTargetTreeView: TreeView; let nxHelpAndFeedbackTreeView: TreeView; @@ -83,6 +78,8 @@ let workspaceFileWatcher: FileSystemWatcher | undefined; let isNxWorkspace = false; +let hasInitializedExtensionPoints = false; + export async function activate(c: ExtensionContext) { try { const startTime = Date.now(); @@ -116,7 +113,7 @@ export async function activate(c: ExtensionContext) { const manuallySelectWorkspaceDefinitionCommand = commands.registerCommand( LOCATE_YOUR_WORKSPACE.command?.command || '', async () => { - return manuallySelectWorkspaceDefinition(); + manuallySelectWorkspaceDefinition(); } ); const vscodeWorkspacePath = @@ -173,7 +170,7 @@ function manuallySelectWorkspaceDefinition() { .then((value) => { if (value && value[0]) { const selectedDirectory = value[0].fsPath; - return setWorkspace(selectedDirectory); + setWorkspace(selectedDirectory); } }); } else { @@ -221,17 +218,19 @@ async function setWorkspace(workspacePath: string) { WorkspaceConfigurationStore.instance.set('nxWorkspacePath', workspacePath); - const lspContext = configureLspClient(context, REFRESH_WORKSPACE); + configureLspClient(context, REFRESH_WORKSPACE); // Set the NX_WORKSPACE_ROOT_PATH as soon as possible so that the nx utils can get this. process.env.NX_WORKSPACE_ROOT_PATH = workspacePath; - setApplicationAndLibraryContext(workspacePath); - isNxWorkspace = await checkIsNxWorkspace(workspacePath); const isAngularWorkspace = existsSync(join(workspacePath, 'angular.json')); - if (!(isAngularWorkspace && !isNxWorkspace)) { + if ( + !(isAngularWorkspace && !isNxWorkspace) && + !hasInitializedExtensionPoints + ) { + hasInitializedExtensionPoints = true; registerNxCommands(context); tasks.registerTaskProvider('nx', CliTaskProvider.instance); registerCliTaskCommands(context); @@ -254,76 +253,24 @@ async function setWorkspace(workspacePath: string) { new AddDependencyCodelensProvider(context); - context.subscriptions.push(nxHelpAndFeedbackTreeView, lspContext); + context.subscriptions.push(nxHelpAndFeedbackTreeView); } else { WorkspaceConfigurationStore.instance.set('nxWorkspacePath', workspacePath); } - commands.executeCommand( - 'setContext', - 'isAngularWorkspace', - isAngularWorkspace - ); - commands.executeCommand('setContext', 'isNxWorkspace', isNxWorkspace); - registerWorkspaceFileWatcher(context, workspacePath); currentRunTargetTreeProvider?.refresh(); nxProjectsTreeProvider?.refresh(); - let workspaceType: 'nx' | 'angular' | 'angularWithNx' = 'nx'; - if (isNxWorkspace && isAngularWorkspace) { - workspaceType = 'angularWithNx'; - } else if (isNxWorkspace && !isAngularWorkspace) { - workspaceType = 'nx'; - } else if (!isNxWorkspace && isAngularWorkspace) { - workspaceType = 'angular'; - } - - if (workspaceType === 'angular') { - initNxConversion(context); - } -} - -async function setApplicationAndLibraryContext(workspacePath: string) { - const { workspaceLayout } = await getNxWorkspace(); - - if (workspaceLayout.appsDir) { - commands.executeCommand('setContext', 'nxAppsDir', [ - join(workspacePath, workspaceLayout.appsDir), - ]); - } - if (workspaceLayout.libsDir) { - commands.executeCommand('setContext', 'nxLibsDir', [ - join(workspacePath, workspaceLayout.libsDir), - ]); - } - - const generatorCollections = await getGenerators(); - - let hasApplicationGenerators = false; - let hasLibraryGenerators = false; - - generatorCollections.forEach((generatorCollection) => { - if (generatorCollection.data) { - if (generatorCollection.data.type === 'application') { - hasApplicationGenerators = true; - } else if (generatorCollection.data.type === 'library') { - hasLibraryGenerators = true; - } - } - }); - commands.executeCommand( 'setContext', - 'nx.hasApplicationGenerators', - hasApplicationGenerators - ); - commands.executeCommand( - 'setContext', - 'nx.hasLibraryGenerators', - hasLibraryGenerators + 'isAngularWorkspace', + isAngularWorkspace ); + commands.executeCommand('setContext', 'isNxWorkspace', isNxWorkspace); + + initNxConversion(context, isAngularWorkspace, isNxWorkspace); } async function registerWorkspaceFileWatcher( diff --git a/libs/language-server/types/src/index.ts b/libs/language-server/types/src/index.ts index ee4c9338dc..18a6529184 100644 --- a/libs/language-server/types/src/index.ts +++ b/libs/language-server/types/src/index.ts @@ -1,17 +1,13 @@ -import { - NotificationType, - RequestType, - ResponseError, -} from 'vscode-languageserver/node'; -import { NxWorkspace, TreeMap, TreeNode } from '@nx-console/shared/types'; +import { GeneratorContext } from '@nx-console/shared/generate-ui-types'; import { CollectionInfo, Option, TaskExecutionSchema, } from '@nx-console/shared/schema'; +import { NxWorkspace, TreeNode } from '@nx-console/shared/types'; import type { ProjectConfiguration } from 'nx/src/devkit-exports'; import { SemVer } from 'semver'; -import { GeneratorContext } from '@nx-console/shared/generate-ui-types'; +import { NotificationType, RequestType } from 'vscode-languageserver/node'; export const NxChangeWorkspace: NotificationType = new NotificationType( 'nx/changeWorkspace' diff --git a/libs/vscode/lsp-client/src/lib/configure-lsp-client.ts b/libs/vscode/lsp-client/src/lib/configure-lsp-client.ts index 4d17b38102..c5d9d9997c 100644 --- a/libs/vscode/lsp-client/src/lib/configure-lsp-client.ts +++ b/libs/vscode/lsp-client/src/lib/configure-lsp-client.ts @@ -20,12 +20,10 @@ let client: LanguageClient | undefined; export function configureLspClient( context: ExtensionContext, refreshCommand: string | undefined -): Disposable { +) { if (client) { sendNotification(NxChangeWorkspace, getWorkspacePath()); - return { - dispose, - }; + return; } const serverModule = context.asAbsolutePath(join('nxls', 'main.js')); @@ -73,9 +71,7 @@ export function configureLspClient( } }); - return { - dispose, - }; + context.subscriptions.push({ dispose }); } function dispose() { diff --git a/libs/vscode/nx-conversion/src/lib/vscode-nx-conversion.ts b/libs/vscode/nx-conversion/src/lib/vscode-nx-conversion.ts index 1410cb4d6f..cca7ef3efc 100644 --- a/libs/vscode/nx-conversion/src/lib/vscode-nx-conversion.ts +++ b/libs/vscode/nx-conversion/src/lib/vscode-nx-conversion.ts @@ -5,7 +5,24 @@ import { commands, ExtensionContext, window } from 'vscode'; let run = false; -export async function initNxConversion(context: ExtensionContext) { +export async function initNxConversion( + context: ExtensionContext, + isAngularWorkspace: boolean, + isNxWorkspace: boolean +) { + let workspaceType: 'nx' | 'angular' | 'angularWithNx' = 'nx'; + if (isNxWorkspace && isAngularWorkspace) { + workspaceType = 'angularWithNx'; + } else if (isNxWorkspace && !isAngularWorkspace) { + workspaceType = 'nx'; + } else if (!isNxWorkspace && isAngularWorkspace) { + workspaceType = 'angular'; + } + + if (workspaceType !== 'angular') { + return; + } + if (run) { return; } diff --git a/libs/vscode/project-graph/src/lib/graph-webview.ts b/libs/vscode/project-graph/src/lib/graph-webview.ts index a564e78ee3..d3e06dbb82 100644 --- a/libs/vscode/project-graph/src/lib/graph-webview.ts +++ b/libs/vscode/project-graph/src/lib/graph-webview.ts @@ -2,7 +2,6 @@ import { getNxWorkspacePath, getNxWorkspaceProjects, getProjectGraphOutput, - revealNxProject, } from '@nx-console/vscode/nx-workspace'; import { getOutputChannel, getTelemetry } from '@nx-console/vscode/utils'; import { @@ -19,6 +18,7 @@ import { graphService } from './graph.machine'; import { loadError, loadHtml, loadNoProject, loadSpinner } from './load-html'; import { join } from 'node:path'; import { CliTaskProvider } from '@nx-console/vscode/tasks'; +import { revealNxProject } from '@nx-console/vscode/nx-config-decoration'; export class GraphWebView implements Disposable { panel: WebviewPanel | undefined; diff --git a/tsconfig.base.json b/tsconfig.base.json index f61e162556..46bf00492c 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -83,6 +83,9 @@ "@nx-console/vscode/nx-commands-view": [ "libs/vscode/nx-commands-view/src/index.ts" ], + "@nx-console/vscode/nx-config-decoration": [ + "libs/vscode/nx-config-decoration/src/index.ts" + ], "@nx-console/vscode/nx-conversion": [ "libs/vscode/nx-conversion/src/index.ts" ], @@ -107,10 +110,7 @@ ], "@nx-console/vscode/utils": ["libs/vscode/utils/src/index.ts"], "@nx-console/vscode/verify": ["libs/vscode/verify/src/index.ts"], - "@nx-console/vscode/webview": ["libs/vscode/webview/src/index.ts"], - "@nx-console/vscode/nx-config-decoration": [ - "libs/vscode/nx-config-decoration/src/index.ts" - ] + "@nx-console/vscode/webview": ["libs/vscode/webview/src/index.ts"] } }, "exclude": ["node_modules", "tmp"]