Skip to content

Commit 068984b

Browse files
BioPhotonJohn Doe
andauthored
refactor: use utils in nx-plugin (#1157)
related to #1091 **This PR includes:** - alignments of terminal args helper - add command formatter helper to `utils` - reuse utils in `nx-plugin` and remove old code - remove bin logic from config target (left over of #1109) --------- Co-authored-by: John Doe <[email protected]>
1 parent b95e56c commit 068984b

File tree

15 files changed

+160
-275
lines changed

15 files changed

+160
-275
lines changed

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,6 @@ describe('nx-plugin', () => {
103103
});
104104
});
105105

106-
it('should consider plugin option bin in configuration target', async () => {
107-
const cwd = path.join(testFileDir, 'configuration-option-bin');
108-
registerPluginInWorkspace(tree, {
109-
plugin: '@code-pushup/nx-plugin',
110-
options: {
111-
bin: 'XYZ',
112-
},
113-
});
114-
await materializeTree(tree, cwd);
115-
116-
const { code, projectJson } = await nxShowProjectJson(cwd, project);
117-
118-
expect(code).toBe(0);
119-
120-
expect(projectJson.targets).toStrictEqual({
121-
'code-pushup--configuration': expect.objectContaining({
122-
options: {
123-
command: `nx g XYZ:configuration --project="${project}"`,
124-
},
125-
}),
126-
});
127-
});
128-
129106
it('should NOT add config targets dynamically if the project is configured', async () => {
130107
const cwd = path.join(testFileDir, 'configuration-already-configured');
131108
registerPluginInWorkspace(tree, '@code-pushup/nx-plugin');
@@ -199,8 +176,10 @@ describe('nx-plugin', () => {
199176
// Nx command
200177
expect(cleanStdout).toContain('nx run my-lib:code-pushup');
201178
// Run CLI executor
202-
expect(cleanStdout).toContain('Command: npx @code-pushup/cli');
203-
expect(cleanStdout).toContain('--dryRun --verbose');
179+
expect(cleanStdout).toContain('Command:');
180+
expect(cleanStdout).toContain('npx @code-pushup/cli');
181+
expect(cleanStdout).toContain('--verbose');
182+
expect(cleanStdout).toContain('--dryRun ');
204183
});
205184

206185
it('should consider plugin option bin in executor target', async () => {

packages/nx-plugin/src/executors/cli/executor.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { type ExecutorContext, logger } from '@nx/devkit';
22
import { executeProcess } from '../../internal/execute-process.js';
3-
import {
4-
createCliCommandObject,
5-
createCliCommandString,
6-
} from '../internal/cli.js';
73
import { normalizeContext } from '../internal/context.js';
84
import type { AutorunCommandExecutorOptions } from './schema.js';
95
import { parseAutorunExecutorOptions } from './utils.js';
@@ -14,20 +10,33 @@ export type ExecutorOutput = {
1410
error?: Error;
1511
};
1612

13+
/* eslint-disable-next-line max-lines-per-function */
1714
export default async function runAutorunExecutor(
1815
terminalAndExecutorOptions: AutorunCommandExecutorOptions,
1916
context: ExecutorContext,
2017
): Promise<ExecutorOutput> {
18+
const { objectToCliArgs, formatCommandStatus } = await import(
19+
'@code-pushup/utils'
20+
);
2121
const normalizedContext = normalizeContext(context);
2222
const cliArgumentObject = parseAutorunExecutorOptions(
2323
terminalAndExecutorOptions,
2424
normalizedContext,
2525
);
26-
const { dryRun, verbose, command, bin } = terminalAndExecutorOptions;
27-
const commandString = createCliCommandString({
28-
command,
29-
args: cliArgumentObject,
26+
const {
27+
dryRun,
28+
verbose,
29+
command: cliCommand,
3030
bin,
31+
} = terminalAndExecutorOptions;
32+
const command = bin ? `node` : 'npx';
33+
const positionals = [
34+
bin ?? '@code-pushup/cli',
35+
...(cliCommand ? [cliCommand] : []),
36+
];
37+
const args = [...positionals, ...objectToCliArgs(cliArgumentObject)];
38+
const commandString = formatCommandStatus([command, ...args].join(' '), {
39+
cwd: context.cwd,
3140
});
3241
if (verbose) {
3342
logger.info(`Run CLI executor ${command ?? ''}`);
@@ -38,7 +47,8 @@ export default async function runAutorunExecutor(
3847
} else {
3948
try {
4049
await executeProcess({
41-
...createCliCommandObject({ command, args: cliArgumentObject, bin }),
50+
command,
51+
args,
4252
...(context.cwd ? { cwd: context.cwd } : {}),
4353
});
4454
} catch (error) {

packages/nx-plugin/src/executors/cli/executor.unit.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('runAutorunExecutor', () => {
122122
expect.stringContaining(`Run CLI executor`),
123123
);
124124
expect(loggerInfoSpy).toHaveBeenCalledWith(
125-
expect.stringContaining('Command: npx @code-pushup/cli'),
125+
expect.stringContaining('Command:'),
126126
);
127127
});
128128

@@ -132,9 +132,7 @@ describe('runAutorunExecutor', () => {
132132
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
133133
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
134134
expect(loggerWarnSpy).toHaveBeenCalledWith(
135-
expect.stringContaining(
136-
'DryRun execution of: npx @code-pushup/cli --dryRun',
137-
),
135+
expect.stringContaining('DryRun execution of'),
138136
);
139137
});
140138
});

packages/nx-plugin/src/executors/internal/cli.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

packages/nx-plugin/src/executors/internal/cli.unit.test.ts

Lines changed: 0 additions & 136 deletions
This file was deleted.

packages/nx-plugin/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const plugin = {
1111
export default plugin;
1212

1313
export type { AutorunCommandExecutorOptions } from './executors/cli/schema.js';
14-
export { objectToCliArgs } from './executors/internal/cli.js';
1514
export { generateCodePushupConfig } from './generators/configuration/code-pushup-config.js';
1615
export { configurationGenerator } from './generators/configuration/generator.js';
1716
export type { ConfigurationGeneratorOptions } from './generators/configuration/schema.js';
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
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';
43
import { PACKAGE_NAME } from '../../internal/constants.js';
54

6-
export function createConfigurationTarget(options?: {
5+
export async function createConfigurationTarget(options?: {
76
projectName?: string;
8-
bin?: string;
9-
}): TargetConfiguration<RunCommandsOptions> {
10-
const { projectName, bin = PACKAGE_NAME } = options ?? {};
7+
}): Promise<TargetConfiguration<RunCommandsOptions>> {
8+
const { projectName } = options ?? {};
9+
const { objectToCliArgs } = await import('@code-pushup/utils');
1110
const args = objectToCliArgs({
1211
...(projectName ? { project: projectName } : {}),
1312
});
14-
const argsString = args.length > 0 ? args.join(' ') : '';
15-
const baseCommand = `nx g ${bin}:configuration`;
13+
const argsString = args.length > 0 ? ` ${args.join(' ')}` : '';
1614
return {
17-
command: argsString ? `${baseCommand} ${argsString}` : baseCommand,
15+
command: `nx g ${PACKAGE_NAME}:configuration${argsString}`,
1816
};
1917
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { PACKAGE_NAME } from '../../internal/constants.js';
33
import { createConfigurationTarget } from './configuration-target.js';
44

55
describe('createConfigurationTarget', () => {
6-
it('should return code-pushup--configuration target for given project', () => {
7-
expect(
6+
it('should return code-pushup--configuration target for given project', async () => {
7+
await expect(
88
createConfigurationTarget({ projectName: 'my-project' }),
9-
).toStrictEqual({
9+
).resolves.toStrictEqual({
1010
command: `nx g ${PACKAGE_NAME}:configuration --project="my-project"`,
1111
});
1212
});
1313

14-
it('should return code-pushup--configuration target without project name', () => {
15-
expect(createConfigurationTarget()).toStrictEqual({
14+
it('should return code-pushup--configuration target without project name', async () => {
15+
await expect(createConfigurationTarget()).resolves.toStrictEqual({
1616
command: `nx g ${PACKAGE_NAME}:configuration`,
1717
});
1818
});

0 commit comments

Comments
 (0)