Skip to content

Commit cbe02f2

Browse files
committed
feat(nx-plugin): add support for local code execution
1 parent c7d314e commit cbe02f2

File tree

11 files changed

+57
-19
lines changed

11 files changed

+57
-19
lines changed

nx.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@
344344
"releaseTagPattern": "v{version}"
345345
},
346346
"plugins": [
347+
{
348+
"plugin": "@code-pushup/nx-plugin",
349+
"options": {
350+
"pluginBin": "packages/nx-plugin/dist",
351+
"cliBin": "packages/cli/src/index.ts",
352+
"env": {
353+
"NODE_OPTIONS": "--import tsx",
354+
"TSX_TSCONFIG_PATH": "tsconfig.base.json"
355+
}
356+
}
357+
},
347358
{
348359
"plugin": "@push-based/nx-verdaccio",
349360
"options": {

packages/nx-plugin/src/executors/cli/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Show what will be executed without actually executing it:
7272
| ----------------- | --------- | ------------------------------------------------------------------ |
7373
| **projectPrefix** | `string` | prefix for upload.project on non root projects |
7474
| **dryRun** | `boolean` | To debug the executor, dry run the command without real execution. |
75-
| **bin** | `string` | Path to Code PushUp CLI |
75+
| **cliBin** | `string` | Path to Code PushUp CLI |
76+
| **pluginBin** | `string` | Path to Code PushUp Nx Plugin |
7677

7778
For all other options see the [CLI autorun documentation](../../../../cli/README.md#autorun-command).

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default async function runAutorunExecutor(
1919
context: ExecutorContext,
2020
): Promise<ExecutorOutput> {
2121
const normalizedContext = normalizeContext(context);
22-
const mergedOptions = mergeExecutorOptions(
22+
const { env, ...mergedOptions } = mergeExecutorOptions(
2323
context.target?.options,
2424
terminalAndExecutorOptions,
2525
);
@@ -43,6 +43,7 @@ export default async function runAutorunExecutor(
4343
await executeProcess({
4444
...createCliCommandObject({ command, args: cliArgumentObject }),
4545
...(context.cwd ? { cwd: context.cwd } : {}),
46+
env,
4647
});
4748
} catch (error) {
4849
logger.error(error);

packages/nx-plugin/src/executors/cli/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
"type": "string",
2222
"description": "Path to Code PushUp CLI"
2323
},
24+
"env": {
25+
"type": "object",
26+
"description": "Environment variables to set when running the command",
27+
"additionalProperties": {
28+
"type": "string"
29+
}
30+
},
2431
"verbose": {
2532
"type": "boolean",
2633
"description": "Print additional logs"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type Command =
3030
export type GlobalExecutorOptions = {
3131
command?: Command;
3232
bin?: string;
33+
env?: Record<string, string>;
3334
verbose?: boolean;
3435
progress?: boolean;
3536
config?: string;

packages/nx-plugin/src/internal/execute-process.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export type ProcessConfig = {
8686
command: string;
8787
args?: string[];
8888
cwd?: string;
89+
env?: Record<string, string>;
8990
observer?: ProcessObserver;
9091
ignoreExitCode?: boolean;
9192
};
@@ -138,7 +139,7 @@ export type ProcessObserver = {
138139
* @param cfg - see {@link ProcessConfig}
139140
*/
140141
export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
141-
const { observer, cwd, command, args, ignoreExitCode = false } = cfg;
142+
const { observer, cwd, command, args, ignoreExitCode = false, env } = cfg;
142143
const { onStdout, onError, onComplete } = observer ?? {};
143144
const date = new Date().toISOString();
144145
const start = performance.now();
@@ -152,7 +153,7 @@ export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
152153

153154
return new Promise((resolve, reject) => {
154155
// shell:true tells Windows to use shell command for spawning a child process
155-
const process = spawn(command, args, { cwd, shell: true });
156+
const process = spawn(command, args, { cwd, shell: true, env });
156157
// eslint-disable-next-line functional/no-let
157158
let stdout = '';
158159
// eslint-disable-next-line functional/no-let
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export type DynamicTargetOptions = {
22
targetName?: string;
3-
bin?: string;
3+
pluginBin?: string;
4+
cliBin?: string;
5+
env?: Record<string, string>;
46
};

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { CP_TARGET_NAME } from '../constants.js';
77
export function createConfigurationTarget(options?: {
88
targetName?: string;
99
projectName?: string;
10-
bin?: string;
10+
pluginBin?: string;
1111
}): TargetConfiguration<RunCommandsOptions> {
1212
const {
1313
projectName,
14-
bin = PACKAGE_NAME,
14+
pluginBin = PACKAGE_NAME,
1515
targetName = CP_TARGET_NAME,
1616
} = options ?? {};
1717
return {
18-
command: `nx g ${bin}:configuration ${objectToCliArgs({
18+
command: `nx g ${pluginBin}:configuration ${objectToCliArgs({
1919
skipTarget: true,
2020
targetName,
2121
...(projectName ? { project: projectName } : {}),

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import type { TargetConfiguration } from '@nx/devkit';
2+
import type { AutorunCommandExecutorOptions } from '../../executors/cli/schema.js';
23
import { PACKAGE_NAME } from '../../internal/constants.js';
3-
import type { ProjectPrefixOptions } from '../types.js';
4+
import type { CreateNodesOptions } from '../types.js';
45

5-
export function createExecutorTarget(options?: {
6-
bin?: string;
7-
projectPrefix?: string;
8-
}): TargetConfiguration<ProjectPrefixOptions> {
9-
const { bin = PACKAGE_NAME, projectPrefix } = options ?? {};
6+
export function createExecutorTarget(
7+
options?: CreateNodesOptions,
8+
): TargetConfiguration<AutorunCommandExecutorOptions> {
9+
const {
10+
pluginBin = PACKAGE_NAME,
11+
projectPrefix,
12+
cliBin,
13+
env,
14+
} = options ?? {};
1015
return {
11-
executor: `${bin}:cli`,
16+
executor: `${pluginBin}:cli`,
1217
...(projectPrefix
1318
? {
1419
options: {
1520
projectPrefix,
21+
...(cliBin ? { bin: cliBin } : {}),
22+
...(env ? { env } : {}),
1623
},
1724
}
1825
: {}),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('createExecutorTarget', () => {
99
});
1010

1111
it('should use bin if provides', () => {
12-
expect(createExecutorTarget({ bin: 'xyz' })).toStrictEqual({
12+
expect(createExecutorTarget({ pluginBin: 'xyz' })).toStrictEqual({
1313
executor: 'xyz:cli',
1414
});
1515
});

0 commit comments

Comments
 (0)