Skip to content

Commit

Permalink
feat(ci): detect persist config from print-config
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Dec 16, 2024
1 parent 836b242 commit ad8bd28
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 445 deletions.
25 changes: 8 additions & 17 deletions e2e/ci-e2e/tests/ci.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ describe('CI package', () => {
),
).resolves.toEqual({
mode: 'standalone',
artifacts: {
files: {
report: {
rootDir: outputDir,
files: [
join(outputDir, 'report.json'),
join(outputDir, 'report.md'),
],
json: join(outputDir, 'report.json'),
md: join(outputDir, 'report.md'),
},
},
} satisfies RunResult);
Expand Down Expand Up @@ -156,20 +153,14 @@ describe('CI package', () => {
mode: 'standalone',
commentId: comment.id,
newIssues: [],
artifacts: {
files: {
report: {
rootDir: outputDir,
files: [
join(outputDir, 'report.json'),
join(outputDir, 'report.md'),
],
json: join(outputDir, 'report.json'),
md: join(outputDir, 'report.md'),
},
diff: {
rootDir: outputDir,
files: [
join(outputDir, 'report-diff.json'),
join(outputDir, 'report-diff.md'),
],
json: join(outputDir, 'report-diff.json'),
md: join(outputDir, 'report-diff.md'),
},
},
} satisfies RunResult);
Expand Down
6 changes: 3 additions & 3 deletions packages/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const result = await runInCI(refs, api);
if (result.mode === 'standalone') {
const {
// output files, can be uploaded as job artifact
artifacts: { report, diff },
files: { report, diff },
// ID of created/updated PR comment
commentId,
// array of source code issues, can be used to annotate changed files in PR
Expand Down Expand Up @@ -211,15 +211,15 @@ if (result.mode === 'monorepo') {
// ID of created/updated PR comment
commentId,
// merged report-diff.md used in PR comment, can also be uploaded as job artifact
diffArtifact,
diffPath,
} = result;

for (const project of projects) {
const {
// detected project name (from package.json, project.json or folder name)
name,
// output files, can be uploaded as job artifacts
artifacts: { report, diff },
files: { report, diff },
// array of source code issues, can be used to annotate changed files in PR
newIssues,
} = project;
Expand Down
3 changes: 2 additions & 1 deletion packages/ci/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@code-pushup/utils": "0.56.0",
"glob": "^10.4.5",
"simple-git": "^3.20.0",
"yaml": "^2.5.1"
"yaml": "^2.5.1",
"zod": "^3.22.1"
}
}
14 changes: 3 additions & 11 deletions packages/ci/src/lib/cli/commands/collect.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';
import {
type PersistedCliFiles,
persistCliOptions,
persistedCliFiles,
} from '../persist.js';

export async function runCollect({
bin,
config,
directory,
silent,
project,
output,
}: CommandContext): Promise<PersistedCliFiles> {
}: CommandContext): Promise<void> {
const { stdout } = await executeProcess({
command: bin,
args: [
...(config ? [`--config=${config}`] : []),
...persistCliOptions({ directory, project, output }),
...DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`),
],
cwd: directory,
});
if (!silent) {
console.info(stdout);
}

return persistedCliFiles({ directory, project, output });
}
14 changes: 4 additions & 10 deletions packages/ci/src/lib/cli/commands/compare.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';
import {
type PersistedCliFiles,
persistCliOptions,
persistedCliFiles,
} from '../persist.js';

type CompareOptions = {
before: string;
Expand All @@ -14,8 +10,8 @@ type CompareOptions = {

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

return persistedCliFiles({ directory, isDiff: true, project, output });
}
27 changes: 13 additions & 14 deletions packages/ci/src/lib/cli/commands/merge-diffs.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { join } from 'node:path';
import {
DEFAULT_PERSIST_FILENAME,
DEFAULT_PERSIST_OUTPUT_DIR,
} from '@code-pushup/models';
import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';
import {
type PersistedCliFiles,
persistCliOptions,
persistedCliFiles,
} from '../persist.js';

export async function runMergeDiffs(
files: string[],
{ bin, config, directory, silent, output }: CommandContext,
): Promise<PersistedCliFiles<'md'>> {
{ bin, config, directory, silent }: CommandContext,
): Promise<string> {
const outputDir = join(process.cwd(), DEFAULT_PERSIST_OUTPUT_DIR);
const filename = `merged-${DEFAULT_PERSIST_FILENAME}`;

const { stdout } = await executeProcess({
command: bin,
args: [
'merge-diffs',
...files.map(file => `--files=${file}`),
...(config ? [`--config=${config}`] : []),
...persistCliOptions({ directory, output }),
`--persist.outputDir=${outputDir}`,
`--persist.filename=${filename}`,
],
cwd: directory,
});
if (!silent) {
console.info(stdout);
}

return persistedCliFiles({
directory,
isDiff: true,
formats: ['md'],
output,
});
return join(outputDir, `${filename}-diff.md`);
}
11 changes: 9 additions & 2 deletions packages/ci/src/lib/cli/commands/print-config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { executeProcess } from '@code-pushup/utils';
import { executeProcess, stringifyError } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';

export async function runPrintConfig({
bin,
config,
directory,
silent,
}: CommandContext): Promise<void> {
}: CommandContext): Promise<unknown> {
const { stdout } = await executeProcess({
command: bin,
args: [...(config ? [`--config=${config}`] : []), 'print-config'],
Expand All @@ -15,4 +15,11 @@ export async function runPrintConfig({
if (!silent) {
console.info(stdout);
}
try {
return JSON.parse(stdout) as unknown;
} catch (error) {
throw new Error(
`Error parsing output of print-config command - ${stringifyError(error)}`,
);
}
}
8 changes: 2 additions & 6 deletions packages/ci/src/lib/cli/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ import type { ProjectConfig } from '../monorepo/index.js';

export type CommandContext = Pick<
Settings,
'bin' | 'config' | 'directory' | 'silent' | 'output'
> & {
project?: string;
};
'bin' | 'config' | 'directory' | 'silent'
>;

export function createCommandContext(
settings: Settings,
project: ProjectConfig | null | undefined,
): CommandContext {
return {
project: project?.name,
bin: project?.bin ?? settings.bin,
directory: project?.directory ?? settings.directory,
config: settings.config,
silent: settings.silent,
output: settings.output.replaceAll('{project}', project?.name ?? ''),
};
}
44 changes: 2 additions & 42 deletions packages/ci/src/lib/cli/context.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DEFAULT_SETTINGS } from '../constants.js';
import { type CommandContext, createCommandContext } from './context.js';

describe('createCommandContext', () => {
Expand All @@ -13,6 +12,7 @@ describe('createCommandContext', () => {
directory: '/test',
logger: console,
monorepo: false,
nxProjectsFilter: '--with-target={task}',
output: '.code-pushup',
projects: null,
silent: false,
Expand All @@ -21,12 +21,10 @@ describe('createCommandContext', () => {
null,
),
).toStrictEqual<CommandContext>({
project: undefined,
bin: 'npx --no-install code-pushup',
directory: '/test',
config: null,
silent: false,
output: '.code-pushup',
});
});

Expand All @@ -41,6 +39,7 @@ describe('createCommandContext', () => {
directory: '/test',
logger: console,
monorepo: false,
nxProjectsFilter: '--with-target={task}',
output: '.code-pushup',
projects: null,
silent: false,
Expand All @@ -53,49 +52,10 @@ describe('createCommandContext', () => {
},
),
).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/',
}),
);
});
});
2 changes: 1 addition & 1 deletion packages/ci/src/lib/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export { runCompare } from './commands/compare.js';
export { runMergeDiffs } from './commands/merge-diffs.js';
export { runPrintConfig } from './commands/print-config.js';
export { createCommandContext, type CommandContext } from './context.js';
export { findPersistedFiles, type PersistedCliFiles } from './persist.js';
export { persistedFilesFromConfig } from './persist.js';
Loading

0 comments on commit ad8bd28

Please sign in to comment.