Skip to content

Commit

Permalink
fix(core): do not forward --updatePackageScripts flag to init generat…
Browse files Browse the repository at this point in the history
…ors that are not from nx core plugins
  • Loading branch information
leosvelperez committed Apr 29, 2024
1 parent 9c6532c commit 0994e18
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,17 +25,22 @@ export function addHandler(options: AddOptions): Promise<void> {
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.`,
});
});
}

async function installPackage(pkgName: string, version: string): Promise<void> {
async function installPackage(
pkgName: string,
version: string,
nxJson: NxJsonConfiguration
): Promise<void> {
const spinner = ora(`Installing ${pkgName}@${version}...`);
spinner.start();

Expand All @@ -57,7 +62,6 @@ async function installPackage(pkgName: string, version: string): Promise<void> {
})
);
} else {
const nxJson = readNxJson();
nxJson.installation.plugins ??= {};
nxJson.installation.plugins[pkgName] = version;
writeJsonFile('nx.json', nxJson);
Expand All @@ -84,7 +88,8 @@ async function installPackage(pkgName: string, version: string): Promise<void> {

async function initializePlugin(
pkgName: string,
options: AddOptions
options: AddOptions,
nxJson: NxJsonConfiguration
): Promise<void> {
const capabilities = await getPluginCapabilities(workspaceRoot, pkgName, {});
const generators = capabilities?.generators;
Expand All @@ -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();
Expand Down

0 comments on commit 0994e18

Please sign in to comment.