From 0994e1842a0a65f6c69d1409c380e78a7ecd02a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Mon, 29 Apr 2024 15:44:51 +0200 Subject: [PATCH] fix(core): do not forward --updatePackageScripts flag to init generators that are not from nx core plugins --- packages/nx/src/command-line/add/add.ts | 57 ++++++++++++++----------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/packages/nx/src/command-line/add/add.ts b/packages/nx/src/command-line/add/add.ts index a5e61c28b772e..00604401054aa 100644 --- a/packages/nx/src/command-line/add/add.ts +++ b/packages/nx/src/command-line/add/add.ts @@ -3,7 +3,7 @@ import { existsSync } from 'fs'; import * as ora from 'ora'; import { isAngularPluginInstalled } from '../../adapter/angular-json'; import type { GeneratorsJsonEntry } from '../../config/misc-interfaces'; -import { readNxJson } from '../../config/nx-json'; +import { readNxJson, type NxJsonConfiguration } from '../../config/nx-json'; import { runNxAsync } from '../../utils/child-process'; import { writeJsonFile } from '../../utils/fileutils'; import { logger } from '../../utils/logger'; @@ -25,9 +25,10 @@ export function addHandler(options: AddOptions): Promise { output.addNewline(); const [pkgName, version] = parsePackageSpecifier(options.packageSpecifier); + const nxJson = readNxJson(); - await installPackage(pkgName, version); - await initializePlugin(pkgName, options); + await installPackage(pkgName, version, nxJson); + await initializePlugin(pkgName, options, nxJson); output.success({ title: `Package ${pkgName} added successfully.`, @@ -35,7 +36,11 @@ export function addHandler(options: AddOptions): Promise { }); } -async function installPackage(pkgName: string, version: string): Promise { +async function installPackage( + pkgName: string, + version: string, + nxJson: NxJsonConfiguration +): Promise { const spinner = ora(`Installing ${pkgName}@${version}...`); spinner.start(); @@ -57,7 +62,6 @@ async function installPackage(pkgName: string, version: string): Promise { }) ); } else { - const nxJson = readNxJson(); nxJson.installation.plugins ??= {}; nxJson.installation.plugins[pkgName] = version; writeJsonFile('nx.json', nxJson); @@ -84,7 +88,8 @@ async function installPackage(pkgName: string, version: string): Promise { async function initializePlugin( pkgName: string, - options: AddOptions + options: AddOptions, + nxJson: NxJsonConfiguration ): Promise { const capabilities = await getPluginCapabilities(workspaceRoot, pkgName, {}); const generators = capabilities?.generators; @@ -107,27 +112,27 @@ async function initializePlugin( spinner.start(); try { - let updatePackageScripts: boolean; - if (options.updatePackageScripts !== undefined) { - updatePackageScripts = options.updatePackageScripts; - } else { - updatePackageScripts = - readNxJson().useInferencePlugins !== false && - process.env.NX_ADD_PLUGINS !== 'false' && - coreNxPlugins.includes(pkgName); - } - await runNxAsync( - `g ${pkgName}:${initGenerator} --keepExistingVersions${ - updatePackageScripts ? ' --updatePackageScripts' : '' - }${ - options.__overrides_unparsed__.length - ? ' ' + options.__overrides_unparsed__.join(' ') - : '' - }`, - { - silent: !options.verbose, + const args = []; + if (coreNxPlugins.includes(pkgName)) { + args.push(`--keepExistingVersions`); + + if ( + options.updatePackageScripts || + (options.updatePackageScripts === undefined && + nxJson.useInferencePlugins !== false && + process.env.NX_ADD_PLUGINS !== 'false') + ) { + args.push(`--updatePackageScripts`); } - ); + } + + if (options.__overrides_unparsed__.length) { + args.push(...options.__overrides_unparsed__); + } + + await runNxAsync(`g ${pkgName}:${initGenerator} ${args.join(' ')}`, { + silent: !options.verbose, + }); } catch (e) { spinner.fail(); output.addNewline();