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
Default: `null`
The name of an installed plugin to query
### version

View File

@ -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<void> {
if (args.plugin) {
listPluginCapabilities(args.plugin);
} else {

View File

@ -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<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: 'reset',
describe:

View File

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

View File

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

View File

@ -9,7 +9,7 @@ export function getInstalledPluginsFromPackageJson(
workspaceRoot: string,
corePlugins: CorePlugin[],
communityPlugins: CommunityPlugin[] = []
): Array<PluginCapabilities> {
): Map<string, PluginCapabilities> {
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<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({
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,
});
}