Skip to content

Commit

Permalink
feat(ci): accept custom output directory, with project name interpola…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
matejchalk committed Nov 25, 2024
1 parent cf29bc3 commit db3fcce
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
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 ?? ''),
};
}
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
2 changes: 2 additions & 0 deletions packages/ci/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_PERSIST_OUTPUT_DIR } from '@code-pushup/models';
import type { Settings } from './models';

export const DEFAULT_SETTINGS: Settings = {
Expand All @@ -11,4 +12,5 @@ export const DEFAULT_SETTINGS: Settings = {
debug: false,
detectNewIssues: true,
logger: console,
output: DEFAULT_PERSIST_OUTPUT_DIR,
};
1 change: 1 addition & 0 deletions packages/ci/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type Options = {
debug?: boolean;
detectNewIssues?: boolean;
logger?: Logger;
output?: string;
};

/**
Expand Down

0 comments on commit db3fcce

Please sign in to comment.