Skip to content

Commit

Permalink
fix: improve performance for retrieving workspace details (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli authored Jun 20, 2022
1 parent e0f1025 commit f8d32ca
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 104 deletions.
20 changes: 15 additions & 5 deletions apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import {
} from '@nx-console/vscode/json-schema';
import { enableTypeScriptPlugin } from '@nx-console/typescript-plugin';
import { NxConversion } from '@nx-console/vscode/nx-conversion';
import { nxVersion } from '@nx-console/vscode/nx-workspace';

let runTargetTreeView: TreeView<RunTargetTreeItem>;
let nxProjectTreeView: TreeView<NxProjectTreeItem>;
Expand Down Expand Up @@ -113,7 +112,13 @@ export async function activate(c: ExtensionContext) {
context.subscriptions.push(
runTargetTreeView,
revealWebViewPanelCommand,
manuallySelectWorkspaceDefinitionCommand
manuallySelectWorkspaceDefinitionCommand,
commands.registerCommand('nxConsole.refreshWorkspace', async () => {
const { nxWorkspace } = await import('@nx-console/vscode/nx-workspace');
nxWorkspace(true);
commands.executeCommand('nxConsole.refreshNxProjectsTree');
commands.executeCommand('nxConsole.refreshRunTargetTree');
})
);

// registers itself as a CodeLensProvider and watches config to dispose/re-register
Expand Down Expand Up @@ -269,6 +274,9 @@ async function setWorkspace(workspacePath: string) {
} else if (!isNxWorkspace && isAngularWorkspace) {
workspaceType = 'angular';
}

const { nxVersion } = await import('@nx-console/vscode/nx-workspace');

WorkspaceConfigurationStore.instance.set('workspaceType', workspaceType);
WorkspaceConfigurationStore.instance.set('nxVersion', await nxVersion());

Expand Down Expand Up @@ -330,10 +338,12 @@ function registerWorkspaceFileWatcher(
const workspaceDir = dirname(workspaceJsonPath);

workspaceFileWatcher = watchFile(
new RelativePattern(workspaceDir, '**/{workspace,angular,project}.json'),
new RelativePattern(
workspaceDir,
'**/{workspace,angular,project,nx,package}.json'
),
() => {
commands.executeCommand('nxConsole.refreshNxProjectsTree');
commands.executeCommand('nxConsole.refreshRunTargetTree');
commands.executeCommand('nxConsole.refreshWorkspace');
}
);

Expand Down
11 changes: 8 additions & 3 deletions apps/vscode/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@
],
"view/title": [
{
"command": "nxConsole.refreshNxProjectsTree",
"command": "nxConsole.refreshWorkspace",
"when": "view == nxProjects",
"group": "navigation"
},
{
"command": "nxConsole.refreshWorkspace",
"when": "view == nxRunTarget",
"group": "navigation"
}
],
"view/item/context": [
Expand Down Expand Up @@ -328,8 +333,8 @@
},
"commands": [
{
"command": "nxConsole.refreshNxProjectsTree",
"title": "Refresh Projects",
"command": "nxConsole.refreshWorkspace",
"title": "Refresh Workspace",
"category": "Nx",
"icon": "$(refresh)"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ import {
switchMap,
startWith,
scan,
debounceTime,
debounce,
filter,
shareReplay,
} from 'rxjs/operators';
import { BehaviorSubject, Observable, fromEvent, merge, of } from 'rxjs';
import {
BehaviorSubject,
Observable,
fromEvent,
merge,
of,
interval,
} from 'rxjs';
import { Option } from '@nx-console/schema';
import { getOptionItems } from '../field-items/field-items.pipe';

Expand Down Expand Up @@ -90,7 +97,11 @@ export class AutocompleteComponent implements OnInit, ControlValueAccessor {
fromEvent(this._elementRef.nativeElement, 'focusout').pipe(
map(() => false)
)
).pipe(startWith(false), debounceTime(300), shareReplay(1));
).pipe(
startWith(false),
debounce(() => interval(300)),
shareReplay(1)
);

this.visibleOptions = this._options$.pipe(
switchMap((options) =>
Expand Down
4 changes: 2 additions & 2 deletions libs/vscode/json-schema/src/lib/project-json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CollectionInfo } from '@nx-console/schema';
import { getExecutors, watchFile } from '@nx-console/server';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import { verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { nxWorkspace } from '@nx-console/vscode/nx-workspace';
import { join } from 'path';
import * as vscode from 'vscode';

Expand Down Expand Up @@ -36,7 +36,7 @@ export class ProjectJsonSchema {
clearPackageJsonCache = false
) {
const filePath = vscode.Uri.joinPath(extensionUri, 'project-schema.json');
const { json } = await verifyWorkspace();
const { json } = await nxWorkspace();
const collections = await getExecutors(
workspacePath,
json.projects,
Expand Down
4 changes: 2 additions & 2 deletions libs/vscode/json-schema/src/lib/workspace-json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CollectionInfo } from '@nx-console/schema';
import { getExecutors, watchFile } from '@nx-console/server';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import { verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { nxWorkspace } from '@nx-console/vscode/nx-workspace';
import { dirname, join } from 'path';
import * as vscode from 'vscode';

Expand Down Expand Up @@ -36,7 +36,7 @@ export class WorkspaceJsonSchema {
clearPackageJsonCache = false
) {
const filePath = vscode.Uri.joinPath(extensionUri, 'workspace-schema.json');
const { json } = await verifyWorkspace();
const { json } = await nxWorkspace();
const collections = await getExecutors(
workspacePath,
json.projects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,6 @@ export class NxProjectTreeProvider extends AbstractTreeProvider<NxProjectTreeIte
}

private async refreshNxProjectsTree() {
const workspacePath = WorkspaceConfigurationStore.instance.get(
'nxWorkspacePath',
''
);
clearJsonCache(workspacePath);

this.refresh();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeneratorType } from '@nx-console/schema';
import { verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { nxWorkspace } from '@nx-console/vscode/nx-workspace';
import { join } from 'path';
import { TreeItem, TreeItemCollapsibleState, TreeView, Uri } from 'vscode';

Expand Down Expand Up @@ -67,7 +67,7 @@ export class RunTargetTreeItem extends TreeItem {
}

async function getTargetNames(): Promise<string[]> {
const { json } = await verifyWorkspace();
const { json } = await nxWorkspace();
const commands = Object.values(json.projects).reduce((acc, project) => {
for (const target of Object.keys(project.targets ?? {})) {
acc.add(target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ export class RunTargetTreeProvider extends AbstractTreeProvider<
}

private refreshRunTargetTree = async () => {
const workspacePath = WorkspaceConfigurationStore.instance.get(
'nxWorkspacePath',
''
);
clearJsonCache(workspacePath);

this.refresh();
};
}
2 changes: 1 addition & 1 deletion libs/vscode/nx-workspace/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './lib/find-workspace-json-target';
export * from './lib/reveal-workspace-json';
export * from './lib/workspace-codelens-provider';
export * from './lib/verify-workspace';
export * from './lib/nx-workspace';
export * from './lib/get-nx-config';
export * from './lib/get-nx-workspace-config';
export * from './lib/nx-version';
2 changes: 1 addition & 1 deletion libs/vscode/nx-workspace/src/lib/get-nx-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cacheJson, readAndCacheJsonFile } from '@nx-console/server';
import { NxJsonConfiguration } from '@nrwl/devkit';
import type { NxJsonConfiguration } from '@nrwl/devkit';
import { join } from 'path';
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';
import { stat } from 'fs/promises';
Expand Down
27 changes: 3 additions & 24 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
NxJsonConfiguration,
WorkspaceJsonConfiguration,
ProjectGraph,
Expand Down Expand Up @@ -61,21 +61,9 @@ export async function getNxWorkspaceConfig(
}

if (version < 13) {
projectGraph = (nxProjectGraph as any).readCurrentProjectGraph();
projectGraph = (nxProjectGraph as any).createProjectGraph();
} else {
projectGraph = nxProjectGraph.readCachedProjectGraph();
}

// Projects can become out of sync if we don't use Nx directly to create projects. This is the case for package.json projects
if (
!projectGraph ||
projectsOutOfSync(workspaceConfiguration, projectGraph)
) {
if (version < 13) {
projectGraph = (nxProjectGraph as any).createProjectGraph();
} else {
projectGraph = await nxProjectGraph.createProjectGraphAsync();
}
projectGraph = await nxProjectGraph.createProjectGraphAsync();
}
} catch {
//noop
Expand Down Expand Up @@ -150,12 +138,3 @@ function addProjectTargets(
}
}
}

function projectsOutOfSync(
workspaceConfiguration: WorkspaceJsonConfiguration,
projectGraph: ProjectGraph
): boolean {
const workspaceProjects = Object.keys(workspaceConfiguration.projects);
const projectGraphProjects = Object.keys(projectGraph.nodes);
return workspaceProjects.length != projectGraphProjects.length;
}
10 changes: 6 additions & 4 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace-package.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { workspaceDependencyPath } from '@nx-console/npm';
import { fileExists, getOutputChannel } from '@nx-console/server';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import * as NxFileUtils from 'nx/src/project-graph/file-utils';
import * as NxProjectGraph from 'nx/src/project-graph/project-graph';
import type * as NxFileUtils from 'nx/src/project-graph/file-utils';
import type * as NxProjectGraph from 'nx/src/project-graph/project-graph';
import { platform } from 'os';
import { join } from 'path';

Expand Down Expand Up @@ -32,7 +32,8 @@ export async function getNxProjectGraph(): Promise<typeof NxProjectGraph> {
);
}

return getNxPackage(importPath, NxProjectGraph, RESOLVED_PROJECTGRAPH_IMPORT);
const nxProjectGraph = await import('nx/src/project-graph/project-graph');
return getNxPackage(importPath, nxProjectGraph, RESOLVED_PROJECTGRAPH_IMPORT);
}

/**
Expand Down Expand Up @@ -62,7 +63,8 @@ export async function getNxWorkspacePackageFileUtils(): Promise<
);
}

return getNxPackage(importPath, NxFileUtils, RESOLVED_FILEUTILS_IMPORT);
const nxFileUtils = await import('nx/src/project-graph/file-utils');
return getNxPackage(importPath, nxFileUtils, RESOLVED_FILEUTILS_IMPORT);
}

async function getNxPackage<T>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { verifyWorkspace } from './verify-workspace';
import { nxWorkspace } from './nx-workspace';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import * as server from '@nx-console/server';
import {
cacheJson,
getOutputChannel,
getTelemetry,
fileExists,
} from '@nx-console/server';
import { getOutputChannel, getTelemetry, fileExists } from '@nx-console/server';
import { mocked } from 'ts-jest/utils';
import type {
NxJsonConfiguration,
Expand Down Expand Up @@ -85,7 +80,7 @@ xdescribe('verifyWorkspace', () => {

// act
const { validWorkspaceJson, json, workspaceType, configurationFilePath } =
await verifyWorkspace();
await nxWorkspace();

// assert
expect(mockStoreInstanceGetFn).toHaveBeenCalledWith(
Expand Down Expand Up @@ -113,7 +108,7 @@ xdescribe('verifyWorkspace', () => {
});
// act
const { validWorkspaceJson, json, workspaceType, configurationFilePath } =
await verifyWorkspace();
await nxWorkspace();

// assert
expect(mockStoreInstanceGetFn).toHaveBeenCalledWith(
Expand All @@ -138,7 +133,7 @@ xdescribe('verifyWorkspace', () => {
.mockResolvedValue('Show Error');

// act
const result = await verifyWorkspace();
const result = await nxWorkspace();

// assert

Expand Down Expand Up @@ -171,7 +166,7 @@ xdescribe('verifyWorkspace', () => {
// act
const {
json: { projects },
} = await verifyWorkspace();
} = await nxWorkspace();
const [project1, project2, project3] = Object.keys(projects);
const [sorted1, sorted2, sorted3] = Object.keys(sortedProject);

Expand Down
Loading

0 comments on commit f8d32ca

Please sign in to comment.