Skip to content

Commit 6c48309

Browse files
author
John Doe
committed
refactor: use dynamic imports, delete duplicated code
1 parent fba5f15 commit 6c48309

4 files changed

Lines changed: 87 additions & 76 deletions

File tree

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { afterEach, expect, vi } from 'vitest';
22
import { executorContext } from '@code-pushup/test-nx-utils';
3+
import { removeColorCodes } from '@code-pushup/test-utils';
34
import * as executeProcessModule from '../../internal/execute-process.js';
45
import runAutorunExecutor from './executor.js';
56
import * as utils from './utils.js';
@@ -26,27 +27,25 @@ describe('runAutorunExecutor', () => {
2627
executeProcessSpy.mockReset();
2728
});
2829

29-
it('should normalize context, parse CLI options and execute command', async () => {
30-
const output = await runAutorunExecutor(
30+
it('should parse CLI options and execute command', async () => {
31+
const { success, command } = await runAutorunExecutor(
3132
{ verbose: true },
3233
executorContext('utils'),
3334
);
34-
expect(output.success).toBe(true);
35-
36-
expect(parseAutorunExecutorOptionsSpy).toHaveBeenCalledTimes(1);
37-
38-
//is context normalized
39-
expect(parseAutorunExecutorOptionsSpy).toHaveBeenCalledWith(
40-
{ verbose: true },
35+
expect(success).toBe(true);
36+
const cleanCommand = removeColorCodes(command || '');
37+
expect(cleanCommand).toMatch('npx @code-pushup/cli');
38+
expect(cleanCommand).toMatch('CP_VERBOSE="true"');
39+
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
40+
expect(executeProcessSpy).toHaveBeenCalledWith(
4141
expect.objectContaining({
42-
projectConfig: expect.objectContaining({ name: 'utils' }),
42+
command: 'npx',
43+
args: expect.arrayContaining(['@code-pushup/cli']),
44+
cwd: expect.any(String),
45+
env: expect.objectContaining({
46+
CP_VERBOSE: 'true',
47+
}),
4348
}),
4449
);
45-
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
46-
expect(executeProcessSpy).toHaveBeenCalledWith({
47-
command: 'npx',
48-
args: expect.arrayContaining(['@code-pushup/cli']),
49-
cwd: process.cwd(),
50-
});
5150
});
5251
});
Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,70 @@
1-
import { type ExecutorContext, logger } from '@nx/devkit';
1+
import type { ExecutorContext } from '@nx/devkit';
22
import { executeProcess } from '../../internal/execute-process.js';
3-
import {
4-
createCliCommandObject,
5-
createCliCommandString,
6-
} from '../internal/cli.js';
7-
import { normalizeContext } from '../internal/context.js';
83
import type { AutorunCommandExecutorOptions } from './schema.js';
9-
import { mergeExecutorOptions, parseAutorunExecutorOptions } from './utils.js';
104

115
export type ExecutorOutput = {
126
success: boolean;
137
command?: string;
148
error?: Error;
159
};
1610

11+
/* eslint-disable max-lines-per-function */
1712
export default async function runAutorunExecutor(
1813
terminalAndExecutorOptions: AutorunCommandExecutorOptions,
19-
context: ExecutorContext,
14+
{ cwd }: ExecutorContext,
2015
): Promise<ExecutorOutput> {
21-
const normalizedContext = normalizeContext(context);
22-
const mergedOptions = mergeExecutorOptions(
23-
context.target?.options,
24-
terminalAndExecutorOptions,
25-
);
26-
const cliArgumentObject = parseAutorunExecutorOptions(
27-
mergedOptions,
28-
normalizedContext,
29-
);
30-
const { dryRun, verbose, command, bin } = mergedOptions;
31-
const commandString = createCliCommandString({
32-
command,
33-
args: cliArgumentObject,
16+
const { logger, stringifyError, objectToCliArgs, formatCommand } =
17+
await import('@code-pushup/utils');
18+
const {
19+
dryRun,
20+
verbose,
21+
command: cliCommand,
22+
env: targetEnv,
3423
bin,
24+
...argsObj
25+
} = terminalAndExecutorOptions;
26+
const command = bin ? `node` : 'npx';
27+
const positionals = [
28+
bin ?? '@code-pushup/cli',
29+
...(cliCommand ? [cliCommand] : []),
30+
];
31+
const args = objectToCliArgs(argsObj);
32+
const executorEnvVariables = {
33+
...targetEnv,
34+
...(verbose && { CP_VERBOSE: 'true' }),
35+
};
36+
const binString = `${command} ${positionals.join(' ')} ${args.join(' ')}`;
37+
const formattedBinString = formatCommand(binString, {
38+
env: executorEnvVariables,
39+
cwd,
3540
});
36-
if (verbose) {
37-
logger.info(`Run CLI executor ${command ?? ''}`);
38-
logger.info(`Command: ${commandString}`);
39-
}
41+
4042
if (dryRun) {
41-
logger.warn(`DryRun execution of: ${commandString}`);
43+
logger.warn(`DryRun execution of: \n ${formattedBinString}`);
4244
} else {
4345
try {
4446
await executeProcess({
45-
...createCliCommandObject({ command, args: cliArgumentObject, bin }),
46-
...(context.cwd ? { cwd: context.cwd } : {}),
47+
command,
48+
args: [...positionals, ...args],
49+
env: {
50+
...process.env,
51+
...executorEnvVariables,
52+
},
53+
...(cwd ? { cwd } : {}),
4754
});
4855
} catch (error) {
49-
logger.error(error);
56+
logger.error(stringifyError(error));
5057
return {
5158
success: false,
52-
command: commandString,
59+
command: formattedBinString,
5360
error: error instanceof Error ? error : new Error(`${error}`),
5461
};
5562
}
5663
}
64+
5765
return {
5866
success: true,
59-
command: commandString,
67+
command: formattedBinString,
6068
};
6169
}
70+
/* eslint-enable max-lines-per-function */

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ describe('runAutorunExecutor', () => {
4444

4545
it('should call executeProcess with return result', async () => {
4646
const output = await runAutorunExecutor({}, executorContext('utils'));
47-
expect(output.success).toBe(true);
48-
expect(output.command).toMatch('npx @code-pushup/cli');
49-
expect(executeProcessSpy).toHaveBeenCalledWith({
50-
command: 'npx',
51-
args: expect.arrayContaining(['@code-pushup/cli']),
52-
cwd: MEMFS_VOLUME,
47+
expect(output).toStrictEqual({
48+
success: true,
49+
command: expect.stringContaining('npx @code-pushup/cli'),
5350
});
51+
expect(executeProcessSpy).toHaveBeenCalledWith(
52+
expect.objectContaining({
53+
command: 'npx',
54+
args: expect.arrayContaining(['@code-pushup/cli']),
55+
cwd: MEMFS_VOLUME,
56+
}),
57+
);
5458
});
5559

5660
it('should normalize context', async () => {
@@ -62,12 +66,15 @@ describe('runAutorunExecutor', () => {
6266
},
6367
);
6468
expect(output.success).toBe(true);
65-
expect(output.command).toMatch('utils');
66-
expect(executeProcessSpy).toHaveBeenCalledWith({
67-
command: 'npx',
68-
args: expect.arrayContaining(['@code-pushup/cli']),
69-
cwd: 'cwd-form-context',
70-
});
69+
expect(output.command).toMatch('npx @code-pushup/cli');
70+
expect(output.command).toContain('cwd-form-context');
71+
expect(executeProcessSpy).toHaveBeenCalledWith(
72+
expect.objectContaining({
73+
command: 'npx',
74+
args: expect.arrayContaining(['@code-pushup/cli']),
75+
cwd: 'cwd-form-context',
76+
}),
77+
);
7178
});
7279

7380
it('should process executorOptions', async () => {
@@ -104,7 +111,6 @@ describe('runAutorunExecutor', () => {
104111
expect(output.command).toMatch(
105112
'--persist.format="md" --persist.format="json"',
106113
);
107-
expect(output.command).toMatch('--upload.apiKey="cp_1234567"');
108114
expect(output.command).toMatch('--upload.project="CLI"');
109115
});
110116

@@ -115,26 +121,23 @@ describe('runAutorunExecutor', () => {
115121
);
116122
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
117123

118-
expect(output.command).toMatch('--verbose');
124+
expect(output.command).toMatch('CP_VERBOSE="true"');
125+
expect(output.command).not.toMatch('--verbose');
119126
expect(loggerWarnSpy).toHaveBeenCalledTimes(0);
120-
expect(loggerInfoSpy).toHaveBeenCalledTimes(2);
121-
expect(loggerInfoSpy).toHaveBeenCalledWith(
122-
expect.stringContaining(`Run CLI executor`),
123-
);
124-
expect(loggerInfoSpy).toHaveBeenCalledWith(
125-
expect.stringContaining('Command: npx @code-pushup/cli'),
126-
);
127+
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
127128
});
128129

129130
it('should log command if dryRun is set', async () => {
130-
await runAutorunExecutor({ dryRun: true }, executorContext('utils'));
131+
const output = await runAutorunExecutor(
132+
{ dryRun: true },
133+
executorContext('utils'),
134+
);
131135

136+
expect(output).toStrictEqual({
137+
success: true,
138+
command: expect.stringContaining('npx @code-pushup/cli'),
139+
});
132140
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
133-
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
134-
expect(loggerWarnSpy).toHaveBeenCalledWith(
135-
expect.stringContaining(
136-
'DryRun execution of: npx @code-pushup/cli --dryRun',
137-
),
138-
);
141+
expect(loggerWarnSpy).toHaveBeenCalledTimes(0);
139142
});
140143
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function createTargets(normalizedContext: CreateTargetsOptions) {
2727
}
2828
: // if NO code-pushup.config.*.(ts|js|mjs) is present return configuration target
2929
{
30-
[`${targetName}--configuration`]: createConfigurationTarget({
30+
[`${targetName}--configuration`]: await createConfigurationTarget({
3131
projectName: normalizedContext.projectJson.name,
3232
bin,
3333
}),

0 commit comments

Comments
 (0)