Skip to content

Commit

Permalink
feat(core): hide hidden and deprecated generators from prompts (#14561)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jan 27, 2023
1 parent cda00d9 commit f4a4059
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/generated/packages/workspace/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"aliases": ["lib"],
"x-type": "library",
"description": "Create a library.",
"x-deprecated": "Use @nrwl/js:lib instead. This will be removed in Nx v16.",
"implementation": "/packages/workspace/src/generators/library/library#libraryGenerator.ts",
"hidden": false,
"path": "/packages/workspace/src/generators/library/schema.json",
Expand Down
71 changes: 57 additions & 14 deletions packages/nx/src/command-line/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
createProjectGraphAsync,
readProjectsConfigurationFromProjectGraph,
} from '../project-graph/project-graph';
import { logger } from '../utils/logger';
import { logger, NX_PREFIX } from '../utils/logger';
import {
combineOptionsForGenerator,
handleErrors,
Expand Down Expand Up @@ -61,12 +61,26 @@ async function promptForCollection(
);

const choicesMap = new Set<string>();

const deprecatedChoices = new Set<string>();

for (const collectionName of installedCollections) {
try {
const { resolvedCollectionName, normalizedGeneratorName } =
ws.readGenerator(collectionName, generatorName);

choicesMap.add(`${resolvedCollectionName}:${normalizedGeneratorName}`);
const {
resolvedCollectionName,
normalizedGeneratorName,
generatorConfiguration: { ['x-deprecated']: deprecated, hidden },
} = ws.readGenerator(collectionName, generatorName);
if (hidden) {
continue;
}
if (deprecated) {
deprecatedChoices.add(
`${resolvedCollectionName}:${normalizedGeneratorName}`
);
} else {
choicesMap.add(`${resolvedCollectionName}:${normalizedGeneratorName}`);
}
} catch {}
}

Expand All @@ -77,15 +91,25 @@ async function promptForCollection(
}[] = [];
for (const [name] of localPlugins) {
try {
const { resolvedCollectionName, normalizedGeneratorName } =
ws.readGenerator(name, generatorName);
const {
resolvedCollectionName,
normalizedGeneratorName,
generatorConfiguration: { ['x-deprecated']: deprecated, hidden },
} = ws.readGenerator(name, generatorName);
if (hidden) {
continue;
}
const value = `${resolvedCollectionName}:${normalizedGeneratorName}`;
if (!choicesMap.has(value)) {
choicesFromLocalPlugins.push({
name: value,
message: chalk.bold(value),
value,
});
if (deprecated) {
deprecatedChoices.add(value);
} else {
choicesFromLocalPlugins.push({
name: value,
message: chalk.bold(value),
value,
});
}
}
} catch {}
}
Expand Down Expand Up @@ -145,6 +169,13 @@ async function promptForCollection(
return customCollection
? `${customCollection}:${generatorName}`
: generator;
} else if (deprecatedChoices.size > 0) {
throw new Error(
[
`All installed generators named "${generatorName}" are deprecated. To run one, provide its full \`collection:generator\` id.`,
[...deprecatedChoices].map((x) => ` - ${x}`),
].join('\n')
);
} else {
throw new Error(`Could not find any generators named "${generatorName}"`);
}
Expand Down Expand Up @@ -289,9 +320,21 @@ export async function generate(cwd: string, args: { [k: string]: any }) {
if (opts.dryRun) {
process.env.NX_DRY_RUN = 'true';
}
const { normalizedGeneratorName, schema, implementationFactory, aliases } =
ws.readGenerator(opts.collectionName, opts.generatorName);
const {
normalizedGeneratorName,
schema,
implementationFactory,
generatorConfiguration: { aliases, hidden, ['x-deprecated']: deprecated },
} = ws.readGenerator(opts.collectionName, opts.generatorName);

if (deprecated) {
logger.warn(
[
`${NX_PREFIX}: ${opts.collectionName}:${normalizedGeneratorName} is deprecated`,
`${deprecated}`,
].join('/n')
);
}
logger.info(
`NX Generating ${opts.collectionName}:${normalizedGeneratorName}`
);
Expand Down
2 changes: 2 additions & 0 deletions packages/nx/src/config/misc-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export type Generator<T = unknown> = (
) => void | GeneratorCallback | Promise<void | GeneratorCallback>;

export interface GeneratorsJsonEntry {
hidden?: boolean;
schema: string;
implementation?: string;
factory?: string;
description?: string;
aliases?: string[];
cli?: 'nx';
'x-type'?: 'library' | 'application';
'x-deprecated'?: string;
}

export type OutputCaptureMethod = 'direct-nodejs' | 'pipe';
Expand Down
23 changes: 22 additions & 1 deletion packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ExecutorsJson,
Generator,
GeneratorsJson,
GeneratorsJsonEntry,
TaskGraphExecutor,
} from './misc-interfaces';
import { PackageJson } from '../utils/package-json';
Expand Down Expand Up @@ -185,7 +186,21 @@ export class Workspaces {
}
}

readGenerator(collectionName: string, generatorName: string) {
readGenerator(
collectionName: string,
generatorName: string
): {
resolvedCollectionName: string;
normalizedGeneratorName: string;
schema: any;
implementationFactory: () => Generator<unknown>;
isNgCompat: boolean;
/**
* @deprecated(v16): This will be removed in v16, use generatorConfiguration.aliases
*/
aliases: string[];
generatorConfiguration: GeneratorsJsonEntry;
} {
try {
const {
generatorsFilePath,
Expand All @@ -209,13 +224,19 @@ export class Workspaces {
generatorConfig.implementation,
generatorsDir
);
const normalizedGeneratorConfiguration: GeneratorsJsonEntry = {
...generatorConfig,
aliases: generatorConfig.aliases ?? [],
hidden: !!generatorConfig.hidden,
};
return {
resolvedCollectionName,
normalizedGeneratorName,
schema,
implementationFactory,
isNgCompat,
aliases: generatorConfig.aliases || [],
generatorConfiguration: normalizedGeneratorConfiguration,
};
} catch (e) {
throw new Error(
Expand Down
6 changes: 4 additions & 2 deletions packages/workspace/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"schema": "./src/generators/library/schema.json",
"aliases": ["lib"],
"x-type": "library",
"description": "Create a library."
"description": "Create a library.",
"x-deprecated": "Use @nrwl/js:lib instead. This will be removed in Nx v16"
},
"workspace-generator": {
"factory": "./src/generators/workspace-generator/workspace-generator",
Expand Down Expand Up @@ -70,7 +71,8 @@
"schema": "./src/generators/library/schema.json",
"aliases": ["lib"],
"x-type": "library",
"description": "Create a library."
"description": "Create a library.",
"x-deprecated": "Use @nrwl/js:lib instead. This will be removed in Nx v16."
},
"workspace-generator": {
"factory": "./src/generators/workspace-generator/workspace-generator",
Expand Down

1 comment on commit f4a4059

@vercel
Copy link

@vercel vercel bot commented on f4a4059 Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.