Skip to content

Commit

Permalink
feat(ci): add nxProjectsFilter option, forwards custom filters to Nx CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Nov 29, 2024
1 parent effd5d2 commit 93a6a42
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
27 changes: 15 additions & 12 deletions packages/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,26 @@ A `Comment` object has the following required properties:

Optionally, you can override default options for further customization:

| Property | Type | Default | Description |
| :---------------- | :------------------------ | :------------------------------- | :----------------------------------------------------------------------------------- |
| `monorepo` | `boolean \| MonorepoTool` | `false` | Enables [monorepo mode](#monorepo-mode) |
| `projects` | `string[] \| null` | `null` | Custom projects configuration for [monorepo mode](#monorepo-mode) |
| `task` | `string` | `'code-pushup'` | Name of command to run Code PushUp per project in [monorepo mode](#monorepo-mode) |
| `directory` | `string` | `process.cwd()` | Directory in which Code PushUp CLI should run |
| `config` | `string \| null` | `null` [^1] | Path to config file (`--config` option) |
| `silent` | `boolean` | `false` | Toggles if logs from CLI commands are printed |
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
| `logger` | `Logger` | `console` | Logger for reporting progress and encountered problems |
| `output` | `string` | `'.code-pushup'` | Directory where Code PushUp reports will be created (interpolates project name [^2]) |
| Property | Type | Default | Description |
| :----------------- | :------------------------ | :------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `monorepo` | `boolean \| MonorepoTool` | `false` | Enables [monorepo mode](#monorepo-mode) |
| `projects` | `string[] \| null` | `null` | Custom projects configuration for [monorepo mode](#monorepo-mode) |
| `task` | `string` | `'code-pushup'` | Name of command to run Code PushUp per project in [monorepo mode](#monorepo-mode) |
| `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] |
| `directory` | `string` | `process.cwd()` | Directory in which Code PushUp CLI should run |
| `config` | `string \| null` | `null` [^1] | Path to config file (`--config` option) |
| `silent` | `boolean` | `false` | Toggles if logs from CLI commands are printed |
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
| `logger` | `Logger` | `console` | Logger for reporting progress and encountered problems |
| `output` | `string` | `'.code-pushup'` | Directory where Code PushUp reports will be created (interpolates project name [^2]) |

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

[^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.

[^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.

The `Logger` object has the following required properties:

| Property | Type | Description |
Expand Down
1 change: 1 addition & 0 deletions packages/ci/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const DEFAULT_SETTINGS: Settings = {
detectNewIssues: true,
logger: console,
output: DEFAULT_PERSIST_OUTPUT_DIR,
nxProjectsFilter: '--with-target={task}',
};
1 change: 1 addition & 0 deletions packages/ci/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Options = {
monorepo?: boolean | MonorepoTool;
projects?: string[] | null;
task?: string;
nxProjectsFilter?: string | string[];
bin?: string;
config?: string | null;
directory?: string;
Expand Down
17 changes: 13 additions & 4 deletions packages/ci/src/lib/monorepo/handlers/nx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { join } from 'node:path';
import { executeProcess, fileExists, stringifyError } from '@code-pushup/utils';
import {
executeProcess,
fileExists,
stringifyError,
toArray,
} from '@code-pushup/utils';
import type { MonorepoToolHandler } from '../tools';

export const nxHandler: MonorepoToolHandler = {
Expand All @@ -9,24 +14,28 @@ export const nxHandler: MonorepoToolHandler = {
(await fileExists(join(options.cwd, 'nx.json'))) &&
(
await executeProcess({
...options,
command: 'npx',
args: ['nx', 'report'],
cwd: options.cwd,
observer: options.observer,
})
).code === 0
);
},
async listProjects(options) {
const { stdout } = await executeProcess({
...options,
command: 'npx',
args: [
'nx',
'show',
'projects',
`--with-target=${options.task}`,
...toArray(options.nxProjectsFilter).map(arg =>
arg.replaceAll('{task}', options.task),
),
'--json',
],
cwd: options.cwd,
observer: options.observer,
});
const projects = parseProjects(stdout);
return projects.map(project => ({
Expand Down
1 change: 1 addition & 0 deletions packages/ci/src/lib/monorepo/list-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function createMonorepoHandlerOptions(
return {
task: settings.task,
cwd: settings.directory,
nxProjectsFilter: settings.nxProjectsFilter,
...(!settings.silent && {
observer: {
onStdout: stdout => {
Expand Down
10 changes: 10 additions & 0 deletions packages/ci/src/lib/monorepo/list-projects.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('listMonorepoProjects', () => {
monorepo: true,
projects: null,
task: 'code-pushup',
nxProjectsFilter: '--with-target={task}',
directory: MEMFS_VOLUME,
bin: 'npx --no-install code-pushup',
logger: {
Expand Down Expand Up @@ -56,6 +57,15 @@ describe('listMonorepoProjects', () => {
{ name: 'backend', bin: 'npx nx run backend:code-pushup --' },
{ name: 'frontend', bin: 'npx nx run frontend:code-pushup --' },
] satisfies ProjectConfig[]);

expect(utils.executeProcess).toHaveBeenCalledWith<
Parameters<(typeof utils)['executeProcess']>
>({
command: 'npx',
args: ['nx', 'show', 'projects', '--with-target=code-pushup', '--json'],
cwd: process.cwd(),
observer: expect.any(Object),
});
});

it('should detect projects in Turborepo which have code-pushup command', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/ci/src/lib/monorepo/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type MonorepoHandlerOptions = {
task: string;
cwd: string;
observer?: ProcessObserver;
nxProjectsFilter: string | string[];
};

export type ProjectConfig = {
Expand Down

0 comments on commit 93a6a42

Please sign in to comment.