Skip to content

Commit d99164a

Browse files
xiongemijaysoo
authored andcommitted
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 #
1 parent 6725518 commit d99164a

File tree

3 files changed

+31
-57
lines changed

3 files changed

+31
-57
lines changed

e2e/webpack/src/__snapshots__/webpack.legacy.test.ts.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ exports[`Webpack Plugin (legacy) ConvertConfigToWebpackPlugin, should convert wi
8585
}
8686
}
8787
},
88+
"lint": {
89+
"executor": "@nx/eslint:lint"
90+
},
8891
"test": {
8992
"executor": "@nx/vite:test",
9093
"outputs": ["{options.reportsDirectory}"],
9194
"options": {
9295
"reportsDirectory": "../coverage/app3224373"
9396
}
9497
},
95-
"lint": {
96-
"executor": "@nx/eslint:lint"
97-
},
9898
"serve-static": {
9999
"executor": "@nx/web:file-server",
100100
"dependsOn": ["build"],

packages/nx/src/command-line/add/add.ts

+9-23
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,15 @@ async function initializePlugin(
111111
options: AddOptions,
112112
nxJson: NxJsonConfiguration
113113
): Promise<void> {
114-
const parsedCommandArgs: { [key: string]: any } = yargsParser(
115-
options.__overrides_unparsed__,
116-
{
117-
configuration: {
118-
'parse-numbers': false,
119-
'parse-positional-numbers': false,
120-
'dot-notation': false,
121-
'camel-case-expansion': false,
122-
},
123-
}
124-
);
125-
126-
if (coreNxPluginVersions.has(pkgName)) {
127-
parsedCommandArgs.keepExistingVersions = true;
128-
129-
if (
130-
options.updatePackageScripts ||
114+
let updatePackageScripts = false;
115+
if (
116+
coreNxPluginVersions.has(pkgName) &&
117+
(options.updatePackageScripts ||
131118
(options.updatePackageScripts === undefined &&
132119
nxJson.useInferencePlugins !== false &&
133-
process.env.NX_ADD_PLUGINS !== 'false')
134-
) {
135-
parsedCommandArgs.updatePackageScripts = true;
136-
}
120+
process.env.NX_ADD_PLUGINS !== 'false'))
121+
) {
122+
updatePackageScripts = true;
137123
}
138124

139125
const spinner = ora(`Initializing ${pkgName}...`);
@@ -143,8 +129,8 @@ async function initializePlugin(
143129
await installPlugin(
144130
pkgName,
145131
workspaceRoot,
146-
options.verbose,
147-
parsedCommandArgs
132+
updatePackageScripts,
133+
options.verbose
148134
);
149135
} catch (e) {
150136
spinner.fail();

packages/nx/src/command-line/init/configure-plugins.ts

+19-31
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import * as createSpinner from 'ora';
22
import { bold } from 'chalk';
3+
import { execSync } from 'child_process';
34

45
import {
56
getPackageManagerCommand,
67
PackageManagerCommands,
78
} from '../../utils/package-manager';
8-
import { GitRepository } from '../../utils/git-utils';
99
import { output } from '../../utils/output';
10-
import { flushChanges, FsTree } from '../../generators/tree';
11-
import {
12-
Generator as NxGenerator,
13-
GeneratorCallback,
14-
GeneratorsJsonEntry,
15-
} from '../../config/misc-interfaces';
16-
import { getGeneratorInformation } from '../generate/generator-utils';
10+
import { GeneratorsJsonEntry } from '../../config/misc-interfaces';
1711
import { workspaceRoot } from '../../utils/workspace-root';
1812
import { addDepsToPackageJson, runInstall } from './implementation/utils';
1913
import { getPluginCapabilities } from '../../utils/plugins';
@@ -40,17 +34,18 @@ export function runPackageManagerInstallPlugins(
4034
* Installs a plugin by running its init generator. It will change the file system tree passed in.
4135
* @param plugin The name of the plugin to install
4236
* @param repoRoot repo root
43-
* @param verbose verbose
44-
* @param options options passed to init generator
37+
* @param pmc package manager commands
38+
* @param updatePackageScripts whether to update package scripts
39+
* @param verbose whether to run in verbose mode
4540
* @returns void
4641
*/
4742
export async function installPlugin(
4843
plugin: string,
4944
repoRoot: string = workspaceRoot,
45+
updatePackageScripts: boolean = false,
5046
verbose: boolean = false,
51-
options: { [k: string]: any }
47+
pmc: PackageManagerCommands = getPackageManagerCommand()
5248
): Promise<void> {
53-
const host = new FsTree(repoRoot, verbose, `install ${plugin}`);
5449
const capabilities = await getPluginCapabilities(repoRoot, plugin, {});
5550
const generators = capabilities?.generators;
5651
if (!generators) {
@@ -64,19 +59,16 @@ export async function installPlugin(
6459
});
6560
return;
6661
}
67-
const { implementationFactory } = getGeneratorInformation(
68-
plugin,
69-
initGenerator,
70-
repoRoot,
71-
{}
62+
execSync(
63+
`${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${
64+
updatePackageScripts ? '--updatePackageScripts' : ''
65+
} ${verbose ? '--verbose' : ''}`,
66+
{
67+
stdio: [0, 1, 2],
68+
cwd: repoRoot,
69+
windowsHide: false,
70+
}
7271
);
73-
74-
const implementation: NxGenerator = implementationFactory();
75-
const task: GeneratorCallback | void = await implementation(host, options);
76-
flushChanges(repoRoot, host.listChanges());
77-
if (task) {
78-
await task();
79-
}
8072
}
8173

8274
/**
@@ -87,6 +79,7 @@ export async function installPlugin(
8779
export async function installPlugins(
8880
plugins: string[],
8981
updatePackageScripts: boolean,
82+
pmc: PackageManagerCommands,
9083
repoRoot: string = workspaceRoot,
9184
verbose: boolean = false
9285
): Promise<{
@@ -108,13 +101,7 @@ export async function installPlugins(
108101
for (const plugin of plugins) {
109102
try {
110103
spinner.start('Installing plugin ' + plugin);
111-
await installPlugin(plugin, repoRoot, verbose, {
112-
keepExistingVersions: true,
113-
updatePackageScripts,
114-
addPlugin: true,
115-
skipFormat: false,
116-
skipPackageJson: false,
117-
});
104+
await installPlugin(plugin, repoRoot, updatePackageScripts, verbose, pmc);
118105
succeededPlugins.push(plugin);
119106
spinner.succeed('Installed plugin ' + plugin);
120107
} catch (e) {
@@ -154,6 +141,7 @@ export async function configurePlugins(
154141
let { succeededPlugins, failedPlugins } = await installPlugins(
155142
plugins,
156143
updatePackageScripts,
144+
pmc,
157145
repoRoot,
158146
verbose
159147
);

0 commit comments

Comments
 (0)