fix(core): change to use init generator during import (#30029)

<!-- 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 -->
- call init generator using implementationFactory, causing issue because
schema.json file isnt respected, and then NX_INTERACTIVE is never set to
true

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
- change back to run init command

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

Fixes #
This commit is contained in:
Emily Xiong 2025-02-13 19:02:12 -08:00 committed by GitHub
parent 3c0820f707
commit ef08108f7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 57 deletions

View File

@ -85,6 +85,9 @@ exports[`Webpack Plugin (legacy) ConvertConfigToWebpackPlugin, should convert wi
} }
} }
}, },
"lint": {
"executor": "@nx/eslint:lint"
},
"test": { "test": {
"executor": "@nx/vite:test", "executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"], "outputs": ["{options.reportsDirectory}"],
@ -92,9 +95,6 @@ exports[`Webpack Plugin (legacy) ConvertConfigToWebpackPlugin, should convert wi
"reportsDirectory": "../coverage/app3224373" "reportsDirectory": "../coverage/app3224373"
} }
}, },
"lint": {
"executor": "@nx/eslint:lint"
},
"serve-static": { "serve-static": {
"executor": "@nx/web:file-server", "executor": "@nx/web:file-server",
"dependsOn": ["build"], "dependsOn": ["build"],

View File

@ -111,29 +111,15 @@ async function initializePlugin(
options: AddOptions, options: AddOptions,
nxJson: NxJsonConfiguration nxJson: NxJsonConfiguration
): Promise<void> { ): Promise<void> {
const parsedCommandArgs: { [key: string]: any } = yargsParser( let updatePackageScripts = false;
options.__overrides_unparsed__, if (
{ coreNxPluginVersions.has(pkgName) &&
configuration: { (options.updatePackageScripts ||
'parse-numbers': false,
'parse-positional-numbers': false,
'dot-notation': false,
'camel-case-expansion': false,
},
}
);
if (coreNxPluginVersions.has(pkgName)) {
parsedCommandArgs.keepExistingVersions = true;
if (
options.updatePackageScripts ||
(options.updatePackageScripts === undefined && (options.updatePackageScripts === undefined &&
nxJson.useInferencePlugins !== false && nxJson.useInferencePlugins !== false &&
process.env.NX_ADD_PLUGINS !== 'false') process.env.NX_ADD_PLUGINS !== 'false'))
) { ) {
parsedCommandArgs.updatePackageScripts = true; updatePackageScripts = true;
}
} }
const spinner = ora(`Initializing ${pkgName}...`); const spinner = ora(`Initializing ${pkgName}...`);
@ -143,8 +129,8 @@ async function initializePlugin(
await installPlugin( await installPlugin(
pkgName, pkgName,
workspaceRoot, workspaceRoot,
options.verbose, updatePackageScripts,
parsedCommandArgs options.verbose
); );
} catch (e) { } catch (e) {
spinner.fail(); spinner.fail();

View File

@ -1,19 +1,13 @@
import * as createSpinner from 'ora'; import * as createSpinner from 'ora';
import { bold } from 'chalk'; import { bold } from 'chalk';
import { execSync } from 'child_process';
import { import {
getPackageManagerCommand, getPackageManagerCommand,
PackageManagerCommands, PackageManagerCommands,
} from '../../utils/package-manager'; } from '../../utils/package-manager';
import { GitRepository } from '../../utils/git-utils';
import { output } from '../../utils/output'; import { output } from '../../utils/output';
import { flushChanges, FsTree } from '../../generators/tree'; import { GeneratorsJsonEntry } from '../../config/misc-interfaces';
import {
Generator as NxGenerator,
GeneratorCallback,
GeneratorsJsonEntry,
} from '../../config/misc-interfaces';
import { getGeneratorInformation } from '../generate/generator-utils';
import { workspaceRoot } from '../../utils/workspace-root'; import { workspaceRoot } from '../../utils/workspace-root';
import { addDepsToPackageJson, runInstall } from './implementation/utils'; import { addDepsToPackageJson, runInstall } from './implementation/utils';
import { getPluginCapabilities } from '../../utils/plugins'; import { getPluginCapabilities } from '../../utils/plugins';
@ -40,17 +34,18 @@ export function runPackageManagerInstallPlugins(
* Installs a plugin by running its init generator. It will change the file system tree passed in. * Installs a plugin by running its init generator. It will change the file system tree passed in.
* @param plugin The name of the plugin to install * @param plugin The name of the plugin to install
* @param repoRoot repo root * @param repoRoot repo root
* @param verbose verbose * @param pmc package manager commands
* @param options options passed to init generator * @param updatePackageScripts whether to update package scripts
* @param verbose whether to run in verbose mode
* @returns void * @returns void
*/ */
export async function installPlugin( export async function installPlugin(
plugin: string, plugin: string,
repoRoot: string = workspaceRoot, repoRoot: string = workspaceRoot,
updatePackageScripts: boolean = false,
verbose: boolean = false, verbose: boolean = false,
options: { [k: string]: any } pmc: PackageManagerCommands = getPackageManagerCommand()
): Promise<void> { ): Promise<void> {
const host = new FsTree(repoRoot, verbose, `install ${plugin}`);
const capabilities = await getPluginCapabilities(repoRoot, plugin, {}); const capabilities = await getPluginCapabilities(repoRoot, plugin, {});
const generators = capabilities?.generators; const generators = capabilities?.generators;
if (!generators) { if (!generators) {
@ -64,19 +59,16 @@ export async function installPlugin(
}); });
return; return;
} }
const { implementationFactory } = getGeneratorInformation( execSync(
plugin, `${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${
initGenerator, updatePackageScripts ? '--updatePackageScripts' : ''
repoRoot, } ${verbose ? '--verbose' : ''}`,
{} {
stdio: [0, 1, 2],
cwd: repoRoot,
windowsHide: false,
}
); );
const implementation: NxGenerator = implementationFactory();
const task: GeneratorCallback | void = await implementation(host, options);
flushChanges(repoRoot, host.listChanges());
if (task) {
await task();
}
} }
/** /**
@ -87,6 +79,7 @@ export async function installPlugin(
export async function installPlugins( export async function installPlugins(
plugins: string[], plugins: string[],
updatePackageScripts: boolean, updatePackageScripts: boolean,
pmc: PackageManagerCommands,
repoRoot: string = workspaceRoot, repoRoot: string = workspaceRoot,
verbose: boolean = false verbose: boolean = false
): Promise<{ ): Promise<{
@ -108,13 +101,7 @@ export async function installPlugins(
for (const plugin of plugins) { for (const plugin of plugins) {
try { try {
spinner.start('Installing plugin ' + plugin); spinner.start('Installing plugin ' + plugin);
await installPlugin(plugin, repoRoot, verbose, { await installPlugin(plugin, repoRoot, updatePackageScripts, verbose, pmc);
keepExistingVersions: true,
updatePackageScripts,
addPlugin: true,
skipFormat: false,
skipPackageJson: false,
});
succeededPlugins.push(plugin); succeededPlugins.push(plugin);
spinner.succeed('Installed plugin ' + plugin); spinner.succeed('Installed plugin ' + plugin);
} catch (e) { } catch (e) {
@ -154,6 +141,7 @@ export async function configurePlugins(
let { succeededPlugins, failedPlugins } = await installPlugins( let { succeededPlugins, failedPlugins } = await installPlugins(
plugins, plugins,
updatePackageScripts, updatePackageScripts,
pmc,
repoRoot, repoRoot,
verbose verbose
); );