Skip to content

Commit db3fcce

Browse files
committed
feat(ci): accept custom output directory, with project name interpolation
1 parent cf29bc3 commit db3fcce

File tree

8 files changed

+48
-28
lines changed

8 files changed

+48
-28
lines changed

packages/ci/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,23 @@ 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 |
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]) |
108109

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

112+
[^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.
113+
111114
The `Logger` object has the following required properties:
112115

113116
| Property | Type | Description |

packages/ci/src/lib/cli/commands/collect.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ export async function runCollect({
1212
directory,
1313
silent,
1414
project,
15+
output,
1516
}: CommandContext): Promise<PersistedCliFiles> {
1617
const { stdout } = await executeProcess({
1718
command: bin,
1819
args: [
1920
...(config ? [`--config=${config}`] : []),
20-
...persistCliOptions({ directory, project }),
21+
...persistCliOptions({ directory, project, output }),
2122
],
2223
cwd: directory,
2324
});
2425
if (!silent) {
2526
console.info(stdout);
2627
}
2728

28-
return persistedCliFiles({ directory, project });
29+
return persistedCliFiles({ directory, project, output });
2930
}

packages/ci/src/lib/cli/commands/compare.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type CompareOptions = {
1414

1515
export async function runCompare(
1616
{ before, after, label }: CompareOptions,
17-
{ bin, config, directory, silent, project }: CommandContext,
17+
{ bin, config, directory, silent, project, output }: CommandContext,
1818
): Promise<PersistedCliFiles> {
1919
const { stdout } = await executeProcess({
2020
command: bin,
@@ -24,13 +24,13 @@ export async function runCompare(
2424
`--after=${after}`,
2525
...(label ? [`--label=${label}`] : []),
2626
...(config ? [`--config=${config}`] : []),
27-
...persistCliOptions({ directory, project }),
27+
...persistCliOptions({ directory, project, output }),
2828
],
2929
cwd: directory,
3030
});
3131
if (!silent) {
3232
console.info(stdout);
3333
}
3434

35-
return persistedCliFiles({ directory, isDiff: true, project });
35+
return persistedCliFiles({ directory, isDiff: true, project, output });
3636
}

packages/ci/src/lib/cli/commands/merge-diffs.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ import {
88

99
export async function runMergeDiffs(
1010
files: string[],
11-
{ bin, config, directory, silent }: CommandContext,
11+
{ bin, config, directory, silent, output }: CommandContext,
1212
): Promise<PersistedCliFiles<'md'>> {
1313
const { stdout } = await executeProcess({
1414
command: bin,
1515
args: [
1616
'merge-diffs',
1717
...files.map(file => `--files=${file}`),
1818
...(config ? [`--config=${config}`] : []),
19-
...persistCliOptions({ directory }),
19+
...persistCliOptions({ directory, output }),
2020
],
2121
cwd: directory,
2222
});
2323
if (!silent) {
2424
console.info(stdout);
2525
}
2626

27-
return persistedCliFiles({ directory, isDiff: true, formats: ['md'] });
27+
return persistedCliFiles({
28+
directory,
29+
isDiff: true,
30+
formats: ['md'],
31+
output,
32+
});
2833
}

packages/ci/src/lib/cli/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ProjectConfig } from '../monorepo';
33

44
export type CommandContext = Pick<
55
Settings,
6-
'bin' | 'config' | 'directory' | 'silent'
6+
'bin' | 'config' | 'directory' | 'silent' | 'output'
77
> & {
88
project?: string;
99
};
@@ -18,5 +18,6 @@ export function createCommandContext(
1818
directory: project?.directory ?? settings.directory,
1919
config: settings.config,
2020
silent: settings.silent,
21+
output: settings.output.replaceAll('{project}', project?.name ?? ''),
2122
};
2223
}

packages/ci/src/lib/cli/persist.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import path from 'node:path';
22
import {
33
DEFAULT_PERSIST_FILENAME,
44
DEFAULT_PERSIST_FORMAT,
5-
DEFAULT_PERSIST_OUTPUT_DIR,
65
type Format,
76
} from '@code-pushup/models';
87
import { projectToFilename } from '@code-pushup/utils';
@@ -22,12 +21,14 @@ export type PersistedCliFilesFormats<T extends Format = Format> = {
2221
export function persistCliOptions({
2322
directory,
2423
project,
24+
output,
2525
}: {
2626
directory: string;
2727
project?: string;
28+
output: string;
2829
}): string[] {
2930
return [
30-
`--persist.outputDir=${path.join(directory, DEFAULT_PERSIST_OUTPUT_DIR)}`,
31+
`--persist.outputDir=${path.join(directory, output)}`,
3132
`--persist.filename=${createFilename(project)}`,
3233
...DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`),
3334
];
@@ -38,13 +39,15 @@ export function persistedCliFiles<TFormat extends Format = Format>({
3839
isDiff,
3940
project,
4041
formats,
42+
output,
4143
}: {
4244
directory: string;
4345
isDiff?: boolean;
4446
project?: string;
4547
formats?: TFormat[];
48+
output: string;
4649
}): PersistedCliFiles<TFormat> {
47-
const rootDir = path.join(directory, DEFAULT_PERSIST_OUTPUT_DIR);
50+
const rootDir = path.join(directory, output);
4851
const filename = isDiff
4952
? `${createFilename(project)}-diff`
5053
: createFilename(project);
@@ -67,11 +70,15 @@ export function persistedCliFiles<TFormat extends Format = Format>({
6770
};
6871
}
6972

70-
export function findPersistedFiles(
71-
rootDir: string,
72-
files: string[],
73-
project?: string,
74-
): PersistedCliFiles {
73+
export function findPersistedFiles({
74+
rootDir,
75+
files,
76+
project,
77+
}: {
78+
rootDir: string;
79+
files: string[];
80+
project?: string;
81+
}): PersistedCliFiles {
7582
const filename = createFilename(project);
7683
const filePaths = DEFAULT_PERSIST_FORMAT.reduce((acc, format) => {
7784
const matchedFile = files.find(file => file === `${filename}.${format}`);

packages/ci/src/lib/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DEFAULT_PERSIST_OUTPUT_DIR } from '@code-pushup/models';
12
import type { Settings } from './models';
23

34
export const DEFAULT_SETTINGS: Settings = {
@@ -11,4 +12,5 @@ export const DEFAULT_SETTINGS: Settings = {
1112
debug: false,
1213
detectNewIssues: true,
1314
logger: console,
15+
output: DEFAULT_PERSIST_OUTPUT_DIR,
1416
};

packages/ci/src/lib/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type Options = {
1616
debug?: boolean;
1717
detectNewIssues?: boolean;
1818
logger?: Logger;
19+
output?: string;
1920
};
2021

2122
/**

0 commit comments

Comments
 (0)