From afbcd000e5eac490a119b993a54869e664edb4c7 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 4 Jul 2022 13:04:10 -0400 Subject: [PATCH] fix: optimize file watches --- apps/vscode/src/commands/refresh-workspace.ts | 4 +- apps/vscode/src/main.ts | 64 ++++++++++--------- .../nx-workspace/src/lib/nx-workspace.ts | 23 +++++++ .../src/lib/get-task-execution-schema.ts | 9 +-- 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/apps/vscode/src/commands/refresh-workspace.ts b/apps/vscode/src/commands/refresh-workspace.ts index 0fa3e5a5c9..5c343138b5 100644 --- a/apps/vscode/src/commands/refresh-workspace.ts +++ b/apps/vscode/src/commands/refresh-workspace.ts @@ -1,6 +1,8 @@ import { debounceTime, Subject } from 'rxjs'; import { commands } from 'vscode'; +export const REFRESH_WORKSPACE = 'nxConsole.refreshWorkspace'; + const refresh = new Subject(); refresh.pipe(debounceTime(300)).subscribe(async () => { @@ -14,7 +16,7 @@ refresh.pipe(debounceTime(300)).subscribe(async () => { * Refresh workspace by debouncing multiple calls to only trigger once */ export function refreshWorkspace() { - return commands.registerCommand('nxConsole.refreshWorkspace', () => { + return commands.registerCommand(REFRESH_WORKSPACE, () => { refresh.next(undefined); }); } diff --git a/apps/vscode/src/main.ts b/apps/vscode/src/main.ts index 0f683b5f3a..b403061d5b 100644 --- a/apps/vscode/src/main.ts +++ b/apps/vscode/src/main.ts @@ -54,7 +54,10 @@ import { } from '@nx-console/vscode/json-schema'; import { enableTypeScriptPlugin } from '@nx-console/typescript-plugin'; import { NxConversion } from '@nx-console/vscode/nx-conversion'; -import { refreshWorkspace } from './commands/refresh-workspace'; +import { + refreshWorkspace, + REFRESH_WORKSPACE, +} from './commands/refresh-workspace'; let runTargetTreeView: TreeView; let nxProjectTreeView: TreeView; @@ -213,13 +216,6 @@ async function setWorkspace(workspacePath: string) { // 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; - // const { verifyWorkspace } = await import('@nx-console/vscode/nx-workspace'); - - // const { validWorkspaceJson } = await verifyWorkspace(); - // if (!validWorkspaceJson) { - // return; - // // } - if (!cliTaskProvider) { cliTaskProvider = new CliTaskProvider(); registerNxCommands(context, cliTaskProvider); @@ -280,20 +276,15 @@ async function setWorkspace(workspacePath: string) { } async function setApplicationAndLibraryContext(workspacePath: string) { - const { getNxConfig } = await import('@nx-console/vscode/nx-workspace'); + const { nxWorkspace } = await import('@nx-console/vscode/nx-workspace'); - let nxConfig: AsyncReturnType; - try { - nxConfig = await getNxConfig(workspacePath); - } catch { - return; - } + const { workspaceLayout } = await nxWorkspace(); commands.executeCommand('setContext', 'nxAppsDir', [ - join(workspacePath, nxConfig.workspaceLayout?.appsDir ?? 'apps'), + join(workspacePath, workspaceLayout.appsDir), ]); commands.executeCommand('setContext', 'nxLibsDir', [ - join(workspacePath, nxConfig.workspaceLayout?.libsDir ?? 'libs'), + join(workspacePath, workspaceLayout.libsDir), ]); const generatorCollections = await getGenerators(workspacePath); @@ -323,25 +314,38 @@ async function setApplicationAndLibraryContext(workspacePath: string) { ); } -function registerWorkspaceFileWatcher( +async function registerWorkspaceFileWatcher( context: ExtensionContext, - workspaceJsonPath: string + workspacePath: string ) { if (workspaceFileWatcher) { workspaceFileWatcher.dispose(); } - const workspaceDir = dirname(workspaceJsonPath); - - workspaceFileWatcher = watchFile( - new RelativePattern( - workspaceDir, - '**/{workspace,angular,project,nx,package}.json' + const { nxWorkspace } = await import('@nx-console/vscode/nx-workspace'); + + const { workspaceLayout } = await nxWorkspace(); + const workspacePackageDirs = new Set(); + workspacePackageDirs.add(workspaceLayout.appsDir); + workspacePackageDirs.add(workspaceLayout.libsDir); + workspacePackageDirs.add('packages'); + context.subscriptions.push( + watchFile( + new RelativePattern(workspacePath, '{workspace,angular,nx}.json'), + () => { + commands.executeCommand(REFRESH_WORKSPACE); + } ), - () => { - commands.executeCommand('nxConsole.refreshWorkspace'); - } + ...Array.from(workspacePackageDirs).map((dir) => { + return watchFile( + new RelativePattern( + join(workspacePath, dir), + `**/{project,package}.json` + ), + () => { + commands.executeCommand(REFRESH_WORKSPACE); + } + ); + }) ); - - context.subscriptions.push(workspaceFileWatcher); } diff --git a/libs/vscode/nx-workspace/src/lib/nx-workspace.ts b/libs/vscode/nx-workspace/src/lib/nx-workspace.ts index e4fbe53a13..c04569465c 100644 --- a/libs/vscode/nx-workspace/src/lib/nx-workspace.ts +++ b/libs/vscode/nx-workspace/src/lib/nx-workspace.ts @@ -29,6 +29,11 @@ interface Workspace { workspaceType: 'ng' | 'nx'; configurationFilePath: string; workspacePath: string; + isLerna: boolean; + workspaceLayout: { + appsDir: string; + libsDir: string; + }; } const enum Status { @@ -85,12 +90,25 @@ async function _workspace(): Promise { isAngularWorkspace ? 'angularCli' : 'nx' ); + const isLerna = await fileExists(join(workspacePath, 'lerna.json')); + try { return { validWorkspaceJson: true, workspaceType: isAngularWorkspace ? 'ng' : 'nx', json: toWorkspaceFormat(config.workspaceConfiguration), configurationFilePath: config.configPath, + isLerna, + workspaceLayout: { + appsDir: + config.workspaceConfiguration.workspaceLayout?.appsDir ?? isLerna + ? 'packages' + : 'apps', + libsDir: + config.workspaceConfiguration.workspaceLayout?.libsDir ?? isLerna + ? 'packages' + : 'libs', + }, workspacePath, }; } catch (e) { @@ -117,6 +135,11 @@ async function _workspace(): Promise { }, configurationFilePath: '', workspacePath, + isLerna: false, + workspaceLayout: { + appsDir: 'apps', + libsDir: 'libs', + }, }; } } diff --git a/libs/vscode/webview/src/lib/get-task-execution-schema.ts b/libs/vscode/webview/src/lib/get-task-execution-schema.ts index e6d4f8bf69..8ee3344634 100644 --- a/libs/vscode/webview/src/lib/get-task-execution-schema.ts +++ b/libs/vscode/webview/src/lib/get-task-execution-schema.ts @@ -4,7 +4,7 @@ import { getTelemetry, readTargetDef, } from '@nx-console/server'; -import { getNxConfig, nxWorkspace } from '@nx-console/vscode/nx-workspace'; +import { nxWorkspace } from '@nx-console/vscode/nx-workspace'; import { verifyBuilderDefinition } from '@nx-console/vscode/verify'; import { Uri, window } from 'vscode'; import { @@ -196,9 +196,10 @@ async function getConfigValuesFromContextMenuUri( .replace(workspacePath, '') .replace(/\\/g, '/') .replace(/^\//, ''); - const nxConfig = await getNxConfig(workspacePath); - const appsDir = nxConfig.workspaceLayout?.appsDir ?? 'packages'; - const libsDir = nxConfig.workspaceLayout?.libsDir ?? 'packages'; + const { workspaceLayout } = await nxWorkspace(); + + const appsDir = workspaceLayout.appsDir; + const libsDir = workspaceLayout.libsDir; if ( (appsDir && generator.name === 'application') || generator.name === 'app'