Skip to content

Commit 93a6a42

Browse files
committed
feat(ci): add nxProjectsFilter option, forwards custom filters to Nx CLI
1 parent effd5d2 commit 93a6a42

File tree

7 files changed

+42
-16
lines changed

7 files changed

+42
-16
lines changed

packages/ci/README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,26 @@ A `Comment` object has the following required properties:
9494

9595
Optionally, you can override default options for further customization:
9696

97-
| Property | Type | Default | Description |
98-
| :---------------- | :------------------------ | :------------------------------- | :----------------------------------------------------------------------------------- |
99-
| `monorepo` | `boolean \| MonorepoTool` | `false` | Enables [monorepo mode](#monorepo-mode) |
100-
| `projects` | `string[] \| null` | `null` | Custom projects configuration for [monorepo mode](#monorepo-mode) |
101-
| `task` | `string` | `'code-pushup'` | Name of command to run Code PushUp per project in [monorepo mode](#monorepo-mode) |
102-
| `directory` | `string` | `process.cwd()` | Directory in which Code PushUp CLI should run |
103-
| `config` | `string \| null` | `null` [^1] | Path to config file (`--config` option) |
104-
| `silent` | `boolean` | `false` | Toggles if logs from CLI commands are printed |
105-
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
106-
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
107-
| `logger` | `Logger` | `console` | Logger for reporting progress and encountered problems |
108-
| `output` | `string` | `'.code-pushup'` | Directory where Code PushUp reports will be created (interpolates project name [^2]) |
97+
| Property | Type | Default | Description |
98+
| :----------------- | :------------------------ | :------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------- |
99+
| `monorepo` | `boolean \| MonorepoTool` | `false` | Enables [monorepo mode](#monorepo-mode) |
100+
| `projects` | `string[] \| null` | `null` | Custom projects configuration for [monorepo mode](#monorepo-mode) |
101+
| `task` | `string` | `'code-pushup'` | Name of command to run Code PushUp per project in [monorepo mode](#monorepo-mode) |
102+
| `nxProjectsFilter` | `string \| string[]` | `'--with-target={task}'` | Arguments passed to [`nx show projects`](https://nx.dev/nx-api/nx/documents/show#projects), only relevant for Nx in [monorepo mode](#monorepo-mode) [^3] |
103+
| `directory` | `string` | `process.cwd()` | Directory in which Code PushUp CLI should run |
104+
| `config` | `string \| null` | `null` [^1] | Path to config file (`--config` option) |
105+
| `silent` | `boolean` | `false` | Toggles if logs from CLI commands are printed |
106+
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
107+
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
108+
| `logger` | `Logger` | `console` | Logger for reporting progress and encountered problems |
109+
| `output` | `string` | `'.code-pushup'` | Directory where Code PushUp reports will be created (interpolates project name [^2]) |
109110

110111
[^1]: By default, the `code-pushup.config` file is autodetected as described in [`@code-pushup/cli` docs](../cli/README.md#configuration).
111112

112113
[^2]: In monorepo mode, any occurrence of `{project}` in the `output` path will be replaced with a project name. This separation of folders per project (e.g. `output: '.code-pushup/{project}'`) may be useful for caching purposes.
113114

115+
[^3]: The `{task}` pattern is replaced with the `task` value, so the default behaviour is to list projects using `npx nx show projects --with-target=code-pushup --json`. The `nxProjectsFilter` options gives Nx users the flexibility to filter projects in alternative ways supported by the Nx CLI (e.g. `--affected`, `--projects`, `--exclude`, `--type`) - refer to [options in Nx docs](https://nx.dev/nx-api/nx/documents/show#options) for details.
116+
114117
The `Logger` object has the following required properties:
115118

116119
| Property | Type | Description |

packages/ci/src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export const DEFAULT_SETTINGS: Settings = {
1313
detectNewIssues: true,
1414
logger: console,
1515
output: DEFAULT_PERSIST_OUTPUT_DIR,
16+
nxProjectsFilter: '--with-target={task}',
1617
};

packages/ci/src/lib/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type Options = {
99
monorepo?: boolean | MonorepoTool;
1010
projects?: string[] | null;
1111
task?: string;
12+
nxProjectsFilter?: string | string[];
1213
bin?: string;
1314
config?: string | null;
1415
directory?: string;

packages/ci/src/lib/monorepo/handlers/nx.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { join } from 'node:path';
2-
import { executeProcess, fileExists, stringifyError } from '@code-pushup/utils';
2+
import {
3+
executeProcess,
4+
fileExists,
5+
stringifyError,
6+
toArray,
7+
} from '@code-pushup/utils';
38
import type { MonorepoToolHandler } from '../tools';
49

510
export const nxHandler: MonorepoToolHandler = {
@@ -9,24 +14,28 @@ export const nxHandler: MonorepoToolHandler = {
914
(await fileExists(join(options.cwd, 'nx.json'))) &&
1015
(
1116
await executeProcess({
12-
...options,
1317
command: 'npx',
1418
args: ['nx', 'report'],
19+
cwd: options.cwd,
20+
observer: options.observer,
1521
})
1622
).code === 0
1723
);
1824
},
1925
async listProjects(options) {
2026
const { stdout } = await executeProcess({
21-
...options,
2227
command: 'npx',
2328
args: [
2429
'nx',
2530
'show',
2631
'projects',
27-
`--with-target=${options.task}`,
32+
...toArray(options.nxProjectsFilter).map(arg =>
33+
arg.replaceAll('{task}', options.task),
34+
),
2835
'--json',
2936
],
37+
cwd: options.cwd,
38+
observer: options.observer,
3039
});
3140
const projects = parseProjects(stdout);
3241
return projects.map(project => ({

packages/ci/src/lib/monorepo/list-projects.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function createMonorepoHandlerOptions(
6161
return {
6262
task: settings.task,
6363
cwd: settings.directory,
64+
nxProjectsFilter: settings.nxProjectsFilter,
6465
...(!settings.silent && {
6566
observer: {
6667
onStdout: stdout => {

packages/ci/src/lib/monorepo/list-projects.unit.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('listMonorepoProjects', () => {
1414
monorepo: true,
1515
projects: null,
1616
task: 'code-pushup',
17+
nxProjectsFilter: '--with-target={task}',
1718
directory: MEMFS_VOLUME,
1819
bin: 'npx --no-install code-pushup',
1920
logger: {
@@ -56,6 +57,15 @@ describe('listMonorepoProjects', () => {
5657
{ name: 'backend', bin: 'npx nx run backend:code-pushup --' },
5758
{ name: 'frontend', bin: 'npx nx run frontend:code-pushup --' },
5859
] satisfies ProjectConfig[]);
60+
61+
expect(utils.executeProcess).toHaveBeenCalledWith<
62+
Parameters<(typeof utils)['executeProcess']>
63+
>({
64+
command: 'npx',
65+
args: ['nx', 'show', 'projects', '--with-target=code-pushup', '--json'],
66+
cwd: process.cwd(),
67+
observer: expect.any(Object),
68+
});
5969
});
6070

6171
it('should detect projects in Turborepo which have code-pushup command', async () => {

packages/ci/src/lib/monorepo/tools.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type MonorepoHandlerOptions = {
1313
task: string;
1414
cwd: string;
1515
observer?: ProcessObserver;
16+
nxProjectsFilter: string | string[];
1617
};
1718

1819
export type ProjectConfig = {

0 commit comments

Comments
 (0)