From cbbcafd382c244c8be008447f6df9173d56827e1 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Thu, 13 Feb 2025 19:02:12 -0800 Subject: [PATCH] fix(core): change to use init generator during import (#30029) ## Current Behavior - call init generator using implementationFactory, causing issue because schema.json file isnt respected, and then NX_INTERACTIVE is never set to true ## Expected Behavior - change back to run init command ## Related Issue(s) Fixes # --- .../__snapshots__/webpack.legacy.test.ts.snap | 6 +-- packages/nx/src/command-line/add/add.ts | 32 ++++-------- .../command-line/init/configure-plugins.ts | 50 +++++++------------ 3 files changed, 31 insertions(+), 57 deletions(-) diff --git a/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap b/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap index eef27f724d743..4b4b8405a5f6d 100644 --- a/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap +++ b/e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap @@ -85,6 +85,9 @@ exports[`Webpack Plugin (legacy) ConvertConfigToWebpackPlugin, should convert wi } } }, + "lint": { + "executor": "@nx/eslint:lint" + }, "test": { "executor": "@nx/vite:test", "outputs": ["{options.reportsDirectory}"], @@ -92,9 +95,6 @@ exports[`Webpack Plugin (legacy) ConvertConfigToWebpackPlugin, should convert wi "reportsDirectory": "../coverage/app3224373" } }, - "lint": { - "executor": "@nx/eslint:lint" - }, "serve-static": { "executor": "@nx/web:file-server", "dependsOn": ["build"], diff --git a/packages/nx/src/command-line/add/add.ts b/packages/nx/src/command-line/add/add.ts index 0f6f4d1172898..f664b5a8d23fe 100644 --- a/packages/nx/src/command-line/add/add.ts +++ b/packages/nx/src/command-line/add/add.ts @@ -111,29 +111,15 @@ async function initializePlugin( options: AddOptions, nxJson: NxJsonConfiguration ): Promise { - const parsedCommandArgs: { [key: string]: any } = yargsParser( - options.__overrides_unparsed__, - { - configuration: { - 'parse-numbers': false, - 'parse-positional-numbers': false, - 'dot-notation': false, - 'camel-case-expansion': false, - }, - } - ); - - if (coreNxPluginVersions.has(pkgName)) { - parsedCommandArgs.keepExistingVersions = true; - - if ( - options.updatePackageScripts || + let updatePackageScripts = false; + if ( + coreNxPluginVersions.has(pkgName) && + (options.updatePackageScripts || (options.updatePackageScripts === undefined && nxJson.useInferencePlugins !== false && - process.env.NX_ADD_PLUGINS !== 'false') - ) { - parsedCommandArgs.updatePackageScripts = true; - } + process.env.NX_ADD_PLUGINS !== 'false')) + ) { + updatePackageScripts = true; } const spinner = ora(`Initializing ${pkgName}...`); @@ -143,8 +129,8 @@ async function initializePlugin( await installPlugin( pkgName, workspaceRoot, - options.verbose, - parsedCommandArgs + updatePackageScripts, + options.verbose ); } catch (e) { spinner.fail(); diff --git a/packages/nx/src/command-line/init/configure-plugins.ts b/packages/nx/src/command-line/init/configure-plugins.ts index 2c5aa1e035522..9cea32562dd52 100644 --- a/packages/nx/src/command-line/init/configure-plugins.ts +++ b/packages/nx/src/command-line/init/configure-plugins.ts @@ -1,19 +1,13 @@ import * as createSpinner from 'ora'; import { bold } from 'chalk'; +import { execSync } from 'child_process'; import { getPackageManagerCommand, PackageManagerCommands, } from '../../utils/package-manager'; -import { GitRepository } from '../../utils/git-utils'; import { output } from '../../utils/output'; -import { flushChanges, FsTree } from '../../generators/tree'; -import { - Generator as NxGenerator, - GeneratorCallback, - GeneratorsJsonEntry, -} from '../../config/misc-interfaces'; -import { getGeneratorInformation } from '../generate/generator-utils'; +import { GeneratorsJsonEntry } from '../../config/misc-interfaces'; import { workspaceRoot } from '../../utils/workspace-root'; import { addDepsToPackageJson, runInstall } from './implementation/utils'; 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. * @param plugin The name of the plugin to install * @param repoRoot repo root - * @param verbose verbose - * @param options options passed to init generator + * @param pmc package manager commands + * @param updatePackageScripts whether to update package scripts + * @param verbose whether to run in verbose mode * @returns void */ export async function installPlugin( plugin: string, repoRoot: string = workspaceRoot, + updatePackageScripts: boolean = false, verbose: boolean = false, - options: { [k: string]: any } + pmc: PackageManagerCommands = getPackageManagerCommand() ): Promise { - const host = new FsTree(repoRoot, verbose, `install ${plugin}`); const capabilities = await getPluginCapabilities(repoRoot, plugin, {}); const generators = capabilities?.generators; if (!generators) { @@ -64,19 +59,16 @@ export async function installPlugin( }); return; } - const { implementationFactory } = getGeneratorInformation( - plugin, - initGenerator, - repoRoot, - {} + execSync( + `${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${ + updatePackageScripts ? '--updatePackageScripts' : '' + } ${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( plugins: string[], updatePackageScripts: boolean, + pmc: PackageManagerCommands, repoRoot: string = workspaceRoot, verbose: boolean = false ): Promise<{ @@ -108,13 +101,7 @@ export async function installPlugins( for (const plugin of plugins) { try { spinner.start('Installing plugin ' + plugin); - await installPlugin(plugin, repoRoot, verbose, { - keepExistingVersions: true, - updatePackageScripts, - addPlugin: true, - skipFormat: false, - skipPackageJson: false, - }); + await installPlugin(plugin, repoRoot, updatePackageScripts, verbose, pmc); succeededPlugins.push(plugin); spinner.succeed('Installed plugin ' + plugin); } catch (e) { @@ -154,6 +141,7 @@ export async function configurePlugins( let { succeededPlugins, failedPlugins } = await installPlugins( plugins, updatePackageScripts, + pmc, repoRoot, verbose );