Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions packages/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,23 @@ 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 |
| 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]) |

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

The `Logger` object has the following required properties:

| Property | Type | Description |
Expand Down
5 changes: 3 additions & 2 deletions packages/ci/src/lib/cli/commands/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ export async function runCollect({
directory,
silent,
project,
output,
}: CommandContext): Promise<PersistedCliFiles> {
const { stdout } = await executeProcess({
command: bin,
args: [
...(config ? [`--config=${config}`] : []),
...persistCliOptions({ directory, project }),
...persistCliOptions({ directory, project, output }),
],
cwd: directory,
});
if (!silent) {
console.info(stdout);
}

return persistedCliFiles({ directory, project });
return persistedCliFiles({ directory, project, output });
}
6 changes: 3 additions & 3 deletions packages/ci/src/lib/cli/commands/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type CompareOptions = {

export async function runCompare(
{ before, after, label }: CompareOptions,
{ bin, config, directory, silent, project }: CommandContext,
{ bin, config, directory, silent, project, output }: CommandContext,
): Promise<PersistedCliFiles> {
const { stdout } = await executeProcess({
command: bin,
Expand All @@ -24,13 +24,13 @@ export async function runCompare(
`--after=${after}`,
...(label ? [`--label=${label}`] : []),
...(config ? [`--config=${config}`] : []),
...persistCliOptions({ directory, project }),
...persistCliOptions({ directory, project, output }),
],
cwd: directory,
});
if (!silent) {
console.info(stdout);
}

return persistedCliFiles({ directory, isDiff: true, project });
return persistedCliFiles({ directory, isDiff: true, project, output });
}
11 changes: 8 additions & 3 deletions packages/ci/src/lib/cli/commands/merge-diffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ import {

export async function runMergeDiffs(
files: string[],
{ bin, config, directory, silent }: CommandContext,
{ bin, config, directory, silent, output }: CommandContext,
): Promise<PersistedCliFiles<'md'>> {
const { stdout } = await executeProcess({
command: bin,
args: [
'merge-diffs',
...files.map(file => `--files=${file}`),
...(config ? [`--config=${config}`] : []),
...persistCliOptions({ directory }),
...persistCliOptions({ directory, output }),
],
cwd: directory,
});
if (!silent) {
console.info(stdout);
}

return persistedCliFiles({ directory, isDiff: true, formats: ['md'] });
return persistedCliFiles({
directory,
isDiff: true,
formats: ['md'],
output,
});
}
3 changes: 2 additions & 1 deletion packages/ci/src/lib/cli/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ProjectConfig } from '../monorepo';

export type CommandContext = Pick<
Settings,
'bin' | 'config' | 'directory' | 'silent'
'bin' | 'config' | 'directory' | 'silent' | 'output'
> & {
project?: string;
};
Expand All @@ -18,5 +18,6 @@ export function createCommandContext(
directory: project?.directory ?? settings.directory,
config: settings.config,
silent: settings.silent,
output: settings.output.replaceAll('{project}', project?.name ?? ''),
};
}
101 changes: 101 additions & 0 deletions packages/ci/src/lib/cli/context.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { DEFAULT_SETTINGS } from '../constants';
import { type CommandContext, createCommandContext } from './context';

describe('createCommandContext', () => {
it('should pick CLI-related settings in standalone mode', () => {
expect(
createCommandContext(
{
bin: 'npx --no-install code-pushup',
config: null,
debug: false,
detectNewIssues: true,
directory: '/test',
logger: console,
monorepo: false,
output: '.code-pushup',
projects: null,
silent: false,
task: 'code-pushup',
},
null,
),
).toStrictEqual<CommandContext>({
project: undefined,
bin: 'npx --no-install code-pushup',
directory: '/test',
config: null,
silent: false,
output: '.code-pushup',
});
});

it('should override some settings when given monorepo project config', () => {
expect(
createCommandContext(
{
bin: 'npx --no-install code-pushup',
config: null,
debug: false,
detectNewIssues: true,
directory: '/test',
logger: console,
monorepo: false,
output: '.code-pushup',
projects: null,
silent: false,
task: 'code-pushup',
},
{
name: 'ui',
directory: '/test/ui',
bin: 'yarn code-pushup',
},
),
).toStrictEqual<CommandContext>({
project: 'ui',
bin: 'yarn code-pushup',
directory: '/test/ui',
config: null,
silent: false,
output: '.code-pushup',
});
});

it('should interpolate project name in output path for monorepo project', () => {
expect(
createCommandContext(
{
...DEFAULT_SETTINGS,
output: '.code-pushup/{project}',
},
{
name: 'website',
bin: 'npx nx run website:code-pushup --',
},
),
).toEqual(
expect.objectContaining<Partial<CommandContext>>({
project: 'website',
bin: 'npx nx run website:code-pushup --',
output: '.code-pushup/website',
}),
);
});

it('should omit {project} placeholder in output path when in standalone mode', () => {
expect(
createCommandContext(
{
...DEFAULT_SETTINGS,
output: '.code-pushup/{project}',
},
undefined,
),
).toEqual(
expect.objectContaining<Partial<CommandContext>>({
output: '.code-pushup/',
}),
);
});
});
23 changes: 15 additions & 8 deletions packages/ci/src/lib/cli/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from 'node:path';
import {
DEFAULT_PERSIST_FILENAME,
DEFAULT_PERSIST_FORMAT,
DEFAULT_PERSIST_OUTPUT_DIR,
type Format,
} from '@code-pushup/models';
import { projectToFilename } from '@code-pushup/utils';
Expand All @@ -22,12 +21,14 @@ export type PersistedCliFilesFormats<T extends Format = Format> = {
export function persistCliOptions({
directory,
project,
output,
}: {
directory: string;
project?: string;
output: string;
}): string[] {
return [
`--persist.outputDir=${path.join(directory, DEFAULT_PERSIST_OUTPUT_DIR)}`,
`--persist.outputDir=${path.join(directory, output)}`,
`--persist.filename=${createFilename(project)}`,
...DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`),
];
Expand All @@ -38,13 +39,15 @@ export function persistedCliFiles<TFormat extends Format = Format>({
isDiff,
project,
formats,
output,
}: {
directory: string;
isDiff?: boolean;
project?: string;
formats?: TFormat[];
output: string;
}): PersistedCliFiles<TFormat> {
const rootDir = path.join(directory, DEFAULT_PERSIST_OUTPUT_DIR);
const rootDir = path.join(directory, output);
const filename = isDiff
? `${createFilename(project)}-diff`
: createFilename(project);
Expand All @@ -67,11 +70,15 @@ export function persistedCliFiles<TFormat extends Format = Format>({
};
}

export function findPersistedFiles(
rootDir: string,
files: string[],
project?: string,
): PersistedCliFiles {
export function findPersistedFiles({
rootDir,
files,
project,
}: {
rootDir: string;
files: string[];
project?: string;
}): PersistedCliFiles {
const filename = createFilename(project);
const filePaths = DEFAULT_PERSIST_FORMAT.reduce((acc, format) => {
const matchedFile = files.find(file => file === `${filename}.${format}`);
Expand Down
Loading