From dc4e15565835f786754634e4e016aae41d253555 Mon Sep 17 00:00:00 2001 From: Nico Vogel Date: Tue, 1 Nov 2022 01:40:34 +0100 Subject: [PATCH] wip --- .../src/lib/nx-project-tree-provider.ts | 8 +- .../src/lib/views/nx-project-base-view.ts | 19 ++- .../lib/views/nx-project-tree-view.spec.ts | 126 ++++++++++++++++++ .../src/lib/views/nx-project-tree-view.ts | 8 +- 4 files changed, 152 insertions(+), 9 deletions(-) create mode 100644 libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.spec.ts diff --git a/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts b/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts index fbf5be4ec1..f81f6ba0d3 100644 --- a/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts +++ b/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts @@ -2,7 +2,7 @@ import { GlobalConfigurationStore } from '@nx-console/vscode/configuration'; import { revealNxProject } from '@nx-console/vscode/nx-workspace'; import { CliTaskProvider } from '@nx-console/vscode/tasks'; import { AbstractTreeProvider } from '@nx-console/vscode/utils'; -import { commands, ExtensionContext, ProviderResult } from 'vscode'; +import { commands, ExtensionContext, ProviderResult, TreeItem } from 'vscode'; import { NxFolderTreeItem, NxListViewItem, @@ -51,7 +51,7 @@ export class NxProjectTreeProvider extends AbstractTreeProvider { return null; } - getChildren(element?: NxTreeItem) { + getChildren(element?: TreeItem) { if (this.isListViewElement(element)) { return this.listView.getChildren(element); } @@ -109,3 +109,7 @@ export class NxProjectTreeProvider extends AbstractTreeProvider { this.refresh(); } } + +class NxTreeItem extends TreeItem { + constructor(public readonly type: string) +} diff --git a/libs/vscode/nx-project-view/src/lib/views/nx-project-base-view.ts b/libs/vscode/nx-project-view/src/lib/views/nx-project-base-view.ts index 8ce207c998..f16d318346 100644 --- a/libs/vscode/nx-project-view/src/lib/views/nx-project-base-view.ts +++ b/libs/vscode/nx-project-view/src/lib/views/nx-project-base-view.ts @@ -1,7 +1,7 @@ import { ProjectConfiguration, TargetConfiguration } from '@nrwl/devkit'; import { CliTaskProvider } from '@nx-console/vscode/tasks'; import { getOutputChannel } from '@nx-console/vscode/utils'; -import { TreeDataProvider, TreeItemCollapsibleState } from 'vscode'; +import { type } from 'os'; import { NxProject, NxProjectTreeItem, @@ -12,8 +12,23 @@ export type ProjectViewStrategy = Required< Pick, 'getChildren'> >; +export type ViewDataProvider = Pick + +export type Collapsible = 'none' | 'collapsed' | 'expanded' + +export type ViewItem = FolderViewItem + +export interface FolderViewItem { + contextValue: 'folder'; + path: string; + resource: string; + label: string + collapsible: Collapsible +} + + export abstract class BaseView { - constructor(protected readonly cliTaskProvider: CliTaskProvider) {} + constructor(protected readonly cliTaskProvider: ViewDataProvider) { } createProjectTreeItem([projectName, { root, name, targets }]: [ projectName: string, diff --git a/libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.spec.ts b/libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.spec.ts new file mode 100644 index 0000000000..0da9460522 --- /dev/null +++ b/libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.spec.ts @@ -0,0 +1,126 @@ +import { ViewDataProvider } from './nx-project-base-view' +import { createTreeViewStrategy } from './nx-project-tree-view' + +describe('Project View: TreeView', () => { + describe('nx workspace', () => { + it('should find root directories', async () => { + const viewProvider = createMockViewDataProvider(nxExample.workspacePath, nxExample.project) + const treeView = createTreeViewStrategy(viewProvider) + + const rootElements = await treeView.getChildren(); + expect(rootElements).toHaveLength(2) + const paths = rootElements?.map((e) => e.label) + expect(paths).toEqual(['apps', 'libs']) + }) + }) +}) + +type MockDataGetWorkspacePath = ReturnType +type MockDataGetProjects = Awaited> + +function createMockViewDataProvider(workspacePath: MockDataGetWorkspacePath, projects: unknown): ViewDataProvider { + return { + getWorkspacePath: () => workspacePath, + getProjects: () => new Promise(resolve => setTimeout(() => resolve(projects as MockDataGetProjects))) + } +} + +// Result of cliTaskProvider.getProjects() for apps/vscode-e2e/testworkspaces/testworkspace-nx +const nxExample = { + workspacePath: '/git/nx-console/apps/vscode-e2e/testworkspaces/testworkspace-nx', + project: { + "app1": { + "targets": { + "build": { + "executor": "@nrwl/webpack:webpack", + "configurations": { + "production": { + + } + }, + "dependsOn": [ + "^build" + ], + "inputs": [ + "production", + "^production" + ] + }, + "test": { + "executor": "@nrwl/jest:jest" + } + }, + "root": "apps/app1", + "tags": [ + + ], + "files": [ + { + "file": "apps/app1/app1.js", + "hash": "fda9bc547f3d044be11c43ba8df3b8f387f29532", + "deps": [ + "lib1", + "lib2" + ] + }, + { + "file": "apps/app1/project.json", + "hash": "5b8dd29636672949793a788e40c9f39b75aadc99" + } + ] + }, + "lib1": { + "targets": { + "test": { + "executor": "@nrwl/jest:jest" + } + }, + "root": "libs/lib1", + "tags": [ + + ], + "files": [ + { + "file": "libs/lib1/project.json", + "hash": "929b00c99a39f3bb085b2487fed80d8e8421aea7" + }, + { + "file": "libs/lib1/src/index.ts", + "hash": "56e2812af30c82f0f15bb726f98a27321363e5ca" + }, + { + "file": "libs/lib1/src/lib/lib1.js", + "hash": "f63c731f25f9ac8efaaee3420cac1e7f08185169" + } + ] + }, + "lib2": { + "targets": { + "test": { + "executor": "@nrwl/jest:jest" + }, + "weird": { + + } + }, + "root": "libs/lib2", + "tags": [ + + ], + "files": [ + { + "file": "libs/lib2/project.json", + "hash": "157a68f9115ba2aeb01e074404066c7aef10c34b" + }, + { + "file": "libs/lib2/src/index.js", + "hash": "9aeaf007321d35731cf3a05e937f966216376c95" + }, + { + "file": "libs/lib2/src/lib/lib2.js", + "hash": "fcc28489c736ebac8fe0b1b4f3c2aa3041a0de8e" + } + ] + } + } +} diff --git a/libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.ts b/libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.ts index 64ddc6dc8f..932672d07c 100644 --- a/libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.ts +++ b/libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.ts @@ -1,20 +1,18 @@ import { ProjectConfiguration } from '@nrwl/devkit'; -import { CliTaskProvider } from '@nx-console/vscode/tasks'; import { getOutputChannel } from '@nx-console/vscode/utils'; -import { TreeItemCollapsibleState } from 'vscode'; import { NxFolderTreeItem, NxProjectTreeItem, NxTreeViewItem, } from '../nx-project-tree-item'; -import { BaseView, ProjectViewStrategy } from './nx-project-base-view'; +import { BaseView, ProjectViewStrategy, ViewDataProvider } from './nx-project-base-view'; import { isDefined, PathHelper } from './nx-project-util'; export type TreeViewStrategy = ProjectViewStrategy; type TreeViewMap = Map; export function createTreeViewStrategy( - cliTaskProvider: CliTaskProvider + cliTaskProvider: ViewDataProvider ): TreeViewStrategy { const listView = new TreeView(cliTaskProvider); return { @@ -25,7 +23,7 @@ export function createTreeViewStrategy( class TreeView extends BaseView { private pathHelper = new PathHelper(); - constructor(cliTaskProvider: CliTaskProvider) { + constructor(cliTaskProvider: ViewDataProvider) { super(cliTaskProvider); }