Skip to content

Commit

Permalink
refactor: show at least folders if node_modules are not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoVogel committed Oct 25, 2022
1 parent f22b477 commit 9943936
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 } from 'vscode';
import {
NxFolderTreeItem,
NxListViewItem,
Expand Down
25 changes: 18 additions & 7 deletions libs/vscode/nx-project-view/src/lib/views/nx-project-base-view.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 {
Expand All @@ -12,8 +11,20 @@ export type ProjectViewStrategy<T> = Required<
Pick<TreeDataProvider<T>, 'getChildren'>
>;

type RootPath = string;
export type ProjectDefinition =
| {
[projectName: string]: ProjectConfiguration;
}
| { [projectName: string]: RootPath };

export interface ProjectInfoProvider {
getWorkspacePath(): string;
getProjects(): Promise<ProjectDefinition>;
}

export abstract class BaseView {
constructor(protected readonly cliTaskProvider: CliTaskProvider) {}
constructor(protected readonly infoProvider: ProjectInfoProvider) {}

createProjectTreeItem([projectName, { root, name, targets }]: [
projectName: string,
Expand All @@ -30,7 +41,7 @@ export abstract class BaseView {

return new NxProjectTreeItem(
nxProject,
this.cliTaskProvider.getWorkspacePath(),
this.infoProvider.getWorkspacePath(),
projectName,
hasChildren
? TreeItemCollapsibleState.Collapsed
Expand All @@ -41,10 +52,10 @@ export abstract class BaseView {
async createTargetsFromProject(parent: NxProjectTreeItem) {
const { nxProject } = parent;

const projectDef = (await this.cliTaskProvider.getProjects())[
const projectDef = (await this.infoProvider.getProjects())[
nxProject.project
];
if (!projectDef) {
if (!projectDef || typeof projectDef === 'string') {
return;
}

Expand Down Expand Up @@ -79,10 +90,10 @@ export abstract class BaseView {
async createConfigurationsFromTarget(parent: NxTargetTreeItem) {
const { nxProject, nxTarget } = parent;

const projectDef = (await this.cliTaskProvider.getProjects())[
const projectDef = (await this.infoProvider.getProjects())[
nxProject.project
];
if (!projectDef) {
if (!projectDef || typeof projectDef === 'string') {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ListView extends BaseView {
}

private async createProjects() {
const projectDefs = await this.cliTaskProvider.getProjects();
const projectDefs = await this.infoProvider.getProjects();
return Object.entries(projectDefs).map((project) =>
this.createProjectTreeItem(project)
);
Expand Down
42 changes: 25 additions & 17 deletions libs/vscode/nx-project-view/src/lib/views/nx-project-tree-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ import {
NxProjectTreeItem,
NxTreeViewItem,
} from '../nx-project-tree-item';
import { BaseView, ProjectViewStrategy } from './nx-project-base-view';
import { isDefined, PathHelper } from './nx-project-util';
import {
BaseView,
ProjectDefinition,
ProjectViewStrategy,
} from './nx-project-base-view';
import { isDefined, objectEntries, PathHelper } from './nx-project-util';

export type TreeViewStrategy = ProjectViewStrategy<NxTreeViewItem>;
type TreeViewMap = Map<string, [string, ProjectConfiguration][]>;
type TreeViewMapValue = Array<
[string, ProjectConfiguration | string] | [string, string]
>;
type TreeViewMap = Map<string, TreeViewMapValue>;

export function createTreeViewStrategy(
cliTaskProvider: CliTaskProvider
Expand Down Expand Up @@ -43,15 +50,15 @@ class TreeView extends BaseView {
}

private async createRootFolders() {
const projectDefs = await this.cliTaskProvider.getProjects();
const projectDefs = await this.infoProvider.getProjects();
const map = this.groupByRootPath(projectDefs);

const rootFolders = this.getRootFolders(map);
return rootFolders.map(([path]) => this.createTreeItemFromPath(path));
}

private async createFoldersOrProjectFromFolder(parent: NxFolderTreeItem) {
const projectDefs = await this.cliTaskProvider.getProjects();
const projectDefs = await this.infoProvider.getProjects();
const map = this.groupByRootPath(projectDefs);

const subFolders = this.getSubFolders(map, parent.path);
Expand All @@ -60,13 +67,14 @@ class TreeView extends BaseView {
);
}

private createTreeItemFromPath(
path: string,
projects?: [string, ProjectConfiguration][]
) {
private createTreeItemFromPath(path: string, projects?: TreeViewMapValue) {
if (projects && projects.length === 1) {
const [project] = projects;
return this.createProjectTreeItem(project);
const [[projectName, projectDef]] = projects;

if (typeof projectDef === 'string') {
return;
}
return this.createProjectTreeItem([projectName, projectDef]);
}
return this.createFolderTreeItem(path);
}
Expand All @@ -75,7 +83,7 @@ class TreeView extends BaseView {
const folderName = this.pathHelper.getFolderName(path);
return new NxFolderTreeItem(
path,
this.cliTaskProvider.getWorkspacePath(),
this.infoProvider.getWorkspacePath(),
folderName,
TreeItemCollapsibleState.Collapsed
);
Expand All @@ -86,13 +94,13 @@ class TreeView extends BaseView {
* Each entry is added n times.
* n is determined by the directory depth.
*/
private groupByRootPath(projectDefs: {
[projectName: string]: ProjectConfiguration;
}): TreeViewMap {
return Object.entries(projectDefs)
private groupByRootPath(projectDefs: ProjectDefinition): TreeViewMap {
return objectEntries(projectDefs)
.flatMap((project) => {
const [projectName, projectDef] = project;
const { root } = projectDef;
const nodeModulesInstalled = typeof projectDef === 'string';

const root = nodeModulesInstalled ? projectDef : projectDef.root;
if (root === undefined) {
getOutputChannel().appendLine(
`Project ${projectName} has no root. This could be because of an error loading the workspace configuration.`
Expand Down
8 changes: 8 additions & 0 deletions libs/vscode/nx-project-view/src/lib/views/nx-project-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ export function isDefined<T>(val?: T): val is T {
return !!val;
}

type Entries<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T][];

export function objectEntries<T extends object>(val: T) {
return Object.entries(val) as Entries<T>;
}

export class PathHelper {
constructor(private pathApi: typeof path = path) {}

Expand Down

0 comments on commit 9943936

Please sign in to comment.