Skip to content

Commit 1976457

Browse files
committed
feat(cli): drop print-config to stdout, output arg is now required
1 parent 928ec3f commit 1976457

File tree

7 files changed

+37
-81
lines changed

7 files changed

+37
-81
lines changed

e2e/cli-e2e/tests/print-config.e2e.test.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ describe('CLI print-config', () => {
4040
it.each(extensions)(
4141
'should load .%s config file with correct arguments',
4242
async ext => {
43-
const { code, stdout } = await executeProcess({
43+
const { code } = await executeProcess({
4444
command: 'npx',
4545
args: [
4646
'@code-pushup/cli',
4747
'print-config',
48+
'--output=config.json',
4849
`--config=${configFilePath(ext)}`,
4950
'--tsconfig=tsconfig.base.json',
5051
'--persist.outputDir=output-dir',
@@ -56,7 +57,11 @@ describe('CLI print-config', () => {
5657

5758
expect(code).toBe(0);
5859

59-
expect(JSON.parse(stdout)).toEqual(
60+
const output = await readFile(
61+
path.join(testFileDummySetup, 'config.json'),
62+
'utf8',
63+
);
64+
expect(JSON.parse(output)).toEqual(
6065
expect.objectContaining({
6166
config: expect.stringContaining(`code-pushup.config.${ext}`),
6267
tsconfig: 'tsconfig.base.json',
@@ -71,30 +76,4 @@ describe('CLI print-config', () => {
7176
);
7277
},
7378
);
74-
75-
it('should print config to output file', async () => {
76-
const { code, stdout } = await executeProcess({
77-
command: 'npx',
78-
args: ['@code-pushup/cli', 'print-config', '--output=config.json'],
79-
cwd: testFileDummySetup,
80-
});
81-
82-
expect(code).toBe(0);
83-
84-
const output = await readFile(
85-
path.join(testFileDummySetup, 'config.json'),
86-
'utf8',
87-
);
88-
expect(JSON.parse(output)).toEqual(
89-
expect.objectContaining({
90-
plugins: [
91-
expect.objectContaining({
92-
slug: 'dummy-plugin',
93-
title: 'Dummy Plugin',
94-
}),
95-
],
96-
}),
97-
);
98-
expect(stdout).not.toContain('dummy-plugin');
99-
});
10079
});

packages/cli/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ Print the resolved configuration.
331331

332332
In addition to the [Common Command Options](#common-command-options), the following options are recognized by the `print-config` command:
333333

334-
| Option | Required | Type | Description |
335-
| -------------- | :------: | -------- | -------------------------------------------------------- |
336-
| **`--output`** | no | `string` | Path to output file to print config (default is stdout). |
334+
| Option | Required | Type | Description |
335+
| -------------- | :------: | -------- | ------------------------------------ |
336+
| **`--output`** | yes | `string` | Path to output file to print config. |
337337

338338
#### `merge-diffs` command
339339

packages/cli/src/lib/implementation/log-intro.middleware.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import { getVersion } from './version.js';
77
export function logIntroMiddleware(
88
args: ArgumentsCamelCase,
99
): ArgumentsCamelCase {
10-
// prevent interference with JSON output
11-
if (args._.includes('print-config') && !('output' in args)) {
12-
return args;
13-
}
1410
logger.info(
1511
ansis.bold.blue(
1612
`${CODE_PUSHUP_UNICODE_LOGO} ${CLI_DISPLAY_NAME} v${getVersion()}`,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export type PrintConfigOptions = {
2-
output?: string;
2+
output: string;
33
};

packages/cli/src/lib/implementation/print-config.options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ export function yargsPrintConfigOptionsDefinition(): Record<
77
> {
88
return {
99
output: {
10-
describe: 'Output file path to use instead of stdout',
10+
describe: 'Output file path for resolved JSON config',
1111
type: 'string',
12+
demandOption: true,
1213
},
1314
};
1415
}

packages/cli/src/lib/print-config/print-config-command.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,19 @@ export function yargsPrintConfigCommandObject() {
1515
describe: 'Print config',
1616
builder: yargsPrintConfigOptionsDefinition(),
1717
handler: async yargsArgs => {
18+
printCliCommand(command);
19+
1820
// it is important to filter out kebab case keys
1921
// because yargs duplicates options in camel case and kebab case
2022
const { _, $0, ...args } = filterKebabCaseKeys(yargsArgs);
2123
const { output, ...config } = args as PrintConfigOptions &
2224
Record<string, unknown>;
2325

24-
// stdout should be valid JSON
25-
if (output) {
26-
printCliCommand(command);
27-
}
28-
2926
const content = JSON.stringify(config, null, 2);
3027

31-
if (output) {
32-
await mkdir(path.dirname(output), { recursive: true });
33-
await writeFile(output, content);
34-
logger.info(`Config printed to file ${ansis.bold(output)}`);
35-
} else {
36-
logger.info(content);
37-
}
28+
await mkdir(path.dirname(output), { recursive: true });
29+
await writeFile(output, content);
30+
logger.info(`Config printed to file ${ansis.bold(output)}`);
3831
},
3932
} satisfies CommandModule;
4033
}

packages/cli/src/lib/print-config/print-config-command.unit.test.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ vi.mock('@code-pushup/core', async () => {
1919
});
2020

2121
describe('print-config-command', () => {
22-
it('should log config to stdout by default', async () => {
23-
await yargsCli(['print-config'], {
24-
...DEFAULT_CLI_CONFIGURATION,
25-
commands: [yargsPrintConfigCommandObject()],
26-
}).parseAsync();
27-
28-
expect(logger.info).toHaveBeenCalledWith(
29-
expect.stringContaining('"plugins": ['),
30-
);
31-
});
32-
3322
it('should write config to file if output option is given', async () => {
3423
const outputPath = path.join(MEMFS_VOLUME, 'config.json');
3524
await yargsCli(['print-config', `--output=${outputPath}`], {
@@ -40,32 +29,30 @@ describe('print-config-command', () => {
4029
await expect(readFile(outputPath, 'utf8')).resolves.toContain(
4130
'"plugins": [',
4231
);
43-
expect(logger.info).not.toHaveBeenCalledWith(
44-
expect.stringContaining('"plugins": ['),
45-
);
4632
expect(logger.info).toHaveBeenCalledWith(
4733
`Config printed to file ${ansis.bold(outputPath)}`,
4834
);
4935
});
5036

5137
it('should filter out meta arguments and kebab duplicates', async () => {
52-
await yargsCli(['print-config', '--persist.outputDir=destinationDir'], {
53-
...DEFAULT_CLI_CONFIGURATION,
54-
commands: [yargsPrintConfigCommandObject()],
55-
}).parseAsync();
56-
57-
expect(logger.info).not.toHaveBeenCalledWith(
58-
expect.stringContaining('"$0":'),
59-
);
60-
expect(logger.info).not.toHaveBeenCalledWith(
61-
expect.stringContaining('"_":'),
62-
);
63-
64-
expect(logger.info).toHaveBeenCalledWith(
65-
expect.stringContaining('"outputDir": "destinationDir"'),
66-
);
67-
expect(logger.info).not.toHaveBeenCalledWith(
68-
expect.stringContaining('"output-dir":'),
69-
);
38+
const outputPath = path.join(MEMFS_VOLUME, 'config.json');
39+
await yargsCli(
40+
[
41+
'print-config',
42+
`--output=${outputPath}`,
43+
'--persist.outputDir=destinationDir',
44+
],
45+
{
46+
...DEFAULT_CLI_CONFIGURATION,
47+
commands: [yargsPrintConfigCommandObject()],
48+
},
49+
).parseAsync();
50+
const output = await readFile(outputPath, 'utf8');
51+
52+
expect(output).not.toContain('"$0":');
53+
expect(output).not.toContain('"_":');
54+
55+
expect(output).toContain('"outputDir": "destinationDir"');
56+
expect(output).not.toContain('"output-dir":');
7057
});
7158
});

0 commit comments

Comments
 (0)