Skip to content

Commit

Permalink
fix(core): change to use init generator during import (#30029)
Browse files Browse the repository at this point in the history
<!-- 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 #
  • Loading branch information
xiongemi authored and jaysoo committed Feb 14, 2025
1 parent 6725518 commit d99164a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 57 deletions.
6 changes: 3 additions & 3 deletions e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ exports[`Webpack Plugin (legacy) ConvertConfigToWebpackPlugin, should convert wi
}
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"reportsDirectory": "../coverage/app3224373"
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
"serve-static": {
"executor": "@nx/web:file-server",
"dependsOn": ["build"],
Expand Down
32 changes: 9 additions & 23 deletions packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,15 @@ async function initializePlugin(
options: AddOptions,
nxJson: NxJsonConfiguration
): Promise<void> {
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}...`);
Expand All @@ -143,8 +129,8 @@ async function initializePlugin(
await installPlugin(
pkgName,
workspaceRoot,
options.verbose,
parsedCommandArgs
updatePackageScripts,
options.verbose
);
} catch (e) {
spinner.fail();
Expand Down
50 changes: 19 additions & 31 deletions packages/nx/src/command-line/init/configure-plugins.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<void> {
const host = new FsTree(repoRoot, verbose, `install ${plugin}`);
const capabilities = await getPluginCapabilities(repoRoot, plugin, {});
const generators = capabilities?.generators;
if (!generators) {
Expand All @@ -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();
}
}

/**
Expand All @@ -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<{
Expand All @@ -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) {
Expand Down Expand Up @@ -154,6 +141,7 @@ export async function configurePlugins(
let { succeededPlugins, failedPlugins } = await installPlugins(
plugins,
updatePackageScripts,
pmc,
repoRoot,
verbose
);
Expand Down

0 comments on commit d99164a

Please sign in to comment.