Skip to content

Commit ad8bd28

Browse files
committed
feat(ci): detect persist config from print-config
1 parent 836b242 commit ad8bd28

File tree

16 files changed

+294
-445
lines changed

16 files changed

+294
-445
lines changed

e2e/ci-e2e/tests/ci.e2e.test.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,10 @@ describe('CI package', () => {
8787
),
8888
).resolves.toEqual({
8989
mode: 'standalone',
90-
artifacts: {
90+
files: {
9191
report: {
92-
rootDir: outputDir,
93-
files: [
94-
join(outputDir, 'report.json'),
95-
join(outputDir, 'report.md'),
96-
],
92+
json: join(outputDir, 'report.json'),
93+
md: join(outputDir, 'report.md'),
9794
},
9895
},
9996
} satisfies RunResult);
@@ -156,20 +153,14 @@ describe('CI package', () => {
156153
mode: 'standalone',
157154
commentId: comment.id,
158155
newIssues: [],
159-
artifacts: {
156+
files: {
160157
report: {
161-
rootDir: outputDir,
162-
files: [
163-
join(outputDir, 'report.json'),
164-
join(outputDir, 'report.md'),
165-
],
158+
json: join(outputDir, 'report.json'),
159+
md: join(outputDir, 'report.md'),
166160
},
167161
diff: {
168-
rootDir: outputDir,
169-
files: [
170-
join(outputDir, 'report-diff.json'),
171-
join(outputDir, 'report-diff.md'),
172-
],
162+
json: join(outputDir, 'report-diff.json'),
163+
md: join(outputDir, 'report-diff.md'),
173164
},
174165
},
175166
} satisfies RunResult);

packages/ci/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const result = await runInCI(refs, api);
138138
if (result.mode === 'standalone') {
139139
const {
140140
// output files, can be uploaded as job artifact
141-
artifacts: { report, diff },
141+
files: { report, diff },
142142
// ID of created/updated PR comment
143143
commentId,
144144
// array of source code issues, can be used to annotate changed files in PR
@@ -211,15 +211,15 @@ if (result.mode === 'monorepo') {
211211
// ID of created/updated PR comment
212212
commentId,
213213
// merged report-diff.md used in PR comment, can also be uploaded as job artifact
214-
diffArtifact,
214+
diffPath,
215215
} = result;
216216

217217
for (const project of projects) {
218218
const {
219219
// detected project name (from package.json, project.json or folder name)
220220
name,
221221
// output files, can be uploaded as job artifacts
222-
artifacts: { report, diff },
222+
files: { report, diff },
223223
// array of source code issues, can be used to annotate changed files in PR
224224
newIssues,
225225
} = project;

packages/ci/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@code-pushup/utils": "0.56.0",
3131
"glob": "^10.4.5",
3232
"simple-git": "^3.20.0",
33-
"yaml": "^2.5.1"
33+
"yaml": "^2.5.1",
34+
"zod": "^3.22.1"
3435
}
3536
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1+
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
12
import { executeProcess } from '@code-pushup/utils';
23
import type { CommandContext } from '../context.js';
3-
import {
4-
type PersistedCliFiles,
5-
persistCliOptions,
6-
persistedCliFiles,
7-
} from '../persist.js';
84

95
export async function runCollect({
106
bin,
117
config,
128
directory,
139
silent,
14-
project,
15-
output,
16-
}: CommandContext): Promise<PersistedCliFiles> {
10+
}: CommandContext): Promise<void> {
1711
const { stdout } = await executeProcess({
1812
command: bin,
1913
args: [
2014
...(config ? [`--config=${config}`] : []),
21-
...persistCliOptions({ directory, project, output }),
15+
...DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`),
2216
],
2317
cwd: directory,
2418
});
2519
if (!silent) {
2620
console.info(stdout);
2721
}
28-
29-
return persistedCliFiles({ directory, project, output });
3022
}
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1+
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
12
import { executeProcess } from '@code-pushup/utils';
23
import type { CommandContext } from '../context.js';
3-
import {
4-
type PersistedCliFiles,
5-
persistCliOptions,
6-
persistedCliFiles,
7-
} from '../persist.js';
84

95
type CompareOptions = {
106
before: string;
@@ -14,8 +10,8 @@ type CompareOptions = {
1410

1511
export async function runCompare(
1612
{ before, after, label }: CompareOptions,
17-
{ bin, config, directory, silent, project, output }: CommandContext,
18-
): Promise<PersistedCliFiles> {
13+
{ bin, config, directory, silent }: CommandContext,
14+
): Promise<void> {
1915
const { stdout } = await executeProcess({
2016
command: bin,
2117
args: [
@@ -24,13 +20,11 @@ export async function runCompare(
2420
`--after=${after}`,
2521
...(label ? [`--label=${label}`] : []),
2622
...(config ? [`--config=${config}`] : []),
27-
...persistCliOptions({ directory, project, output }),
23+
...DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`),
2824
],
2925
cwd: directory,
3026
});
3127
if (!silent) {
3228
console.info(stdout);
3329
}
34-
35-
return persistedCliFiles({ directory, isDiff: true, project, output });
3630
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1+
import { join } from 'node:path';
2+
import {
3+
DEFAULT_PERSIST_FILENAME,
4+
DEFAULT_PERSIST_OUTPUT_DIR,
5+
} from '@code-pushup/models';
16
import { executeProcess } from '@code-pushup/utils';
27
import type { CommandContext } from '../context.js';
3-
import {
4-
type PersistedCliFiles,
5-
persistCliOptions,
6-
persistedCliFiles,
7-
} from '../persist.js';
88

99
export async function runMergeDiffs(
1010
files: string[],
11-
{ bin, config, directory, silent, output }: CommandContext,
12-
): Promise<PersistedCliFiles<'md'>> {
11+
{ bin, config, directory, silent }: CommandContext,
12+
): Promise<string> {
13+
const outputDir = join(process.cwd(), DEFAULT_PERSIST_OUTPUT_DIR);
14+
const filename = `merged-${DEFAULT_PERSIST_FILENAME}`;
15+
1316
const { stdout } = await executeProcess({
1417
command: bin,
1518
args: [
1619
'merge-diffs',
1720
...files.map(file => `--files=${file}`),
1821
...(config ? [`--config=${config}`] : []),
19-
...persistCliOptions({ directory, output }),
22+
`--persist.outputDir=${outputDir}`,
23+
`--persist.filename=${filename}`,
2024
],
2125
cwd: directory,
2226
});
2327
if (!silent) {
2428
console.info(stdout);
2529
}
2630

27-
return persistedCliFiles({
28-
directory,
29-
isDiff: true,
30-
formats: ['md'],
31-
output,
32-
});
31+
return join(outputDir, `${filename}-diff.md`);
3332
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { executeProcess } from '@code-pushup/utils';
1+
import { executeProcess, stringifyError } from '@code-pushup/utils';
22
import type { CommandContext } from '../context.js';
33

44
export async function runPrintConfig({
55
bin,
66
config,
77
directory,
88
silent,
9-
}: CommandContext): Promise<void> {
9+
}: CommandContext): Promise<unknown> {
1010
const { stdout } = await executeProcess({
1111
command: bin,
1212
args: [...(config ? [`--config=${config}`] : []), 'print-config'],
@@ -15,4 +15,11 @@ export async function runPrintConfig({
1515
if (!silent) {
1616
console.info(stdout);
1717
}
18+
try {
19+
return JSON.parse(stdout) as unknown;
20+
} catch (error) {
21+
throw new Error(
22+
`Error parsing output of print-config command - ${stringifyError(error)}`,
23+
);
24+
}
1825
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@ import type { ProjectConfig } from '../monorepo/index.js';
33

44
export type CommandContext = Pick<
55
Settings,
6-
'bin' | 'config' | 'directory' | 'silent' | 'output'
7-
> & {
8-
project?: string;
9-
};
6+
'bin' | 'config' | 'directory' | 'silent'
7+
>;
108

119
export function createCommandContext(
1210
settings: Settings,
1311
project: ProjectConfig | null | undefined,
1412
): CommandContext {
1513
return {
16-
project: project?.name,
1714
bin: project?.bin ?? settings.bin,
1815
directory: project?.directory ?? settings.directory,
1916
config: settings.config,
2017
silent: settings.silent,
21-
output: settings.output.replaceAll('{project}', project?.name ?? ''),
2218
};
2319
}
Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DEFAULT_SETTINGS } from '../constants.js';
21
import { type CommandContext, createCommandContext } from './context.js';
32

43
describe('createCommandContext', () => {
@@ -13,6 +12,7 @@ describe('createCommandContext', () => {
1312
directory: '/test',
1413
logger: console,
1514
monorepo: false,
15+
nxProjectsFilter: '--with-target={task}',
1616
output: '.code-pushup',
1717
projects: null,
1818
silent: false,
@@ -21,12 +21,10 @@ describe('createCommandContext', () => {
2121
null,
2222
),
2323
).toStrictEqual<CommandContext>({
24-
project: undefined,
2524
bin: 'npx --no-install code-pushup',
2625
directory: '/test',
2726
config: null,
2827
silent: false,
29-
output: '.code-pushup',
3028
});
3129
});
3230

@@ -41,6 +39,7 @@ describe('createCommandContext', () => {
4139
directory: '/test',
4240
logger: console,
4341
monorepo: false,
42+
nxProjectsFilter: '--with-target={task}',
4443
output: '.code-pushup',
4544
projects: null,
4645
silent: false,
@@ -53,49 +52,10 @@ describe('createCommandContext', () => {
5352
},
5453
),
5554
).toStrictEqual<CommandContext>({
56-
project: 'ui',
5755
bin: 'yarn code-pushup',
5856
directory: '/test/ui',
5957
config: null,
6058
silent: false,
61-
output: '.code-pushup',
6259
});
6360
});
64-
65-
it('should interpolate project name in output path for monorepo project', () => {
66-
expect(
67-
createCommandContext(
68-
{
69-
...DEFAULT_SETTINGS,
70-
output: '.code-pushup/{project}',
71-
},
72-
{
73-
name: 'website',
74-
bin: 'npx nx run website:code-pushup --',
75-
},
76-
),
77-
).toEqual(
78-
expect.objectContaining<Partial<CommandContext>>({
79-
project: 'website',
80-
bin: 'npx nx run website:code-pushup --',
81-
output: '.code-pushup/website',
82-
}),
83-
);
84-
});
85-
86-
it('should omit {project} placeholder in output path when in standalone mode', () => {
87-
expect(
88-
createCommandContext(
89-
{
90-
...DEFAULT_SETTINGS,
91-
output: '.code-pushup/{project}',
92-
},
93-
undefined,
94-
),
95-
).toEqual(
96-
expect.objectContaining<Partial<CommandContext>>({
97-
output: '.code-pushup/',
98-
}),
99-
);
100-
});
10161
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export { runCompare } from './commands/compare.js';
33
export { runMergeDiffs } from './commands/merge-diffs.js';
44
export { runPrintConfig } from './commands/print-config.js';
55
export { createCommandContext, type CommandContext } from './context.js';
6-
export { findPersistedFiles, type PersistedCliFiles } from './persist.js';
6+
export { persistedFilesFromConfig } from './persist.js';

0 commit comments

Comments
 (0)