cleanup(core): improve list command performance

This commit is contained in:
Phillip Barta 2022-01-18 20:44:25 +01:00 committed by Victor Savkin
parent 4b21ab9502
commit 75eb1c532f
6 changed files with 57 additions and 59 deletions

View File

@ -37,8 +37,6 @@ Show help
### plugin ### plugin
Default: `null`
The name of an installed plugin to query The name of an installed plugin to query
### version ### version

View File

@ -1,4 +1,3 @@
import * as yargs from 'yargs';
import { appRootPath } from '@nrwl/tao/src/utils/app-root'; import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { output } from '../utilities/output'; import { output } from '../utilities/output';
import { import {
@ -11,24 +10,11 @@ import {
listPluginCapabilities, listPluginCapabilities,
} from '../utilities/plugins'; } from '../utilities/plugins';
export interface YargsListArgs extends yargs.Arguments, ListArgs {} export interface ListArgs {
/** The name of an installed plugin to query */
interface ListArgs { plugin?: string | undefined;
plugin?: string;
} }
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 * List available plugins or capabilities within a specific plugin
* *
@ -37,7 +23,7 @@ export const list = {
* Must be run within an Nx workspace * Must be run within an Nx workspace
* *
*/ */
async function listHandler(args: YargsListArgs) { export async function listHandler(args: ListArgs): Promise<void> {
if (args.plugin) { if (args.plugin) {
listPluginCapabilities(args.plugin); listPluginCapabilities(args.plugin);
} else { } else {

View File

@ -11,6 +11,7 @@ import { generateDaemonHelpOutput } from '../core/project-graph/daemon/client/ge
import { nxVersion } from '../utils/versions'; import { nxVersion } from '../utils/versions';
import { examples } from './examples'; import { examples } from './examples';
import { appRootPath } from '@nrwl/tao/src/utils/app-root'; import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import type { ListArgs } from './list';
const noop = (yargs: yargs.Argv): yargs.Argv => yargs; const noop = (yargs: yargs.Argv): yargs.Argv => yargs;
@ -291,7 +292,17 @@ npx nx daemon
} }
) )
.command(require('./report').report) .command(require('./report').report)
.command(require('./list').list) .command<ListArgs>({
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({
command: 'reset', command: 'reset',
describe: describe:

View File

@ -32,17 +32,13 @@ export async function fetchCommunityPlugins(): Promise<CommunityPlugin[]> {
} }
export function listCommunityPlugins( export function listCommunityPlugins(
installedPlugins: PluginCapabilities[], installedPlugins: Map<string, PluginCapabilities>,
communityPlugins?: CommunityPlugin[] communityPlugins?: CommunityPlugin[]
): void { ): void {
if (!communityPlugins) return; if (!communityPlugins) return;
const installedPluginsMap: Set<string> = new Set<string>(
installedPlugins.map((p) => p.name)
);
const availableCommunityPlugins = communityPlugins.filter( const availableCommunityPlugins = communityPlugins.filter(
(p) => !installedPluginsMap.has(p.name) (p) => !installedPlugins.has(p.name)
); );
output.log({ output.log({

View File

@ -61,15 +61,11 @@ export function fetchCorePlugins() {
} }
export function listCorePlugins( export function listCorePlugins(
installedPlugins: PluginCapabilities[], installedPlugins: Map<string, PluginCapabilities>,
corePlugins: CorePlugin[] corePlugins: CorePlugin[]
): void { ): void {
const installedPluginsMap: Set<string> = new Set<string>(
installedPlugins.map((p) => p.name)
);
const alsoAvailable = corePlugins.filter( const alsoAvailable = corePlugins.filter(
(p) => !installedPluginsMap.has(p.name) (p) => !installedPlugins.has(p.name)
); );
if (alsoAvailable.length) { if (alsoAvailable.length) {

View File

@ -9,7 +9,7 @@ export function getInstalledPluginsFromPackageJson(
workspaceRoot: string, workspaceRoot: string,
corePlugins: CorePlugin[], corePlugins: CorePlugin[],
communityPlugins: CommunityPlugin[] = [] communityPlugins: CommunityPlugin[] = []
): Array<PluginCapabilities> { ): Map<string, PluginCapabilities> {
const packageJson = readJsonFile(`${workspaceRoot}/package.json`); const packageJson = readJsonFile(`${workspaceRoot}/package.json`);
const plugins = new Set([ const plugins = new Set([
@ -19,34 +19,45 @@ export function getInstalledPluginsFromPackageJson(
...Object.keys(packageJson.devDependencies || {}), ...Object.keys(packageJson.devDependencies || {}),
]); ]);
return Array.from(plugins) return new Map(
.filter((name) => { Array.from(plugins)
try { .filter((name) => {
// Check for `package.json` existence instead of requiring the module itself try {
// because malformed entries like `main`, may throw false exceptions. // Check for `package.json` existence instead of requiring the module itself
require.resolve(`${name}/package.json`, { paths: [workspaceRoot] }); // because malformed entries like `main`, may throw false exceptions.
return true; require.resolve(`${name}/package.json`, { paths: [workspaceRoot] });
} catch { return true;
return false; } catch {
} return false;
}) }
.sort() })
.map((name) => getPluginCapabilities(workspaceRoot, name)) .sort()
.filter((x) => x && !!(x.generators || x.executors)); .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<string, PluginCapabilities>
) {
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({ output.log({
title: `Installed plugins:`, title: `Installed plugins:`,
bodyLines: installedPlugins.map((p) => { bodyLines: bodyLines,
const capabilities = [];
if (hasElements(p.executors)) {
capabilities.push('builders');
}
if (hasElements(p.generators)) {
capabilities.push('generators');
}
return `${chalk.bold(p.name)} (${capabilities.join()})`;
}),
}); });
} }