Skip to content

Commit 59db660

Browse files
committed
refactor(create-cli): pass single input to generateConfig
1 parent 47eb97a commit 59db660

21 files changed

Lines changed: 353 additions & 202 deletions

File tree

packages/create-cli/src/lib/setup/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type {
55
CategoryCodegenConfig,
66
ImportDeclarationStructure,
77
PluginAnswer,
8+
PluginCodegenInput,
89
PluginCodegenResult,
910
PluginPromptDescriptor,
1011
PluginSetupBinding,

packages/create-cli/src/lib/setup/wizard.int.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const TEST_BINDINGS: PluginSetupBinding[] = [
2828
default: 'alpha.config.js',
2929
},
3030
],
31-
generateConfig(answers) {
31+
generateConfig({ answers }) {
3232
const configPath = answers['alpha.path'] ?? 'alpha.config.js';
3333
return {
3434
imports: [

packages/create-cli/src/lib/setup/wizard.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ async function resolveBinding(
9393
tree: Pick<Tree, 'read' | 'write'>,
9494
): Promise<PluginCodegenResult> {
9595
if (!binding.prompts) {
96-
return binding.generateConfig({}, tree);
96+
return binding.generateConfig({
97+
tree,
98+
targetDir,
99+
cliArgs,
100+
answers: {},
101+
});
97102
}
98103
logger.newline();
99104
logger.info(ansis.bold(binding.title));
@@ -102,7 +107,7 @@ async function resolveBinding(
102107
descriptors.length > 0
103108
? await promptPluginOptions(descriptors, cliArgs)
104109
: {};
105-
return binding.generateConfig(answers, tree);
110+
return binding.generateConfig({ tree, targetDir, cliArgs, answers });
106111
}
107112

108113
async function writeStandaloneConfig(

packages/models/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export type {
116116
CategoryCodegenConfig,
117117
ImportDeclarationStructure,
118118
PluginAnswer,
119+
PluginCodegenInput,
119120
PluginCodegenResult,
120121
PluginDeclarationStructure,
121122
PluginPromptDescriptor,

packages/models/src/lib/plugin-setup.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ export type PluginSetupTree = {
7272
write: (path: string, content: string) => Promise<void>;
7373
};
7474

75+
export type PluginCodegenInput = {
76+
answers: Record<string, PluginAnswer>;
77+
tree: PluginSetupTree;
78+
targetDir: string;
79+
cliArgs: Record<string, unknown>;
80+
};
81+
7582
/**
7683
* Defines how a plugin integrates with the setup wizard.
7784
*
@@ -88,7 +95,6 @@ export type PluginSetupBinding = {
8895
prompts?: (targetDir: string) => Promise<PluginPromptDescriptor[]>;
8996
isRecommended?: (targetDir: string) => Promise<boolean>;
9097
generateConfig: (
91-
answers: Record<string, PluginAnswer>,
92-
tree: PluginSetupTree,
98+
input: PluginCodegenInput,
9399
) => PluginCodegenResult | Promise<PluginCodegenResult>;
94100
};

packages/plugin-axe/src/lib/binding.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
CategoryCodegenConfig,
44
PluginAnswer,
55
PluginSetupBinding,
6-
PluginSetupTree,
76
} from '@code-pushup/models';
87
import {
98
answerBoolean,
@@ -84,10 +83,7 @@ export const axeSetupBinding = {
8483
default: true,
8584
},
8685
],
87-
generateConfig: async (
88-
answers: Record<string, PluginAnswer>,
89-
tree: PluginSetupTree,
90-
) => {
86+
generateConfig: async ({ answers, tree }) => {
9187
const options = parseAnswers(answers);
9288
if (options.setupScript) {
9389
await tree.write(SETUP_SCRIPT_PATH, SETUP_SCRIPT_CONTENT);

packages/plugin-axe/src/lib/binding.unit.test.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { PluginAnswer } from '@code-pushup/models';
2-
import { createMockTree } from '@code-pushup/test-utils';
2+
import {
3+
createMockCodegenInput,
4+
createMockTree,
5+
} from '@code-pushup/test-utils';
36
import { axeSetupBinding as binding } from './binding.js';
47

58
const defaultAnswers: Record<string, PluginAnswer> = {
@@ -32,8 +35,7 @@ describe('axeSetupBinding', () => {
3235
describe('generateConfig with categories selected', () => {
3336
it('should declare plugin as a variable for use in category refs', async () => {
3437
const { pluginDeclaration } = await binding.generateConfig(
35-
defaultAnswers,
36-
createMockTree(),
38+
createMockCodegenInput(defaultAnswers, createMockTree()),
3739
);
3840
expect(pluginDeclaration).toStrictEqual({
3941
identifier: 'axe',
@@ -43,8 +45,7 @@ describe('axeSetupBinding', () => {
4345

4446
it('should import axeGroupRefs helper', async () => {
4547
const { imports } = await binding.generateConfig(
46-
defaultAnswers,
47-
createMockTree(),
48+
createMockCodegenInput(defaultAnswers, createMockTree()),
4849
);
4950
expect(imports).toStrictEqual([
5051
expect.objectContaining({ namedImports: ['axeGroupRefs'] }),
@@ -53,8 +54,7 @@ describe('axeSetupBinding', () => {
5354

5455
it('should produce accessibility category with refs expression', async () => {
5556
const { categories } = await binding.generateConfig(
56-
defaultAnswers,
57-
createMockTree(),
57+
createMockCodegenInput(defaultAnswers, createMockTree()),
5858
);
5959
expect(categories).toStrictEqual([
6060
expect.objectContaining({
@@ -68,24 +68,21 @@ describe('axeSetupBinding', () => {
6868
describe('generateConfig without categories selected', () => {
6969
it('should not declare plugin as a variable', async () => {
7070
const { pluginDeclaration } = await binding.generateConfig(
71-
noCategoryAnswers,
72-
createMockTree(),
71+
createMockCodegenInput(noCategoryAnswers, createMockTree()),
7372
);
7473
expect(pluginDeclaration).toBeUndefined();
7574
});
7675

7776
it('should not import axeGroupRefs helper', async () => {
7877
const { imports } = await binding.generateConfig(
79-
noCategoryAnswers,
80-
createMockTree(),
78+
createMockCodegenInput(noCategoryAnswers, createMockTree()),
8179
);
8280
expect(imports[0]).not.toHaveProperty('namedImports');
8381
});
8482

8583
it('should not produce categories', async () => {
8684
const { categories } = await binding.generateConfig(
87-
noCategoryAnswers,
88-
createMockTree(),
85+
createMockCodegenInput(noCategoryAnswers, createMockTree()),
8986
);
9087
expect(categories).toBeUndefined();
9188
});
@@ -95,8 +92,10 @@ describe('axeSetupBinding', () => {
9592
it('should write setup script file when confirmed', async () => {
9693
const tree = createMockTree();
9794
await binding.generateConfig(
98-
{ ...defaultAnswers, 'axe.setupScript': true },
99-
tree,
95+
createMockCodegenInput(
96+
{ ...defaultAnswers, 'axe.setupScript': true },
97+
tree,
98+
),
10099
);
101100
expect(tree.written.get('./axe-setup.ts')).toContain(
102101
"import type { Page } from 'playwright-core'",
@@ -105,8 +104,10 @@ describe('axeSetupBinding', () => {
105104

106105
it('should include setupScript in plugin call when confirmed', async () => {
107106
const { pluginDeclaration } = await binding.generateConfig(
108-
{ ...defaultAnswers, 'axe.setupScript': true },
109-
createMockTree(),
107+
createMockCodegenInput(
108+
{ ...defaultAnswers, 'axe.setupScript': true },
109+
createMockTree(),
110+
),
110111
);
111112
expect(pluginDeclaration!.expression).toContain(
112113
"setupScript: './axe-setup.ts'",
@@ -115,26 +116,32 @@ describe('axeSetupBinding', () => {
115116

116117
it('should not write setup script file when declined', async () => {
117118
const tree = createMockTree();
118-
await binding.generateConfig(defaultAnswers, tree);
119+
await binding.generateConfig(
120+
createMockCodegenInput(defaultAnswers, tree),
121+
);
119122
expect(tree.written.size).toBe(0);
120123
});
121124
});
122125

123126
it('should include non-default preset in plugin call', async () => {
124127
const { pluginDeclaration } = await binding.generateConfig(
125-
{ ...defaultAnswers, 'axe.preset': 'wcag22aa' },
126-
createMockTree(),
128+
createMockCodegenInput(
129+
{ ...defaultAnswers, 'axe.preset': 'wcag22aa' },
130+
createMockTree(),
131+
),
127132
);
128133
expect(pluginDeclaration!.expression).toContain("preset: 'wcag22aa'");
129134
});
130135

131136
it('should format multiple URLs as array', async () => {
132137
const { pluginDeclaration } = await binding.generateConfig(
133-
{
134-
...defaultAnswers,
135-
'axe.urls': 'http://localhost:4200/login, http://localhost:4200/home',
136-
},
137-
createMockTree(),
138+
createMockCodegenInput(
139+
{
140+
...defaultAnswers,
141+
'axe.urls': 'http://localhost:4200/login, http://localhost:4200/home',
142+
},
143+
createMockTree(),
144+
),
138145
);
139146
expect(pluginDeclaration!.expression).toContain(
140147
"axePlugin(['http://localhost:4200/login', 'http://localhost:4200/home']",

packages/plugin-coverage/src/lib/binding.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ export const coverageSetupBinding = {
128128
},
129129
];
130130
},
131-
generateConfig: async (
132-
answers: Record<string, PluginAnswer>,
133-
tree?: PluginSetupTree,
134-
) => {
131+
generateConfig: async ({ answers, tree }) => {
135132
const options = parseAnswers(answers);
136133
const lcovConfigured = await configureLcovReporter(options, tree);
137134
return {

0 commit comments

Comments
 (0)