Skip to content

Commit f987e8a

Browse files
committed
feat: mvp
1 parent c677fa2 commit f987e8a

File tree

8 files changed

+69
-23
lines changed

8 files changed

+69
-23
lines changed

e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
E2E_ENVIRONMENTS_DIR,
1515
TEST_OUTPUT_DIR,
1616
removeColorCodes,
17-
teardownTestFolder,
1817
} from '@code-pushup/test-utils';
1918
import { executeProcess, readTextFile } from '@code-pushup/utils';
2019
import { INLINE_PLUGIN } from './inline-plugin.js';
@@ -35,7 +34,7 @@ describe('nx-plugin', () => {
3534
});
3635

3736
afterEach(async () => {
38-
await teardownTestFolder(testFileDir);
37+
// await teardownTestFolder(testFileDir);
3938
});
4039

4140
it('should add configuration target dynamically', async () => {

nx.json

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
"filterByTags": ["publishable"]
9595
}
9696
}
97+
},
98+
{
99+
"plugin": "./packages/nx-plugin/src/plugin/plugin.ts"
97100
}
98101
]
99102
}

packages/nx-plugin/src/plugin/plugin.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import type {
1+
import {
22
CreateNodes,
33
CreateNodesContext,
44
CreateNodesResult,
5+
CreateNodesV2,
6+
createNodesFromFiles,
57
} from '@nx/devkit';
6-
import { PROJECT_JSON_FILE_NAME } from '../internal/constants.js';
7-
import { createTargets } from './target/targets.js';
8-
import type { CreateNodesOptions } from './types.js';
9-
import { normalizedCreateNodesContext } from './utils.js';
8+
import { PROJECT_JSON_FILE_NAME } from '../internal/constants';
9+
import { createTargets } from './target/targets';
10+
import type { CreateNodesOptions } from './types';
11+
import { normalizedCreateNodesContext } from './utils';
1012

1113
// name has to be "createNodes" to get picked up by Nx
14+
1215
export const createNodes: CreateNodes = [
1316
`**/${PROJECT_JSON_FILE_NAME}`,
1417
async (
@@ -32,3 +35,31 @@ export const createNodes: CreateNodes = [
3235
};
3336
},
3437
];
38+
39+
export const createNodesV2: CreateNodesV2<any> = [
40+
`**/${PROJECT_JSON_FILE_NAME}`,
41+
42+
async (configFiles, options, context) => {
43+
return await createNodesFromFiles(
44+
async (globMatchingFile, internalOptions) => {
45+
const parsedCreateNodesOptions = internalOptions as CreateNodesOptions;
46+
47+
const normalizedContext = await normalizedCreateNodesContext(
48+
context,
49+
globMatchingFile,
50+
parsedCreateNodesOptions,
51+
);
52+
return {
53+
projects: {
54+
[normalizedContext.projectRoot]: {
55+
targets: await createTargets(normalizedContext),
56+
},
57+
},
58+
};
59+
},
60+
configFiles,
61+
options,
62+
context,
63+
);
64+
},
65+
];

packages/nx-plugin/src/plugin/target/configuration-target.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { TargetConfiguration } from '@nx/devkit';
22
import type { RunCommandsOptions } from 'nx/src/executors/run-commands/run-commands.impl';
3-
import { objectToCliArgs } from '../../executors/internal/cli.js';
4-
import { PACKAGE_NAME } from '../../internal/constants.js';
5-
import { CP_TARGET_NAME } from '../constants.js';
3+
import { objectToCliArgs } from '../../executors/internal/cli';
4+
import { PACKAGE_NAME } from '../../internal/constants';
5+
import { CP_TARGET_NAME } from '../constants';
66

77
export function createConfigurationTarget(options?: {
88
targetName?: string;

packages/nx-plugin/src/plugin/target/executor-target.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TargetConfiguration } from '@nx/devkit';
2-
import { PACKAGE_NAME } from '../../internal/constants.js';
3-
import type { ProjectPrefixOptions } from '../types.js';
2+
import { PACKAGE_NAME } from '../../internal/constants';
3+
import type { ProjectPrefixOptions } from '../types';
44

55
export function createExecutorTarget(options?: {
66
bin?: string;

packages/nx-plugin/src/plugin/target/targets.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { readdir } from 'node:fs/promises';
2-
import { CP_TARGET_NAME } from '../constants.js';
3-
import type { NormalizedCreateNodesContext } from '../types.js';
4-
import { createConfigurationTarget } from './configuration-target.js';
5-
import { CODE_PUSHUP_CONFIG_REGEX } from './constants.js';
6-
import { createExecutorTarget } from './executor-target.js';
2+
import { CP_TARGET_NAME } from '../constants';
3+
import type { NormalizedCreateNodesContext } from '../types';
4+
import { createConfigurationTarget } from './configuration-target';
5+
import { CODE_PUSHUP_CONFIG_REGEX } from './constants';
6+
import { createExecutorTarget } from './executor-target';
77

88
export async function createTargets(
99
normalizedContext: NormalizedCreateNodesContext,

packages/nx-plugin/src/plugin/types.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import type { CreateNodesContext, ProjectConfiguration } from '@nx/devkit';
1+
import type {
2+
CreateNodesContext,
3+
CreateNodesContextV2,
4+
ProjectConfiguration,
5+
} from '@nx/devkit';
26
import type { WithRequired } from '@code-pushup/utils';
3-
import type { DynamicTargetOptions } from '../internal/types.js';
7+
import type { DynamicTargetOptions } from '../internal/types';
48

59
export type ProjectPrefixOptions = {
610
projectPrefix?: string;
@@ -13,7 +17,16 @@ export type ProjectConfigurationWithName = WithRequired<
1317
'name'
1418
>;
1519

16-
export type NormalizedCreateNodesContext = CreateNodesContext & {
20+
export type NormalizedCreateNodesContext = (
21+
| CreateNodesContext
22+
| CreateNodesContextV2
23+
) & {
24+
projectJson: ProjectConfigurationWithName;
25+
projectRoot: string;
26+
createOptions: CreateNodesOptions;
27+
};
28+
29+
export type NormalizedCreateNodesContextV2 = CreateNodesContextV2 & {
1730
projectJson: ProjectConfigurationWithName;
1831
projectRoot: string;
1932
createOptions: CreateNodesOptions;

packages/nx-plugin/src/plugin/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type { CreateNodesContext } from '@nx/devkit';
1+
import type { CreateNodesContext, CreateNodesContextV2 } from '@nx/devkit';
22
import { readFile } from 'node:fs/promises';
33
import * as path from 'node:path';
4-
import { CP_TARGET_NAME } from './constants.js';
4+
import { CP_TARGET_NAME } from './constants';
55
import type {
66
CreateNodesOptions,
77
NormalizedCreateNodesContext,
88
ProjectConfigurationWithName,
99
} from './types.js';
1010

1111
export async function normalizedCreateNodesContext(
12-
context: CreateNodesContext,
12+
context: CreateNodesContext | CreateNodesContextV2,
1313
projectConfigurationFile: string,
1414
createOptions: CreateNodesOptions = {},
1515
): Promise<NormalizedCreateNodesContext> {

0 commit comments

Comments
 (0)