diff --git a/apps/vscode/src/main.ts b/apps/vscode/src/main.ts index 1b6a8b03d4..322e4d4cd2 100644 --- a/apps/vscode/src/main.ts +++ b/apps/vscode/src/main.ts @@ -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; let nxProjectTreeView: TreeView; @@ -234,7 +235,7 @@ async function setWorkspace(workspacePath: string) { WorkspaceConfigurationStore.instance.set('nxWorkspacePath', workspacePath); } - await setApplicationAndLibraryContext(workspacePath); + setApplicationAndLibraryContext(workspacePath); const isNxWorkspace = existsSync(join(workspacePath, 'nx.json')); const isAngularWorkspace = existsSync(join(workspacePath, 'angular.json')); @@ -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 }); } diff --git a/libs/npm/src/lib/workspace-dependencies.ts b/libs/npm/src/lib/workspace-dependencies.ts index 718260d2c7..6eff97961a 100644 --- a/libs/npm/src/lib/workspace-dependencies.ts +++ b/libs/npm/src/lib/workspace-dependencies.ts @@ -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'; @@ -16,13 +19,20 @@ import { */ export async function workspaceDependencies( - workspacePath: string + workspacePath: string, + projects?: WorkspaceProjects ): Promise { + 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( @@ -47,3 +57,37 @@ export async function workspaceDependencyPath( return; } } + +async function localDependencies( + workspacePath: string, + projects?: WorkspaceProjects +): Promise { + 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; +} diff --git a/libs/schema/src/index.ts b/libs/schema/src/index.ts index 556d3d200b..28ab41b553 100644 --- a/libs/schema/src/index.ts +++ b/libs/schema/src/index.ts @@ -1,3 +1,4 @@ +import { WorkspaceJsonConfiguration } from '@nrwl/devkit'; import { Schema } from 'nx/src/utils/params'; export enum OptionType { @@ -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; @@ -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'; diff --git a/libs/server/src/lib/stores/store.ts b/libs/schema/src/store.ts similarity index 100% rename from libs/server/src/lib/stores/store.ts rename to libs/schema/src/store.ts diff --git a/libs/server/src/index.ts b/libs/server/src/index.ts index 505a63e52d..f155743bdf 100644 --- a/libs/server/src/index.ts +++ b/libs/server/src/index.ts @@ -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'; diff --git a/libs/server/src/lib/stores/index.ts b/libs/server/src/lib/stores/index.ts deleted file mode 100644 index d4068169bc..0000000000 --- a/libs/server/src/lib/stores/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './store'; diff --git a/libs/server/src/lib/telemetry/init.ts b/libs/server/src/lib/telemetry/init.ts index d68682f666..a05e9cb88d 100644 --- a/libs/server/src/lib/telemetry/init.ts +++ b/libs/server/src/lib/telemetry/init.ts @@ -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; diff --git a/libs/server/src/lib/telemetry/telemetry.ts b/libs/server/src/lib/telemetry/telemetry.ts index 894e33d636..78d71f8ed1 100644 --- a/libs/server/src/lib/telemetry/telemetry.ts +++ b/libs/server/src/lib/telemetry/telemetry.ts @@ -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[] = []; diff --git a/libs/server/src/lib/telemetry/user.ts b/libs/server/src/lib/telemetry/user.ts index 6eb6f65660..936f34ca87 100644 --- a/libs/server/src/lib/telemetry/user.ts +++ b/libs/server/src/lib/telemetry/user.ts @@ -1,4 +1,4 @@ -import { Store } from '../stores/store'; +import { Store } from '@nx-console/schema'; export type UserState = 'untracked' | 'tracked'; diff --git a/libs/server/src/lib/utils/get-executors.ts b/libs/server/src/lib/utils/get-executors.ts index 770939833d..74cfcff9ba 100644 --- a/libs/server/src/lib/utils/get-executors.ts +++ b/libs/server/src/lib/utils/get-executors.ts @@ -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 { - return (await readCollections(workspacePath, clearPackageJsonCache)).filter( - (collection) => collection.type === 'executor' - ); + return ( + await readCollections(workspacePath, { projects, clearPackageJsonCache }) + ).filter((collection) => collection.type === 'executor'); } diff --git a/libs/server/src/lib/utils/get-generators.ts b/libs/server/src/lib/utils/get-generators.ts index 76502b943f..453c806e81 100644 --- a/libs/server/src/lib/utils/get-generators.ts +++ b/libs/server/src/lib/utils/get-generators.ts @@ -1,6 +1,11 @@ -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, @@ -8,13 +13,16 @@ import { normalizeSchema, readAndCacheJsonFile, } from './utils'; -import { getCollectionInfo, readCollections } from './read-collections'; export async function getGenerators( - workspacePath: string + workspacePath: string, + projects?: WorkspaceProjects ): Promise { 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' ); diff --git a/libs/server/src/lib/utils/read-collections.ts b/libs/server/src/lib/utils/read-collections.ts index 086a2f8ee8..2fc94c119e 100644 --- a/libs/server/src/lib/utils/read-collections.ts +++ b/libs/server/src/lib/utils/read-collections.ts @@ -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 { - 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) => { diff --git a/libs/server/src/lib/utils/read-projects.ts b/libs/server/src/lib/utils/read-projects.ts index 613d364d1b..ae617f7476 100644 --- a/libs/server/src/lib/utils/read-projects.ts +++ b/libs/server/src/lib/utils/read-projects.ts @@ -1,6 +1,5 @@ import { Targets, - Project, Option, DefaultValue, TargetConfiguration, diff --git a/libs/vscode/configuration/src/lib/configuration-keys.ts b/libs/vscode/configuration/src/lib/configuration-keys.ts index d52cdf3e51..6186caca40 100644 --- a/libs/vscode/configuration/src/lib/configuration-keys.ts +++ b/libs/vscode/configuration/src/lib/configuration-keys.ts @@ -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 diff --git a/libs/vscode/configuration/src/lib/global-configuration-store.ts b/libs/vscode/configuration/src/lib/global-configuration-store.ts index ce726612b2..9f3b7d547c 100644 --- a/libs/vscode/configuration/src/lib/global-configuration-store.ts +++ b/libs/vscode/configuration/src/lib/global-configuration-store.ts @@ -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; diff --git a/libs/vscode/configuration/src/lib/workspace-configuration-store.ts b/libs/vscode/configuration/src/lib/workspace-configuration-store.ts index 895b745d34..4686fb9328 100644 --- a/libs/vscode/configuration/src/lib/workspace-configuration-store.ts +++ b/libs/vscode/configuration/src/lib/workspace-configuration-store.ts @@ -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'; diff --git a/libs/vscode/json-schema/src/lib/project-json-schema.ts b/libs/vscode/json-schema/src/lib/project-json-schema.ts index 97004cf8fa..339f1c0af8 100644 --- a/libs/vscode/json-schema/src/lib/project-json-schema.ts +++ b/libs/vscode/json-schema/src/lib/project-json-schema.ts @@ -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'; @@ -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); diff --git a/libs/vscode/json-schema/src/lib/workspace-json-schema.ts b/libs/vscode/json-schema/src/lib/workspace-json-schema.ts index 34328a8ca4..8950e7eeaa 100644 --- a/libs/vscode/json-schema/src/lib/workspace-json-schema.ts +++ b/libs/vscode/json-schema/src/lib/workspace-json-schema.ts @@ -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'; @@ -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); diff --git a/libs/vscode/tasks/src/lib/cli-task-commands.ts b/libs/vscode/tasks/src/lib/cli-task-commands.ts index 3fd812b583..23b53b0400 100644 --- a/libs/vscode/tasks/src/lib/cli-task-commands.ts +++ b/libs/vscode/tasks/src/lib/cli-task-commands.ts @@ -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' ) @@ -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' ) diff --git a/libs/vscode/tasks/src/lib/cli-task-provider.ts b/libs/vscode/tasks/src/lib/cli-task-provider.ts index 545cdd3bbc..7749191348 100644 --- a/libs/vscode/tasks/src/lib/cli-task-provider.ts +++ b/libs/vscode/tasks/src/lib/cli-task-provider.ts @@ -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'; @@ -112,7 +115,9 @@ export class CliTaskProvider implements TaskProvider { }); } - async getProjects(json?: WorkspaceJsonConfiguration) { + async getProjects( + json?: WorkspaceJsonConfiguration + ): Promise { if (json) { return json.projects; } else { diff --git a/libs/vscode/tasks/src/lib/select-generator.ts b/libs/vscode/tasks/src/lib/select-generator.ts index d0064f7447..0d4adaae37 100644 --- a/libs/vscode/tasks/src/lib/select-generator.ts +++ b/libs/vscode/tasks/src/lib/select-generator.ts @@ -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 => {