diff --git a/docs/generated/cli/list.md b/docs/generated/cli/list.md index 899aa61222..79d632710a 100644 --- a/docs/generated/cli/list.md +++ b/docs/generated/cli/list.md @@ -37,8 +37,6 @@ Show help ### plugin -Default: `null` - The name of an installed plugin to query ### version diff --git a/packages/workspace/src/command-line/list.ts b/packages/workspace/src/command-line/list.ts index a1a6f39666..aee3a09b29 100644 --- a/packages/workspace/src/command-line/list.ts +++ b/packages/workspace/src/command-line/list.ts @@ -1,4 +1,3 @@ -import * as yargs from 'yargs'; import { appRootPath } from '@nrwl/tao/src/utils/app-root'; import { output } from '../utilities/output'; import { @@ -11,24 +10,11 @@ import { listPluginCapabilities, } from '../utilities/plugins'; -export interface YargsListArgs extends yargs.Arguments, ListArgs {} - -interface ListArgs { - plugin?: string; +export interface ListArgs { + /** The name of an installed plugin to query */ + plugin?: string | undefined; } -export const list = { - command: 'list [plugin]', - describe: - 'Lists installed plugins, capabilities of installed plugins and other available plugins.', - builder: (yargs: yargs.Argv) => - yargs.positional('plugin', { - default: null, - description: 'The name of an installed plugin to query', - }), - handler: listHandler, -}; - /** * List available plugins or capabilities within a specific plugin * @@ -37,7 +23,7 @@ export const list = { * Must be run within an Nx workspace * */ -async function listHandler(args: YargsListArgs) { +export async function listHandler(args: ListArgs): Promise { if (args.plugin) { listPluginCapabilities(args.plugin); } else { diff --git a/packages/workspace/src/command-line/nx-commands.ts b/packages/workspace/src/command-line/nx-commands.ts index 8351558f01..487c51df2f 100644 --- a/packages/workspace/src/command-line/nx-commands.ts +++ b/packages/workspace/src/command-line/nx-commands.ts @@ -11,6 +11,7 @@ import { generateDaemonHelpOutput } from '../core/project-graph/daemon/client/ge import { nxVersion } from '../utils/versions'; import { examples } from './examples'; import { appRootPath } from '@nrwl/tao/src/utils/app-root'; +import type { ListArgs } from './list'; const noop = (yargs: yargs.Argv): yargs.Argv => yargs; @@ -291,7 +292,17 @@ npx nx daemon } ) .command(require('./report').report) - .command(require('./list').list) + .command({ + command: 'list [plugin]', + describe: + 'Lists installed plugins, capabilities of installed plugins and other available plugins.', + builder: (yargs) => + yargs.positional('plugin', { + type: 'string', + description: 'The name of an installed plugin to query', + }), + handler: async (args) => (await import('./list')).listHandler(args), + }) .command({ command: 'reset', describe: diff --git a/packages/workspace/src/utilities/plugins/community-plugins.ts b/packages/workspace/src/utilities/plugins/community-plugins.ts index f25c68f1c1..dd8f84c66f 100644 --- a/packages/workspace/src/utilities/plugins/community-plugins.ts +++ b/packages/workspace/src/utilities/plugins/community-plugins.ts @@ -32,17 +32,13 @@ export async function fetchCommunityPlugins(): Promise { } export function listCommunityPlugins( - installedPlugins: PluginCapabilities[], + installedPlugins: Map, communityPlugins?: CommunityPlugin[] ): void { if (!communityPlugins) return; - const installedPluginsMap: Set = new Set( - installedPlugins.map((p) => p.name) - ); - const availableCommunityPlugins = communityPlugins.filter( - (p) => !installedPluginsMap.has(p.name) + (p) => !installedPlugins.has(p.name) ); output.log({ diff --git a/packages/workspace/src/utilities/plugins/core-plugins.ts b/packages/workspace/src/utilities/plugins/core-plugins.ts index 2826b3ca21..7ca9f126e4 100644 --- a/packages/workspace/src/utilities/plugins/core-plugins.ts +++ b/packages/workspace/src/utilities/plugins/core-plugins.ts @@ -61,15 +61,11 @@ export function fetchCorePlugins() { } export function listCorePlugins( - installedPlugins: PluginCapabilities[], + installedPlugins: Map, corePlugins: CorePlugin[] ): void { - const installedPluginsMap: Set = new Set( - installedPlugins.map((p) => p.name) - ); - const alsoAvailable = corePlugins.filter( - (p) => !installedPluginsMap.has(p.name) + (p) => !installedPlugins.has(p.name) ); if (alsoAvailable.length) { diff --git a/packages/workspace/src/utilities/plugins/installed-plugins.ts b/packages/workspace/src/utilities/plugins/installed-plugins.ts index 895f66da84..356d162fbe 100644 --- a/packages/workspace/src/utilities/plugins/installed-plugins.ts +++ b/packages/workspace/src/utilities/plugins/installed-plugins.ts @@ -9,7 +9,7 @@ export function getInstalledPluginsFromPackageJson( workspaceRoot: string, corePlugins: CorePlugin[], communityPlugins: CommunityPlugin[] = [] -): Array { +): Map { const packageJson = readJsonFile(`${workspaceRoot}/package.json`); const plugins = new Set([ @@ -19,34 +19,45 @@ export function getInstalledPluginsFromPackageJson( ...Object.keys(packageJson.devDependencies || {}), ]); - return Array.from(plugins) - .filter((name) => { - try { - // Check for `package.json` existence instead of requiring the module itself - // because malformed entries like `main`, may throw false exceptions. - require.resolve(`${name}/package.json`, { paths: [workspaceRoot] }); - return true; - } catch { - return false; - } - }) - .sort() - .map((name) => getPluginCapabilities(workspaceRoot, name)) - .filter((x) => x && !!(x.generators || x.executors)); + return new Map( + Array.from(plugins) + .filter((name) => { + try { + // Check for `package.json` existence instead of requiring the module itself + // because malformed entries like `main`, may throw false exceptions. + require.resolve(`${name}/package.json`, { paths: [workspaceRoot] }); + return true; + } catch { + return false; + } + }) + .sort() + .map<[string, PluginCapabilities]>((name) => [ + name, + getPluginCapabilities(workspaceRoot, name), + ]) + .filter(([, x]) => x && !!(x.generators || x.executors)) + ); } -export function listInstalledPlugins(installedPlugins: PluginCapabilities[]) { +export function listInstalledPlugins( + installedPlugins: Map +) { + const bodyLines: string[] = []; + + for (const [, p] of installedPlugins) { + const capabilities = []; + if (hasElements(p.executors)) { + capabilities.push('executors'); + } + if (hasElements(p.generators)) { + capabilities.push('generators'); + } + bodyLines.push(`${chalk.bold(p.name)} (${capabilities.join()})`); + } + output.log({ title: `Installed plugins:`, - bodyLines: installedPlugins.map((p) => { - const capabilities = []; - if (hasElements(p.executors)) { - capabilities.push('builders'); - } - if (hasElements(p.generators)) { - capabilities.push('generators'); - } - return `${chalk.bold(p.name)} (${capabilities.join()})`; - }), + bodyLines: bodyLines, }); }