Skip to content

Commit f270316

Browse files
author
John Doe
committed
refactor: intro evn options to cli executor
1 parent b95e56c commit f270316

7 files changed

Lines changed: 54 additions & 19 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ export default async function runAutorunExecutor(
2323
terminalAndExecutorOptions,
2424
normalizedContext,
2525
);
26-
const { dryRun, verbose, command, bin } = terminalAndExecutorOptions;
26+
const {
27+
dryRun,
28+
verbose,
29+
command,
30+
bin,
31+
env: targetEnv,
32+
} = terminalAndExecutorOptions;
2733
const commandString = createCliCommandString({
2834
command,
2935
args: cliArgumentObject,
@@ -40,6 +46,7 @@ export default async function runAutorunExecutor(
4046
await executeProcess({
4147
...createCliCommandObject({ command, args: cliArgumentObject, bin }),
4248
...(context.cwd ? { cwd: context.cwd } : {}),
49+
...(targetEnv ? { env: targetEnv } : {}),
4350
});
4451
} catch (error) {
4552
logger.error(error);

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ describe('runAutorunExecutor', () => {
4646
const output = await runAutorunExecutor({}, executorContext('utils'));
4747
expect(output.success).toBe(true);
4848
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,
53-
});
49+
expect(executeProcessSpy).toHaveBeenCalledWith(
50+
expect.objectContaining({
51+
command: 'npx',
52+
args: expect.arrayContaining(['@code-pushup/cli']),
53+
cwd: MEMFS_VOLUME,
54+
}),
55+
);
5456
});
5557

5658
it('should normalize context', async () => {
@@ -62,12 +64,15 @@ describe('runAutorunExecutor', () => {
6264
},
6365
);
6466
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-
});
67+
expect(output.command).toMatch('npx @code-pushup/cli');
68+
expect(output.command).toContain('cwd-form-context');
69+
expect(executeProcessSpy).toHaveBeenCalledWith(
70+
expect.objectContaining({
71+
command: 'npx',
72+
args: expect.arrayContaining(['@code-pushup/cli']),
73+
cwd: 'cwd-form-context',
74+
}),
75+
);
7176
});
7277

7378
it('should process executorOptions', async () => {

packages/nx-plugin/src/generators/configuration/schema.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { DynamicTargetOptions } from '../../internal/types.js';
22

33
export type ConfigurationGeneratorOptions = {
44
project: string;
5-
bin?: string;
65
skipTarget?: boolean;
76
skipConfig?: boolean;
87
skipFormat?: boolean;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type DynamicTargetOptions = {
22
targetName?: string;
33
bin?: string;
4+
env?: Record<string, string>;
45
};

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import type { ProjectPrefixOptions } from '../types.js';
55
export function createExecutorTarget(options?: {
66
bin?: string;
77
projectPrefix?: string;
8-
}): TargetConfiguration<ProjectPrefixOptions> {
9-
const { bin, projectPrefix } = options ?? {};
8+
env?: Record<string, string>;
9+
}): TargetConfiguration<
10+
ProjectPrefixOptions & { env?: Record<string, string> }
11+
> {
12+
const { bin, projectPrefix, env } = options ?? {};
1013

1114
const executor = `${PACKAGE_NAME}:cli`;
12-
const executorOptions = (bin || projectPrefix) && {
15+
const executorOptions = (bin || projectPrefix || env) && {
1316
...(bin && { bin }),
1417
...(projectPrefix && { projectPrefix }),
18+
...(env && { env }),
1519
};
1620
return { executor, ...(executorOptions && { options: executorOptions }) };
1721
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,23 @@ describe('createExecutorTarget', () => {
2727
},
2828
});
2929
});
30+
31+
it('should use env if provided', () => {
32+
expect(
33+
createExecutorTarget({
34+
env: {
35+
NODE_OPTIONS: '--import tsx',
36+
TSX_TSCONFIG_PATH: 'tsconfig.base.json',
37+
},
38+
}),
39+
).toStrictEqual({
40+
executor: '@code-pushup/nx-plugin:cli',
41+
options: {
42+
env: {
43+
NODE_OPTIONS: '--import tsx',
44+
TSX_TSCONFIG_PATH: 'tsconfig.base.json',
45+
},
46+
},
47+
});
48+
});
3049
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ export async function createTargets(normalizedContext: CreateTargetsOptions) {
1919
targetName = CP_TARGET_NAME,
2020
bin,
2121
projectPrefix,
22+
env,
2223
} = normalizedContext.createOptions;
2324
const rootFiles = await readdir(normalizedContext.projectRoot);
2425
return rootFiles.some(filename => filename.match(CODE_PUSHUP_CONFIG_REGEX))
2526
? {
26-
[targetName]: createExecutorTarget({ bin, projectPrefix }),
27+
[targetName]: createExecutorTarget({ bin, projectPrefix, env }),
2728
}
2829
: // if NO code-pushup.config.*.(ts|js|mjs) is present return configuration target
2930
{
30-
[`${targetName}--configuration`]: createConfigurationTarget({
31+
[`${targetName}--configuration`]: await createConfigurationTarget({
3132
projectName: normalizedContext.projectJson.name,
32-
bin,
3333
}),
3434
};
3535
}

0 commit comments

Comments
 (0)