Skip to content

Commit

Permalink
fix(gradle): expose create nodes v2 (#26282)
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` -->

## Current Behavior
<!-- This is the behavior we have today -->

`createNodesV2` was written but not exported.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

`createNodesV2` is exported.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz authored May 31, 2024
1 parent 2cb7ecb commit fde4932
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 160 deletions.
2 changes: 1 addition & 1 deletion packages/cypress/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
updateNxJson,
} from '@nx/devkit';
import {
addPlugin as _addPlugin,
addPluginV1 as _addPlugin,
generateCombinations,
} from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
Expand Down
4 changes: 2 additions & 2 deletions packages/detox/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Tree,
} from '@nx/devkit';
import {
addPlugin,
addPluginV1,
generateCombinations,
} from '@nx/devkit/src/utils/add-plugin';
import { createNodes, DetoxPluginOptions } from '../../plugins/plugin';
Expand All @@ -36,7 +36,7 @@ export async function detoxInitGeneratorInternal(host: Tree, schema: Schema) {
}

if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
host,
await createProjectGraphAsync(),
'@nx/detox/plugin',
Expand Down
132 changes: 82 additions & 50 deletions packages/devkit/src/utils/add-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/cr
import type { Tree } from 'nx/src/generators/tree';
import { readJson, writeJson } from 'nx/src/generators/utils/json';
import type { PackageJson } from 'nx/src/utils/package-json';
import { CreateNodes } from 'nx/src/project-graph/plugins';
import { CreateNodesV2 } from 'nx/src/project-graph/plugins';
import { ProjectGraph } from 'nx/src/devkit-exports';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';

import { addPlugin, generateCombinations } from './add-plugin';

describe('addPlugin', () => {
let tree: Tree;
let createNodes: CreateNodes<{ targetName: string }>;
let createNodes: CreateNodesV2<{ targetName: string }>;
let graph: ProjectGraph;
let fs: TempFs;

Expand Down Expand Up @@ -55,22 +55,34 @@ describe('addPlugin', () => {
};
createNodes = [
'**/next.config.{js,cjs,mjs}',
(_, { targetName }) => ({
projects: {
app1: {
name: 'app1',
targets: {
[targetName]: { command: 'next build' },
(_, { targetName }) => [
[
'app1/next.config.js',
{
projects: {
app1: {
name: 'app1',
targets: {
[targetName]: { command: 'next build' },
},
},
},
},
app2: {
name: 'app2',
targets: {
[targetName]: { command: 'next build' },
],
[
'app2/next.config.js',
{
projects: {
app2: {
name: 'app2',
targets: {
[targetName]: { command: 'next build' },
},
},
},
},
},
}),
],
],
];

await fs.createFiles({
Expand Down Expand Up @@ -217,19 +229,24 @@ describe('addPlugin', () => {

createNodes = [
'**/cypress.config.{js,ts,mjs,mts,cjs,cts}',
() => ({
projects: {
app1: {
name: 'app1',
targets: {
e2e: {
command:
'cypress run --config-file cypress.config.ts --e2e',
() => [
[
'app1/cypress.config.ts',
{
projects: {
app1: {
name: 'app1',
targets: {
e2e: {
command:
'cypress run --config-file cypress.config.ts --e2e',
},
},
},
},
},
},
}),
],
],
];

await addPlugin(
Expand Down Expand Up @@ -284,18 +301,23 @@ describe('addPlugin', () => {

createNodes = [
'**/next.config.{js,cjs,mjs}',
() => ({
projects: {
app1: {
name: 'app1',
targets: {
build: { command: 'next build' },
dev: { command: 'next dev' },
start: { command: 'next start' },
() => [
[
'app1/next.config.js',
{
projects: {
app1: {
name: 'app1',
targets: {
build: { command: 'next build' },
dev: { command: 'next dev' },
start: { command: 'next start' },
},
},
},
},
},
}),
],
],
];

await addPlugin(
Expand Down Expand Up @@ -328,16 +350,21 @@ describe('addPlugin', () => {

createNodes = [
'**/tsconfig.json',
() => ({
projects: {
app1: {
name: 'app1',
targets: {
build: { command: 'tsc' },
() => [
[
'app1/tsconfig.json',
{
projects: {
app1: {
name: 'app1',
targets: {
build: { command: 'tsc' },
},
},
},
},
},
}),
],
],
];

await addPlugin(
Expand Down Expand Up @@ -370,16 +397,21 @@ describe('addPlugin', () => {

createNodes = [
'**/tsconfig.json',
() => ({
projects: {
app1: {
name: 'app1',
targets: {
build: { command: 'tsc' },
() => [
[
'app1/tsconfig.json',
{
projects: {
app1: {
name: 'app1',
targets: {
build: { command: 'tsc' },
},
},
},
},
},
}),
],
],
];

await addPlugin(
Expand Down
79 changes: 67 additions & 12 deletions packages/devkit/src/utils/add-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as yargs from 'yargs-parser';

import {
CreateNodes,
CreateNodesV2,
ProjectConfiguration,
ProjectGraph,
readJson,
Expand All @@ -24,6 +25,41 @@ import {
*/
export async function addPlugin<PluginOptions>(
tree: Tree,
graph: ProjectGraph,
pluginName: string,
createNodesTuple: CreateNodesV2<PluginOptions>,
options: Partial<
Record<keyof PluginOptions, PluginOptions[keyof PluginOptions][]>
>,
shouldUpdatePackageJsonScripts: boolean
): Promise<void> {
return _addPluginInternal(
tree,
graph,
pluginName,
(pluginOptions) =>
new LoadedNxPlugin(
{
name: pluginName,
createNodesV2: createNodesTuple,
},
{
plugin: pluginName,
options: pluginOptions,
}
),
options,
shouldUpdatePackageJsonScripts
);
}

/**
* @deprecated Use `addPlugin` instead
* Iterates through various forms of plugin options to find the one which does not conflict with the current graph
*/
export async function addPluginV1<PluginOptions>(
tree: Tree,
graph: ProjectGraph,
pluginName: string,
Expand All @@ -33,6 +69,36 @@ export async function addPlugin<PluginOptions>(
>,
shouldUpdatePackageJsonScripts: boolean
): Promise<void> {
return _addPluginInternal(
tree,
graph,
pluginName,
(pluginOptions) =>
new LoadedNxPlugin(
{
name: pluginName,
createNodes: createNodesTuple,
},
{
plugin: pluginName,
options: pluginOptions,
}
),
options,
shouldUpdatePackageJsonScripts
);
}

async function _addPluginInternal<PluginOptions>(
tree: Tree,
graph: ProjectGraph,
pluginName: string,
pluginFactory: (pluginOptions: PluginOptions) => LoadedNxPlugin,
options: Partial<
Record<keyof PluginOptions, PluginOptions[keyof PluginOptions][]>
>,
shouldUpdatePackageJsonScripts: boolean
) {
const graphNodes = Object.values(graph.nodes);
const nxJson = readNxJson(tree);

Expand All @@ -56,18 +122,7 @@ export async function addPlugin<PluginOptions>(
global.NX_GRAPH_CREATION = true;
try {
projConfigs = await retrieveProjectConfigurations(
[
new LoadedNxPlugin(
{
name: pluginName,
createNodes: createNodesTuple,
},
{
plugin: pluginName,
options: pluginOptions,
}
),
],
[pluginFactory(pluginOptions)],
tree.root,
nxJson
);
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Tree,
updateNxJson,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { eslintVersion, nxVersion } from '../../utils/versions';
import { findEslintFile } from '../utils/eslint-file';
import { createNodes } from '../../plugins/plugin';
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function initEsLint(
];

if (rootEslintFile && options.addPlugin && !hasPlugin) {
await addPlugin(
await addPluginV1(
tree,
graph,
'@nx/eslint/plugin',
Expand All @@ -94,7 +94,7 @@ export async function initEsLint(
updateProductionFileset(tree);

if (options.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
graph,
'@nx/eslint/plugin',
Expand Down
4 changes: 2 additions & 2 deletions packages/expo/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
runTasksInSerial,
Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../../plugins/plugin';
import {
expoCliVersion,
Expand Down Expand Up @@ -36,7 +36,7 @@ export async function expoInitGeneratorInternal(host: Tree, schema: Schema) {
addGitIgnoreEntry(host);

if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
host,
await createProjectGraphAsync(),
'@nx/expo/plugin',
Expand Down
2 changes: 1 addition & 1 deletion packages/gradle/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { createDependencies } from './src/plugin/dependencies';
export { createNodes } from './src/plugin/nodes';
export { createNodes, createNodesV2 } from './src/plugin/nodes';
Loading

0 comments on commit fde4932

Please sign in to comment.