diff --git a/e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts b/e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts index d4ad9f65c..fb5408812 100644 --- a/e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts +++ b/e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts @@ -189,28 +189,27 @@ describe('nx-plugin', () => { await materializeTree(tree, cwd); - const { stdout, stderr } = await executeProcess({ + const { stdout } = await executeProcess({ command: 'npx', - args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'], + args: ['nx', 'run', `${project}:code-pushup`, '--dryRun', '--verbose'], cwd, }); - const cleanStderr = removeColorCodes(stderr); - // @TODO create test environment for working plugin. This here misses package-lock.json to execute correctly - expect(cleanStderr).toContain('DryRun execution of: npx @code-pushup/cli'); - const cleanStdout = removeColorCodes(stdout); - expect(cleanStdout).toContain( - 'NX Successfully ran target code-pushup for project my-lib', - ); + // Nx command + expect(cleanStdout).toContain('nx run my-lib:code-pushup'); + // Run CLI executor + expect(cleanStdout).toContain('Command: npx @code-pushup/cli'); + expect(cleanStdout).toContain('--dryRun --verbose'); }); it('should consider plugin option bin in executor target', async () => { - const cwd = path.join(testFileDir, 'configuration-option-bin'); + const cwd = path.join(testFileDir, 'executor-option-bin'); + const binPath = `packages/cli/dist`; registerPluginInWorkspace(tree, { plugin: '@code-pushup/nx-plugin', options: { - bin: 'XYZ', + bin: binPath, }, }); const { root } = readProjectConfiguration(tree, project); @@ -223,13 +222,15 @@ describe('nx-plugin', () => { expect(projectJson.targets).toStrictEqual({ 'code-pushup': expect.objectContaining({ - executor: 'XYZ:cli', + options: { + bin: binPath, + }, }), }); }); it('should consider plugin option projectPrefix in executor target', async () => { - const cwd = path.join(testFileDir, 'configuration-option-bin'); + const cwd = path.join(testFileDir, 'executor-option-projectPrefix'); registerPluginInWorkspace(tree, { plugin: '@code-pushup/nx-plugin', options: { diff --git a/packages/nx-plugin/src/executors/cli/README.md b/packages/nx-plugin/src/executors/cli/README.md index fdb01c9f7..720432911 100644 --- a/packages/nx-plugin/src/executors/cli/README.md +++ b/packages/nx-plugin/src/executors/cli/README.md @@ -7,10 +7,10 @@ For details on the CLI command read the [CLI commands documentation](https://git ## Usage -Configure a target in your project json. +Configure a target in your `project.json`. ```jsonc -// /project.json +// {projectRoot}/project.json { "name": "my-project", "targets": { @@ -74,4 +74,4 @@ Show what will be executed without actually executing it: | **dryRun** | `boolean` | To debug the executor, dry run the command without real execution. | | **bin** | `string` | Path to Code PushUp CLI | -For all other options see the [CLI autorun documentation](../../../../cli/README.md#autorun-command). +For all other options, see the [CLI autorun documentation](../../../../cli/README.md#autorun-command). diff --git a/packages/nx-plugin/src/executors/cli/executor.ts b/packages/nx-plugin/src/executors/cli/executor.ts index 7eece1129..2e644f184 100644 --- a/packages/nx-plugin/src/executors/cli/executor.ts +++ b/packages/nx-plugin/src/executors/cli/executor.ts @@ -27,10 +27,11 @@ export default async function runAutorunExecutor( mergedOptions, normalizedContext, ); - const { dryRun, verbose, command } = mergedOptions; + const { dryRun, verbose, command, bin } = mergedOptions; const commandString = createCliCommandString({ command, args: cliArgumentObject, + bin, }); if (verbose) { logger.info(`Run CLI executor ${command ?? ''}`); @@ -41,7 +42,7 @@ export default async function runAutorunExecutor( } else { try { await executeProcess({ - ...createCliCommandObject({ command, args: cliArgumentObject }), + ...createCliCommandObject({ command, args: cliArgumentObject, bin }), ...(context.cwd ? { cwd: context.cwd } : {}), }); } catch (error) { @@ -49,7 +50,7 @@ export default async function runAutorunExecutor( return { success: false, command: commandString, - error: error as Error, + error: error instanceof Error ? error : new Error(`${error}`), }; } } diff --git a/packages/nx-plugin/src/executors/internal/config.ts b/packages/nx-plugin/src/executors/internal/config.ts index 0eb13f8a8..1ef022a44 100644 --- a/packages/nx-plugin/src/executors/internal/config.ts +++ b/packages/nx-plugin/src/executors/internal/config.ts @@ -1,5 +1,6 @@ import * as path from 'node:path'; import type { PersistConfig, UploadConfig } from '@code-pushup/models'; +import type { NormalizedExecutorContext } from './context.js'; import { parseEnv } from './env.js'; import type { BaseNormalizedExecutorContext, @@ -44,26 +45,32 @@ export function persistConfig( export function uploadConfig( options: Partial, - context: BaseNormalizedExecutorContext, + context: NormalizedExecutorContext, ): Partial { - const { projectConfig, workspaceRoot } = context; + const { workspaceRoot, projectName } = context; - const { name: projectName } = projectConfig ?? {}; const { projectPrefix, server, apiKey, organization, project, timeout } = options; const applyPrefix = workspaceRoot === '.'; const prefix = projectPrefix ? `${projectPrefix}-` : ''; + + const derivedProject = + projectName && !project + ? applyPrefix + ? `${prefix}${projectName}` + : projectName + : project; + return { - ...(projectName - ? { - project: applyPrefix ? `${prefix}${projectName}` : projectName, - } - : {}), ...parseEnv(process.env), ...Object.fromEntries( - Object.entries({ server, apiKey, organization, project, timeout }).filter( - ([_, v]) => v !== undefined, - ), + Object.entries({ + server, + apiKey, + organization, + ...(derivedProject ? { project: derivedProject } : {}), + timeout, + }).filter(([_, v]) => v !== undefined), ), }; } diff --git a/packages/nx-plugin/src/executors/internal/config.unit.test.ts b/packages/nx-plugin/src/executors/internal/config.unit.test.ts index cd8eb0aab..6cb8ed0e8 100644 --- a/packages/nx-plugin/src/executors/internal/config.unit.test.ts +++ b/packages/nx-plugin/src/executors/internal/config.unit.test.ts @@ -267,8 +267,8 @@ describe('uploadConfig', () => { expect( uploadConfig(baseUploadConfig, { workspaceRoot: 'workspace-root', + projectName, projectConfig: { - name: projectName, root: 'root', }, }), diff --git a/packages/nx-plugin/src/plugin/target/executor-target.ts b/packages/nx-plugin/src/plugin/target/executor-target.ts index e8b52eb8f..d8cfab569 100644 --- a/packages/nx-plugin/src/plugin/target/executor-target.ts +++ b/packages/nx-plugin/src/plugin/target/executor-target.ts @@ -6,15 +6,12 @@ export function createExecutorTarget(options?: { bin?: string; projectPrefix?: string; }): TargetConfiguration { - const { bin = PACKAGE_NAME, projectPrefix } = options ?? {}; - return { - executor: `${bin}:cli`, - ...(projectPrefix - ? { - options: { - projectPrefix, - }, - } - : {}), + const { bin, projectPrefix } = options ?? {}; + + const executor = `${PACKAGE_NAME}:cli`; + const executorOptions = (bin || projectPrefix) && { + ...(bin && { bin }), + ...(projectPrefix && { projectPrefix }), }; + return { executor, ...(executorOptions && { options: executorOptions }) }; } diff --git a/packages/nx-plugin/src/plugin/target/executor.target.unit.test.ts b/packages/nx-plugin/src/plugin/target/executor.target.unit.test.ts index 610b44bd7..2926d3367 100644 --- a/packages/nx-plugin/src/plugin/target/executor.target.unit.test.ts +++ b/packages/nx-plugin/src/plugin/target/executor.target.unit.test.ts @@ -1,4 +1,4 @@ -import { expect } from 'vitest'; +import { describe, expect, it } from 'vitest'; import { createExecutorTarget } from './executor-target.js'; describe('createExecutorTarget', () => { @@ -8,9 +8,14 @@ describe('createExecutorTarget', () => { }); }); - it('should use bin if provides', () => { - expect(createExecutorTarget({ bin: 'xyz' })).toStrictEqual({ - executor: 'xyz:cli', + it('should use bin if provided', () => { + expect( + createExecutorTarget({ bin: 'packages/cli/src/index.ts' }), + ).toStrictEqual({ + executor: '@code-pushup/nx-plugin:cli', + options: { + bin: 'packages/cli/src/index.ts', + }, }); });