Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nx-plugin with v2 format #971

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions e2e/nx-plugin-e2e/tests/plugin-derive-config.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { type Tree, writeJson } from '@nx/devkit';
import path from 'node:path';
import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
import { afterEach, expect } from 'vitest';
import { generateCodePushupConfig } from '@code-pushup/nx-plugin';
import {
generateProject,
generateWorkspaceAndProject,
materializeTree,
nxShowProjectJson,
nxTargetProject,
registerPluginInWorkspace,
} from '@code-pushup/test-nx-utils';
import {
E2E_ENVIRONMENTS_DIR,
TEST_OUTPUT_DIR,
teardownTestFolder,
} from '@code-pushup/test-utils';

describe('nx-plugin-derived-config', () => {
let root: string;
let tree: Tree;
const projectName = 'pkg';
const testFileDir = path.join(
E2E_ENVIRONMENTS_DIR,
nxTargetProject(),
TEST_OUTPUT_DIR,
'plugin-create-nodes',
);

beforeEach(async () => {
tree = await generateWorkspaceAndProject();
registerPluginInWorkspace(tree, '@code-pushup/nx-plugin');
await generateProject(tree, projectName);
root = readProjectConfiguration(tree, projectName).root;
generateCodePushupConfig(tree, root);
});

afterEach(async () => {
await teardownTestFolder(testFileDir);
});

it('should derive config from project.json', async () => {
const cwd = path.join(testFileDir, 'project-config');
const projectJsonPath = path.join('libs', projectName, 'project.json');
const packageJsonPath = path.join('libs', projectName, 'package.json');
tree.delete(projectJsonPath);
tree.delete(packageJsonPath);
writeJson(tree, projectJsonPath, {
root,
name: projectName,
targets: {
'code-pushup': {
executor: `@code-pushup/nx-plugin:cli`,
options: {
'persist.filename': 'my-report',
},
},
},
});
await materializeTree(tree, cwd);

const { code, projectJson } = await nxShowProjectJson(cwd, projectName);
expect(code).toBe(0);

expect(projectJson.targets).toStrictEqual(
expect.objectContaining({
'code-pushup': {
configurations: {},
executor: `@code-pushup/nx-plugin:cli`,
options: {
'persist.filename': 'my-report',
},
parallelism: true,
},
}),
);
});

it('should derive config from package.json', async () => {
const cwd = path.join(testFileDir, 'package-config');
const projectJsonPath = path.join('libs', projectName, 'project.json');
const packageJsonPath = path.join('libs', projectName, 'package.json');
tree.delete(projectJsonPath);
tree.delete(packageJsonPath);
writeJson(tree, packageJsonPath, {
name: `@code-pushup/${projectName}`,
nx: {
root,
name: projectName,
targets: {
'code-pushup': {
executor: `@code-pushup/nx-plugin:cli`,
options: {
'persist.filename': 'my-report',
},
},
},
},
});
await materializeTree(tree, cwd);

const { code, projectJson } = await nxShowProjectJson(cwd, projectName);
expect(code).toBe(0);

expect(projectJson.targets).toStrictEqual(
expect.objectContaining({
'code-pushup': {
configurations: {},
executor: `@code-pushup/nx-plugin:cli`,
options: {
'persist.filename': 'my-report',
},
parallelism: true,
},
}),
);
});

it('should derive config from mixed', async () => {
const cwd = path.join(testFileDir, 'mixed-config');
const projectJsonPath = path.join('libs', projectName, 'project.json');
const packageJsonPath = path.join('libs', projectName, 'package.json');

writeJson(tree, projectJsonPath, {
root,
name: projectName,
targets: {
'code-pushup': {
executor: `@code-pushup/nx-plugin:cli`,
options: {
'persist.filename': 'my-report',
},
},
},
});
writeJson(tree, packageJsonPath, {
name: `@code-pushup/${projectName}`,
nx: {
root,
name: projectName,
targets: {
'code-pushup': {
executor: `@code-pushup/nx-plugin:cli`,
options: {
'persist.outputPath': 'my-dir',
},
},
},
},
});
await materializeTree(tree, cwd);

const { code, projectJson } = await nxShowProjectJson(cwd, projectName);
expect(code).toBe(0);

expect(projectJson.targets).toStrictEqual(
expect.objectContaining({
'code-pushup': {
configurations: {},
executor: `@code-pushup/nx-plugin:cli`,
options: {
'persist.filename': 'my-report',
'persist.outputPath': 'my-dir',
},
parallelism: true,
},
}),
);
});
});
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@nx/react": "19.8.13",
"@nx/vite": "19.8.13",
"@nx/workspace": "19.8.13",
"@push-based/nx-verdaccio": "0.0.0-alpha.30",
"@push-based/nx-verdaccio": "^0.0.0-alpha.31",
"@swc-node/register": "1.9.2",
"@swc/cli": "0.3.14",
"@swc/core": "1.5.7",
Expand Down
2 changes: 2 additions & 0 deletions packages/nx-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ Examples:

- `nx run <project-name>:code-pushup`
- `nx run <project-name>:code-pushup print-config --persist.filename=custom-report`

> ℹ️ This plugin supports both V1 and V2 plugin strategies. When installed in an Nx workspace using Nx 18 or later, the executors will be automatically inferred. Learn more about inferred tasks [here](https://nx.dev/concepts/inferred-tasks).
40 changes: 35 additions & 5 deletions packages/nx-plugin/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type {
CreateNodes,
CreateNodesContext,
CreateNodesResult,
import {
type CreateNodes,
type CreateNodesContext,
type CreateNodesResult,
type CreateNodesV2,
createNodesFromFiles,
} from '@nx/devkit';
import { PROJECT_JSON_FILE_NAME } from '../internal/constants.js';
import { createTargets } from './target/targets.js';
import type { CreateNodesOptions } from './types.js';
import { normalizedCreateNodesContext } from './utils.js';

// name has to be "createNodes" to get picked up by Nx <v20
/** Create the nodes for a V1 Plugin. The name `createNodes` is required by Nx in order to be picked up as a plugin. */
export const createNodes: CreateNodes = [
`**/${PROJECT_JSON_FILE_NAME}`,
async (
Expand All @@ -32,3 +34,31 @@ export const createNodes: CreateNodes = [
};
},
];

/** Create the nodes for a V2 Plugin. The name `createNodesV2` is required by Nx in order to be picked up as a plugin. */
export const createNodesV2: CreateNodesV2 = [
`**/${PROJECT_JSON_FILE_NAME}`,
async (configFiles, options, context) =>
createNodesFromFiles(
async (globMatchingFile, internalOptions) => {
const parsedCreateNodesOptions = internalOptions as CreateNodesOptions;

const normalizedContext = await normalizedCreateNodesContext(
context,
globMatchingFile,
parsedCreateNodesOptions,
);

return {
projects: {
[normalizedContext.projectRoot]: {
targets: await createTargets(normalizedContext),
},
},
};
},
configFiles,
options,
context,
),
];
Loading
Loading