/
plugin-capabilities.ts
118 lines (106 loc) · 3.27 KB
/
plugin-capabilities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { workspaceRoot } from '../workspace-root';
import * as chalk from 'chalk';
import { dirname, join } from 'path';
import { output } from '../output';
import type { PluginCapabilities } from './models';
import { hasElements } from './shared';
import { readJsonFile } from '../fileutils';
import { getPackageManagerCommand } from '../package-manager';
import { readPluginPackageJson } from '../nx-plugin';
function tryGetCollection<T extends object>(
packageJsonPath: string,
collectionFile: string | undefined,
propName: string
): T | null {
if (!collectionFile) {
return null;
}
try {
const collectionFilePath = join(dirname(packageJsonPath), collectionFile);
return readJsonFile<T>(collectionFilePath)[propName];
} catch {
return null;
}
}
export function getPluginCapabilities(
workspaceRoot: string,
pluginName: string
): PluginCapabilities | null {
try {
const { json: packageJson, path: packageJsonPath } =
readPluginPackageJson(pluginName);
return {
name: pluginName,
generators:
tryGetCollection(
packageJsonPath,
packageJson.generators,
'generators'
) ||
tryGetCollection(
packageJsonPath,
packageJson.schematics,
'generators'
) ||
tryGetCollection(
packageJsonPath,
packageJson.schematics,
'generators'
) ||
tryGetCollection(packageJsonPath, packageJson.schematics, 'schematics'),
executors:
tryGetCollection(packageJsonPath, packageJson.executors, 'executors') ||
tryGetCollection(packageJsonPath, packageJson.executors, 'builders') ||
tryGetCollection(packageJsonPath, packageJson.builders, 'executors') ||
tryGetCollection(packageJsonPath, packageJson.builders, 'builders'),
};
} catch {
return null;
}
}
export function listPluginCapabilities(pluginName: string) {
const plugin = getPluginCapabilities(workspaceRoot, pluginName);
if (!plugin) {
const pmc = getPackageManagerCommand();
output.note({
title: `${pluginName} is not currently installed`,
bodyLines: [
`Use "${pmc.addDev} ${pluginName}" to install the plugin.`,
`After that, use "${pmc.exec} nx g ${pluginName}:init" to add the required peer deps and initialize the plugin.`,
],
});
return;
}
const hasBuilders = hasElements(plugin.executors);
const hasGenerators = hasElements(plugin.generators);
if (!hasBuilders && !hasGenerators) {
output.warn({ title: `No capabilities found in ${pluginName}` });
return;
}
const bodyLines = [];
if (hasGenerators) {
bodyLines.push(chalk.bold(chalk.green('GENERATORS')));
bodyLines.push('');
bodyLines.push(
...Object.keys(plugin.generators).map(
(name) => `${chalk.bold(name)} : ${plugin.generators[name].description}`
)
);
if (hasBuilders) {
bodyLines.push('');
}
}
if (hasBuilders) {
bodyLines.push(chalk.bold(chalk.green('EXECUTORS/BUILDERS')));
bodyLines.push('');
bodyLines.push(
...Object.keys(plugin.executors).map(
(name) => `${chalk.bold(name)} : ${plugin.executors[name].description}`
)
);
}
output.log({
title: `Capabilities in ${plugin.name}:`,
bodyLines,
});
}