fix(core): run init generators from extended collections during nx add (#30280)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

When the `init` generator is in an extended collection, `nx add` will
not run it.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

When the `init` generator is in an extended collection, `nx add` will
run it.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Jason Jean 2025-03-05 18:32:44 -05:00 committed by GitHub
parent 5b034965d8
commit 121d9973a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,13 +10,13 @@ import { output } from '../../utils/output';
import { GeneratorsJsonEntry } from '../../config/misc-interfaces';
import { workspaceRoot } from '../../utils/workspace-root';
import { addDepsToPackageJson, runInstall } from './implementation/utils';
import { getPluginCapabilities } from '../../utils/plugins';
import { isAngularPluginInstalled } from '../../adapter/angular-json';
import {
isAggregateCreateNodesError,
isProjectConfigurationsError,
isProjectsWithNoNameError,
} from '../../project-graph/error-types';
import { getGeneratorInformation } from '../generate/generator-utils';
export function runPackageManagerInstallPlugins(
repoRoot: string,
@ -46,29 +46,25 @@ export async function installPlugin(
verbose: boolean = false,
pmc: PackageManagerCommands = getPackageManagerCommand()
): Promise<void> {
const capabilities = await getPluginCapabilities(repoRoot, plugin, {});
const generators = capabilities?.generators;
if (!generators) {
throw new Error(`No generators found in ${plugin}.`);
}
const initGenerator = findInitGenerator(generators);
if (!initGenerator) {
try {
getGeneratorInformation(plugin, 'init', workspaceRoot, {});
execSync(
`${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${
updatePackageScripts ? '--updatePackageScripts' : ''
} ${verbose ? '--verbose' : ''}`,
{
stdio: [0, 1, 2],
cwd: repoRoot,
windowsHide: false,
}
);
} catch {
// init generator does not exist, so this function should noop
output.log({
title: `No "init" generator found in ${plugin}. Skipping initialization.`,
});
return;
}
execSync(
`${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${
updatePackageScripts ? '--updatePackageScripts' : ''
} ${verbose ? '--verbose' : ''}`,
{
stdio: [0, 1, 2],
cwd: repoRoot,
windowsHide: false,
}
);
}
/**