Skip to content

Commit d5ca550

Browse files
authored
refactor: remove target generator in favour of task inference (#1110)
1 parent 33714e2 commit d5ca550

File tree

11 files changed

+17
-253
lines changed

11 files changed

+17
-253
lines changed

e2e/nx-plugin-e2e/tests/__snapshots__/plugin-create-nodes.e2e.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports[`nx-plugin > should add configuration target dynamically 1`] = `
1717
"configurations": {},
1818
"executor": "nx:run-commands",
1919
"options": {
20-
"command": "nx g @code-pushup/nx-plugin:configuration --skipTarget --targetName="code-pushup" --project="my-lib"",
20+
"command": "nx g @code-pushup/nx-plugin:configuration --project="my-lib"",
2121
},
2222
"parallelism": true,
2323
},

e2e/nx-plugin-e2e/tests/generator-configuration.e2e.test.ts

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('nx-plugin g configuration', () => {
3535
await teardownTestFolder(testFileDir);
3636
});
3737

38-
it('should generate code-pushup.config.ts file and add target to project.json', async () => {
38+
it('should generate code-pushup.config.ts file', async () => {
3939
const cwd = path.join(testFileDir, 'configure');
4040
await materializeTree(tree, cwd);
4141

@@ -64,22 +64,7 @@ describe('nx-plugin g configuration', () => {
6464
'NX Generating @code-pushup/nx-plugin:configuration',
6565
);
6666
expect(cleanedStdout).toMatch(/^CREATE.*code-pushup.config.ts/m);
67-
expect(cleanedStdout).toMatch(/^UPDATE.*project.json/m);
6867

69-
const projectJson = await readFile(
70-
path.join(cwd, 'libs', project, 'project.json'),
71-
'utf8',
72-
);
73-
74-
expect(JSON.parse(projectJson)).toStrictEqual(
75-
expect.objectContaining({
76-
targets: expect.objectContaining({
77-
'code-pushup': {
78-
executor: '@code-pushup/nx-plugin:cli',
79-
},
80-
}),
81-
}),
82-
);
8368
await expect(
8469
readFile(
8570
path.join(cwd, 'libs', project, 'code-pushup.config.ts'),
@@ -111,21 +96,6 @@ describe('nx-plugin g configuration', () => {
11196
'NX Generating @code-pushup/nx-plugin:configuration',
11297
);
11398
expect(cleanedStdout).not.toMatch(/^CREATE.*code-pushup.config.ts/m);
114-
expect(cleanedStdout).toMatch(/^UPDATE.*project.json/m);
115-
116-
const projectJson = await readFile(
117-
path.join(cwd, 'libs', project, 'project.json'),
118-
'utf8',
119-
);
120-
expect(JSON.parse(projectJson)).toStrictEqual(
121-
expect.objectContaining({
122-
targets: expect.objectContaining({
123-
'code-pushup': {
124-
executor: '@code-pushup/nx-plugin:cli',
125-
},
126-
}),
127-
}),
128-
);
12999
});
130100

131101
it('should NOT create a code-pushup.config.ts file if skipConfig is given', async () => {
@@ -152,21 +122,6 @@ describe('nx-plugin g configuration', () => {
152122
'NX Generating @code-pushup/nx-plugin:configuration',
153123
);
154124
expect(cleanedStdout).not.toMatch(/^CREATE.*code-pushup.config.ts/m);
155-
expect(cleanedStdout).toMatch(/^UPDATE.*project.json/m);
156-
157-
const projectJson = await readFile(
158-
path.join(cwd, 'libs', project, 'project.json'),
159-
'utf8',
160-
);
161-
expect(JSON.parse(projectJson)).toStrictEqual(
162-
expect.objectContaining({
163-
targets: expect.objectContaining({
164-
'code-pushup': {
165-
executor: '@code-pushup/nx-plugin:cli',
166-
},
167-
}),
168-
}),
169-
);
170125

171126
await expect(
172127
readFile(
@@ -176,53 +131,6 @@ describe('nx-plugin g configuration', () => {
176131
).rejects.toThrow('no such file or directory');
177132
});
178133

179-
it('should NOT add target to project.json if skipTarget is given', async () => {
180-
const cwd = path.join(testFileDir, 'configure-skip-target');
181-
await materializeTree(tree, cwd);
182-
183-
const { code, stdout } = await executeProcess({
184-
command: 'npx',
185-
args: [
186-
'nx',
187-
'g',
188-
'@code-pushup/nx-plugin:configuration',
189-
project,
190-
'--skipTarget',
191-
],
192-
cwd,
193-
});
194-
expect(code).toBe(0);
195-
196-
const cleanedStdout = removeColorCodes(stdout);
197-
198-
expect(cleanedStdout).toContain(
199-
'NX Generating @code-pushup/nx-plugin:configuration',
200-
);
201-
expect(cleanedStdout).toMatch(/^CREATE.*code-pushup.config.ts/m);
202-
expect(cleanedStdout).not.toMatch(/^UPDATE.*project.json/m);
203-
204-
const projectJson = await readFile(
205-
path.join(cwd, 'libs', project, 'project.json'),
206-
'utf8',
207-
);
208-
expect(JSON.parse(projectJson)).toStrictEqual(
209-
expect.objectContaining({
210-
targets: expect.not.objectContaining({
211-
'code-pushup': {
212-
executor: '@code-pushup/nx-plugin:cli',
213-
},
214-
}),
215-
}),
216-
);
217-
218-
await expect(
219-
readFile(
220-
path.join(cwd, 'libs', project, 'code-pushup.config.ts'),
221-
'utf8',
222-
),
223-
).resolves.toStrictEqual(expect.any(String));
224-
});
225-
226134
it('should inform about dry run', async () => {
227135
const cwd = path.join(testFileDir, 'configure');
228136
await materializeTree(tree, cwd);

e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('nx-plugin', () => {
5151
configurations: {},
5252
executor: 'nx:run-commands',
5353
options: {
54-
command: `nx g @code-pushup/nx-plugin:configuration --skipTarget --targetName="code-pushup" --project="${project}"`,
54+
command: `nx g @code-pushup/nx-plugin:configuration --project="${project}"`,
5555
},
5656
parallelism: true,
5757
},
@@ -120,7 +120,7 @@ describe('nx-plugin', () => {
120120
expect(projectJson.targets).toStrictEqual({
121121
'code-pushup--configuration': expect.objectContaining({
122122
options: {
123-
command: `nx g XYZ:configuration --skipTarget --targetName="code-pushup" --project="${project}"`,
123+
command: `nx g XYZ:configuration --project="${project}"`,
124124
},
125125
}),
126126
});

packages/nx-plugin/src/generators/configuration/generator.int.test.ts

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,7 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
88
import * as path from 'node:path';
99
import { afterEach, describe, expect, it, vi } from 'vitest';
1010
import { DEFAULT_TARGET_NAME, PACKAGE_NAME } from '../../internal/constants.js';
11-
import { addTargetToProject, configurationGenerator } from './generator.js';
12-
13-
describe('addTargetToProject', () => {
14-
let tree: Tree;
15-
const testProjectName = 'test-app';
16-
17-
beforeEach(() => {
18-
tree = createTreeWithEmptyWorkspace();
19-
addProjectConfiguration(tree, 'test-app', {
20-
root: 'test-app',
21-
});
22-
});
23-
24-
afterEach(() => {
25-
//reset tree
26-
tree.delete(testProjectName);
27-
});
28-
29-
it('should generate a project target', () => {
30-
addTargetToProject(
31-
tree,
32-
{
33-
root: testProjectName,
34-
projectType: 'library',
35-
sourceRoot: `${testProjectName}/src`,
36-
targets: {},
37-
},
38-
{
39-
project: testProjectName,
40-
},
41-
);
42-
43-
const projectConfiguration = readProjectConfiguration(
44-
tree,
45-
testProjectName,
46-
);
47-
48-
expect(projectConfiguration.targets?.[DEFAULT_TARGET_NAME]).toEqual({
49-
executor: `${PACKAGE_NAME}:cli`,
50-
});
51-
});
52-
53-
it('should use targetName to generate a project target', () => {
54-
addTargetToProject(
55-
tree,
56-
{
57-
root: testProjectName,
58-
projectType: 'library',
59-
sourceRoot: `${testProjectName}/src`,
60-
targets: {},
61-
},
62-
{
63-
project: testProjectName,
64-
targetName: 'cp',
65-
},
66-
);
67-
68-
const projectConfiguration = readProjectConfiguration(
69-
tree,
70-
testProjectName,
71-
);
72-
73-
expect(projectConfiguration.targets?.['cp']).toEqual({
74-
executor: `${PACKAGE_NAME}:cli`,
75-
});
76-
});
77-
});
11+
import { configurationGenerator } from './generator.js';
7812

7913
describe('configurationGenerator', () => {
8014
let tree: Tree;
@@ -92,21 +26,6 @@ describe('configurationGenerator', () => {
9226
tree.delete(testProjectName);
9327
});
9428

95-
it('should generate a project target and config file', async () => {
96-
await configurationGenerator(tree, {
97-
project: testProjectName,
98-
});
99-
100-
const projectConfiguration = readProjectConfiguration(
101-
tree,
102-
testProjectName,
103-
);
104-
105-
expect(projectConfiguration.targets?.[DEFAULT_TARGET_NAME]).toEqual({
106-
executor: `${PACKAGE_NAME}:cli`,
107-
});
108-
});
109-
11029
it('should skip config creation if skipConfig is used', async () => {
11130
await configurationGenerator(tree, {
11231
project: testProjectName,
@@ -121,20 +40,6 @@ describe('configurationGenerator', () => {
12140
expect(loggerInfoSpy).toHaveBeenCalledWith('Skip config file creation');
12241
});
12342

124-
it('should skip target creation if skipTarget is used', async () => {
125-
await configurationGenerator(tree, {
126-
project: testProjectName,
127-
skipTarget: true,
128-
});
129-
130-
const projectConfiguration = readProjectConfiguration(
131-
tree,
132-
testProjectName,
133-
);
134-
expect(projectConfiguration.targets).toBeUndefined();
135-
expect(loggerInfoSpy).toHaveBeenCalledWith('Skip adding target to project');
136-
});
137-
13843
it('should skip formatting', async () => {
13944
await configurationGenerator(tree, {
14045
project: testProjectName,

packages/nx-plugin/src/generators/configuration/generator.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import {
33
formatFiles,
44
logger,
55
readProjectConfiguration,
6-
updateProjectConfiguration,
76
} from '@nx/devkit';
8-
import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json';
9-
import { DEFAULT_TARGET_NAME, PACKAGE_NAME } from '../../internal/constants.js';
107
import { generateCodePushupConfig } from './code-pushup-config.js';
118
import type { ConfigurationGeneratorOptions } from './schema.js';
129

@@ -16,46 +13,19 @@ export async function configurationGenerator(
1613
) {
1714
const projectConfiguration = readProjectConfiguration(tree, options.project);
1815

19-
const { skipConfig, skipTarget, skipFormat } = options;
16+
const { skipConfig, skipFormat } = options;
2017

2118
if (skipConfig === true) {
2219
logger.info('Skip config file creation');
2320
} else {
2421
generateCodePushupConfig(tree, projectConfiguration.root);
2522
}
2623

27-
if (skipTarget === true) {
28-
logger.info('Skip adding target to project');
29-
} else {
30-
addTargetToProject(tree, projectConfiguration, options);
31-
}
32-
3324
if (skipFormat === true) {
3425
logger.info('Skip formatting files');
3526
} else {
3627
await formatFiles(tree);
3728
}
3829
}
3930

40-
export function addTargetToProject(
41-
tree: Tree,
42-
projectConfiguration: ProjectConfiguration,
43-
options: ConfigurationGeneratorOptions,
44-
) {
45-
const { targets } = projectConfiguration;
46-
const { targetName, project } = options;
47-
48-
const codePushupTargetConfig = {
49-
executor: `${PACKAGE_NAME}:cli`,
50-
};
51-
52-
updateProjectConfiguration(tree, project, {
53-
...projectConfiguration,
54-
targets: {
55-
...targets,
56-
[targetName ?? DEFAULT_TARGET_NAME]: codePushupTargetConfig,
57-
},
58-
});
59-
}
60-
6131
export default configurationGenerator;

packages/nx-plugin/src/generators/configuration/schema.json

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,10 @@
1515
"index": 0
1616
}
1717
},
18-
"targetName": {
19-
"type": "string",
20-
"description": "The name of the target.",
21-
"x-prompt": "Which name should the target get? default is code-pushup.",
22-
"default": "code-pushup"
23-
},
2418
"bin": {
2519
"type": "string",
2620
"description": "Path to Code PushUp CLI"
2721
},
28-
"skipTarget": {
29-
"type": "boolean",
30-
"description": "Skip adding the target to project.json.",
31-
"$default": "false"
32-
},
3322
"skipConfig": {
3423
"type": "boolean",
3524
"description": "Skip adding the code-pushup.config.ts to the project root.",

packages/nx-plugin/src/plugin/plugin.unit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('@code-pushup/nx-plugin/plugin', () => {
4040
[projectRoot]: {
4141
targets: {
4242
[`${CP_TARGET_NAME}--configuration`]: {
43-
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
43+
command: `nx g ${PACKAGE_NAME}:configuration --project="@org/empty-root"`,
4444
},
4545
},
4646
},
@@ -66,7 +66,7 @@ describe('@code-pushup/nx-plugin/plugin', () => {
6666
[projectRoot]: {
6767
targets: {
6868
[`${CP_TARGET_NAME}--configuration`]: {
69-
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
69+
command: `nx g ${PACKAGE_NAME}:configuration --project="@org/empty-root"`,
7070
},
7171
},
7272
},

0 commit comments

Comments
 (0)