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 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions packages/nx-plugin/eslint.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = tseslint.config(
{
files: ['**/*.ts'],
rules: {
'n/file-extension-in-import': 'off',
// Nx plugins don't yet support ESM: https://github.com/nrwl/nx/issues/15682
'unicorn/prefer-module': 'off',
// used instead of verbatimModuleSyntax tsconfig flag (requires ESM)
Expand Down
37 changes: 33 additions & 4 deletions packages/nx-plugin/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import type {
CreateNodes,
CreateNodesContext,
CreateNodesResult,
CreateNodesV2,
} 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';
import { createNodesFromFiles } from '@nx/devkit';
import { PROJECT_JSON_FILE_NAME } from '../internal/constants';
import { createTargets } from './target/targets';
import type { CreateNodesOptions } from './types';
import { normalizedCreateNodesContext } from './utils';

// name has to be "createNodes" to get picked up by Nx

export const createNodes: CreateNodes = [
`**/${PROJECT_JSON_FILE_NAME}`,
async (
Expand All @@ -32,3 +35,29 @@ export const createNodes: CreateNodes = [
};
},
];

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,
)
];
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { TargetConfiguration } from '@nx/devkit';
import type { RunCommandsOptions } from 'nx/src/executors/run-commands/run-commands.impl';
import { objectToCliArgs } from '../../executors/internal/cli.js';
import { PACKAGE_NAME } from '../../internal/constants.js';
import { CP_TARGET_NAME } from '../constants.js';
import { objectToCliArgs } from '../../executors/internal/cli';
import { PACKAGE_NAME } from '../../internal/constants';
import { CP_TARGET_NAME } from '../constants';

export function createConfigurationTarget(options?: {
targetName?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/nx-plugin/src/plugin/target/executor-target.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TargetConfiguration } from '@nx/devkit';
import { PACKAGE_NAME } from '../../internal/constants.js';
import type { ProjectPrefixOptions } from '../types.js';
import { PACKAGE_NAME } from '../../internal/constants';
import type { ProjectPrefixOptions } from '../types';

export function createExecutorTarget(options?: {
bin?: string;
Expand Down
10 changes: 5 additions & 5 deletions packages/nx-plugin/src/plugin/target/targets.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { readdir } from 'node:fs/promises';
import { CP_TARGET_NAME } from '../constants.js';
import type { NormalizedCreateNodesContext } from '../types.js';
import { createConfigurationTarget } from './configuration-target.js';
import { CODE_PUSHUP_CONFIG_REGEX } from './constants.js';
import { createExecutorTarget } from './executor-target.js';
import { CP_TARGET_NAME } from '../constants';
import type { NormalizedCreateNodesContext } from '../types';
import { createConfigurationTarget } from './configuration-target';
import { CODE_PUSHUP_CONFIG_REGEX } from './constants';
import { createExecutorTarget } from './executor-target';

export async function createTargets(
normalizedContext: NormalizedCreateNodesContext,
Expand Down
19 changes: 16 additions & 3 deletions packages/nx-plugin/src/plugin/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { CreateNodesContext, ProjectConfiguration } from '@nx/devkit';
import type {
CreateNodesContext,
CreateNodesContextV2,
ProjectConfiguration,
} from '@nx/devkit';
import type { WithRequired } from '@code-pushup/utils';
import type { DynamicTargetOptions } from '../internal/types.js';
import type { DynamicTargetOptions } from '../internal/types';

export type ProjectPrefixOptions = {
projectPrefix?: string;
Expand All @@ -13,7 +17,16 @@ export type ProjectConfigurationWithName = WithRequired<
'name'
>;

export type NormalizedCreateNodesContext = CreateNodesContext & {
export type NormalizedCreateNodesContext = (
| CreateNodesContext
| CreateNodesContextV2
) & {
projectJson: ProjectConfigurationWithName;
projectRoot: string;
createOptions: CreateNodesOptions;
};

export type NormalizedCreateNodesContextV2 = CreateNodesContextV2 & {
projectJson: ProjectConfigurationWithName;
projectRoot: string;
createOptions: CreateNodesOptions;
Expand Down
6 changes: 3 additions & 3 deletions packages/nx-plugin/src/plugin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { CreateNodesContext } from '@nx/devkit';
import type { CreateNodesContext, CreateNodesContextV2 } from '@nx/devkit';
import { readFile } from 'node:fs/promises';
import * as path from 'node:path';
import { CP_TARGET_NAME } from './constants.js';
import { CP_TARGET_NAME } from './constants';
import type {
CreateNodesOptions,
NormalizedCreateNodesContext,
ProjectConfigurationWithName,
} from './types.js';

export async function normalizedCreateNodesContext(
context: CreateNodesContext,
context: CreateNodesContext | CreateNodesContextV2,
projectConfigurationFile: string,
createOptions: CreateNodesOptions = {},
): Promise<NormalizedCreateNodesContext> {
Expand Down
Loading