Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list local plugins #1274

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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 @@ -234,7 +235,7 @@ async function setWorkspace(workspacePath: string) {
WorkspaceConfigurationStore.instance.set('nxWorkspacePath', workspacePath);
}

await setApplicationAndLibraryContext(workspacePath);
setApplicationAndLibraryContext(workspacePath);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the await because I want this to be "fired and forgotten"


const isNxWorkspace = existsSync(join(workspacePath, 'nx.json'));
const isAngularWorkspace = existsSync(join(workspacePath, 'angular.json'));
Expand All @@ -260,6 +261,7 @@ async function setWorkspace(workspacePath: string) {
workspaceType = 'angular';
}
WorkspaceConfigurationStore.instance.set('workspaceType', workspaceType);
WorkspaceConfigurationStore.instance.set('nxVersion', nxVersion());

getTelemetry().record('WorkspaceType', { workspaceType });
}
Expand Down
50 changes: 47 additions & 3 deletions libs/npm/src/lib/workspace-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { WorkspaceProjects } from '@nx-console/schema';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import { stat } from 'fs/promises';
import { join } from 'path';
import { FileType, Uri, workspace } from 'vscode';
import { npmDependencies } from './npm-dependencies';
Expand All @@ -16,13 +19,20 @@ import {
*/

export async function workspaceDependencies(
workspacePath: string
workspacePath: string,
projects?: WorkspaceProjects
): Promise<string[]> {
const dependencies: string[] = [];

dependencies.push(...(await localDependencies(workspacePath, projects)));

if (await isWorkspaceInPnp(workspacePath)) {
return pnpDependencies(workspacePath);
dependencies.push(...(await pnpDependencies(workspacePath)));
}

return npmDependencies(workspacePath);
dependencies.push(...(await npmDependencies(workspacePath)));

return dependencies;
}

export async function workspaceDependencyPath(
Expand All @@ -47,3 +57,37 @@ export async function workspaceDependencyPath(
return;
}
}

async function localDependencies(
workspacePath: string,
projects?: WorkspaceProjects
): Promise<string[]> {
if (!projects) {
return [];
}

const nxVersion = WorkspaceConfigurationStore.instance.get('nxVersion', null);

if (nxVersion && nxVersion < 13) {
return [];
}

const packages = Object.values(projects).map(
(project) => `${workspacePath}/${project.root}/package.json`
);

const existingPackages: string[] = [];

for (const pkg of packages) {
try {
const fileStat = await stat(pkg);
if (fileStat.isFile()) {
existingPackages.push(pkg.replace('/package.json', ''));
}
} catch {
// noop
}
}

return existingPackages;
}
12 changes: 5 additions & 7 deletions libs/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import { Schema } from 'nx/src/utils/params';

export enum OptionType {
Expand Down Expand Up @@ -98,13 +99,6 @@ export interface TargetConfiguration {
defaultValues: DefaultValue[];
}

export interface Project {
name: string;
root: string;
projectType: string;
targets: Targets[];
}

export interface Targets {
name: string;
project: string;
Expand All @@ -116,3 +110,7 @@ export interface Targets {

export const WORKSPACE_GENERATOR_NAME_REGEX =
/^workspace-(schematic|generator):(.+)/;

export type WorkspaceProjects = WorkspaceJsonConfiguration['projects'];

export { Store } from './store';
File renamed without changes.
1 change: 0 additions & 1 deletion libs/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './lib/abstract-tree-provider';
export * from './lib/stores';
export * from './lib/telemetry';
export * from './lib/utils/output-channel';
export * from './lib/utils/read-projects';
Expand Down
1 change: 0 additions & 1 deletion libs/server/src/lib/stores/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions libs/server/src/lib/telemetry/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Telemetry } from './telemetry';
import { Disposable, window, workspace } from 'vscode';

import { Store } from '../stores';
import { Store } from '@nx-console/schema';

let telemetry: Telemetry;
let disposer: Disposable | null = null;
Expand Down
2 changes: 1 addition & 1 deletion libs/server/src/lib/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Sink } from './sink';
import { LoggerSink, GoogleAnalyticsSink, ApplicationPlatform } from './sinks';
import { User, UserState } from './user';
import { TelemetryMessageBuilder } from './message-builder';
import { Store } from '../stores';
import { Store } from '@nx-console/schema';

export class Telemetry implements TelemetryMessageBuilder {
readonly sinks: Sink[] = [];
Expand Down
2 changes: 1 addition & 1 deletion libs/server/src/lib/telemetry/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Store } from '../stores/store';
import { Store } from '@nx-console/schema';

export type UserState = 'untracked' | 'tracked';

Expand Down
9 changes: 5 additions & 4 deletions libs/server/src/lib/utils/get-executors.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CollectionInfo } from '@nx-console/schema';
import { CollectionInfo, WorkspaceProjects } from '@nx-console/schema';
import { readCollections } from './read-collections';

export async function getExecutors(
workspacePath: string,
projects: WorkspaceProjects,
clearPackageJsonCache: boolean
): Promise<CollectionInfo[]> {
return (await readCollections(workspacePath, clearPackageJsonCache)).filter(
(collection) => collection.type === 'executor'
);
return (
await readCollections(workspacePath, { projects, clearPackageJsonCache })
).filter((collection) => collection.type === 'executor');
}
16 changes: 12 additions & 4 deletions libs/server/src/lib/utils/get-generators.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { CollectionInfo, GeneratorType } from '@nx-console/schema';
import {
CollectionInfo,
GeneratorType,
WorkspaceProjects,
} from '@nx-console/schema';
import { basename, join } from 'path';

import { getCollectionInfo, readCollections } from './read-collections';
import {
directoryExists,
fileExists,
listFiles,
normalizeSchema,
readAndCacheJsonFile,
} from './utils';
import { getCollectionInfo, readCollections } from './read-collections';

export async function getGenerators(
workspacePath: string
workspacePath: string,
projects?: WorkspaceProjects
): Promise<CollectionInfo[]> {
const basedir = workspacePath;
const collections = await readCollections(workspacePath, false);
const collections = await readCollections(workspacePath, {
projects,
clearPackageJsonCache: false,
});
let generatorCollections = collections.filter(
(collection) => collection.type === 'generator'
);
Expand Down
16 changes: 12 additions & 4 deletions libs/server/src/lib/utils/read-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ import {
workspaceDependencies,
workspaceDependencyPath,
} from '@nx-console/npm';
import { CollectionInfo, Generator, GeneratorType } from '@nx-console/schema';
import {
CollectionInfo,
Generator,
GeneratorType,
WorkspaceProjects,
} from '@nx-console/schema';
import { platform } from 'os';
import { dirname, join, resolve } from 'path';
import { clearJsonCache, readAndCacheJsonFile } from './utils';

export async function readCollections(
workspacePath: string,
clearPackageJsonCache: boolean
options: {
projects?: WorkspaceProjects;
clearPackageJsonCache?: boolean;
}
): Promise<CollectionInfo[]> {
if (clearPackageJsonCache) {
if (options?.clearPackageJsonCache) {
clearJsonCache('package.json', workspacePath);
}

const packages = await workspaceDependencies(workspacePath);
const packages = await workspaceDependencies(workspacePath, options.projects);

const collections = await Promise.all(
packages.map(async (p) => {
Expand Down
1 change: 0 additions & 1 deletion libs/server/src/lib/utils/read-projects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Targets,
Project,
Option,
DefaultValue,
TargetConfiguration,
Expand Down
1 change: 1 addition & 0 deletions libs/vscode/configuration/src/lib/configuration-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const WORKSPACE_CONFIG_KEYS = [
'nxConversionCount',
'nxConversionDoNotAskAgain',
'workspaceType',
'nxVersion',
] as const;
/**
* configuration Keys used for NxConsole on a vscode workspace level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
workspace,
Memento,
} from 'vscode';
import { Store } from '@nx-console/server';
import { Store } from '@nx-console/schema';
import { GLOBAL_CONFIG_KEYS, GlobalConfigKeys } from './configuration-keys';

let CONFIG_STORE: GlobalConfigurationStore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Store } from '@nx-console/server';
import { Store } from '@nx-console/schema';
import { ExtensionContext, Memento } from 'vscode';
import { WorkspaceConfigKeys } from './configuration-keys';

Expand Down
3 changes: 3 additions & 0 deletions libs/vscode/json-schema/src/lib/project-json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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 { join } from 'path';
import * as vscode from 'vscode';

Expand Down Expand Up @@ -35,8 +36,10 @@ export class ProjectJsonSchema {
clearPackageJsonCache = false
) {
const filePath = vscode.Uri.joinPath(extensionUri, 'project-schema.json');
const { json } = await verifyWorkspace();
const collections = await getExecutors(
workspacePath,
json.projects,
clearPackageJsonCache
);
const contents = getProjectJsonSchema(collections);
Expand Down
3 changes: 3 additions & 0 deletions libs/vscode/json-schema/src/lib/workspace-json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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 { dirname, join } from 'path';
import * as vscode from 'vscode';

Expand Down Expand Up @@ -35,8 +36,10 @@ export class WorkspaceJsonSchema {
clearPackageJsonCache = false
) {
const filePath = vscode.Uri.joinPath(extensionUri, 'workspace-schema.json');
const { json } = await verifyWorkspace();
const collections = await getExecutors(
workspacePath,
json.projects,
clearPackageJsonCache
);
const contents = await getWorkspaceJsonSchema(collections);
Expand Down
10 changes: 8 additions & 2 deletions libs/vscode/tasks/src/lib/cli-task-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export function registerCliTaskCommands(
*/
const getCorrectMoveGenerator = async () => {
const workspacePath = cliTaskProvider.getWorkspacePath();
const generators = await getGenerators(workspacePath);
const generators = await getGenerators(
workspacePath,
await cliTaskProvider.getProjects()
);
return generators.find(
(generator) => generator.name === '@nrwl/angular:move'
)
Expand Down Expand Up @@ -116,7 +119,10 @@ export function registerCliTaskCommands(
*/
const getCorrectRemoveGenerator = async () => {
const workspacePath = cliTaskProvider.getWorkspacePath();
const generators = await getGenerators(workspacePath);
const generators = await getGenerators(
workspacePath,
await cliTaskProvider.getProjects()
);
return generators.find(
(generator) => generator.name === '@nrwl/angular:remove'
)
Expand Down
9 changes: 7 additions & 2 deletions libs/vscode/tasks/src/lib/cli-task-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import { WORKSPACE_GENERATOR_NAME_REGEX } from '@nx-console/schema';
import {
WorkspaceProjects,
WORKSPACE_GENERATOR_NAME_REGEX,
} from '@nx-console/schema';
import { getTelemetry } from '@nx-console/server';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import { NxConversion } from '@nx-console/vscode/nx-conversion';
Expand Down Expand Up @@ -112,7 +115,9 @@ export class CliTaskProvider implements TaskProvider {
});
}

async getProjects(json?: WorkspaceJsonConfiguration) {
async getProjects(
json?: WorkspaceJsonConfiguration
): Promise<WorkspaceProjects> {
if (json) {
return json.projects;
} else {
Expand Down
4 changes: 2 additions & 2 deletions libs/vscode/tasks/src/lib/select-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export async function selectGenerator(
generator: Generator;
collectionPath: string;
}

const generators = await getGenerators(workspacePath);
const { json } = await verifyWorkspace();
const generators = await getGenerators(workspacePath, json.projects);
let generatorsQuickPicks = generators
.filter((collection) => !!collection.data)
.map((collection): GenerateQuickPickItem => {
Expand Down