Skip to content

Commit e74c992

Browse files
committed
refactor: remove target generator in favor of task inference
1 parent c7d314e commit e74c992

File tree

6 files changed

+7
-121
lines changed

6 files changed

+7
-121
lines changed

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

Lines changed: 1 addition & 67 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;

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 --skipTarget --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 --skipTarget --project="@org/empty-root"`,
7070
},
7171
},
7272
},

packages/nx-plugin/src/plugin/target/configuration-target.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@ import type { TargetConfiguration } from '@nx/devkit';
22
import type { RunCommandsOptions } from 'nx/src/executors/run-commands/run-commands.impl';
33
import { objectToCliArgs } from '../../executors/internal/cli.js';
44
import { PACKAGE_NAME } from '../../internal/constants.js';
5-
import { CP_TARGET_NAME } from '../constants.js';
65

76
export function createConfigurationTarget(options?: {
8-
targetName?: string;
97
projectName?: string;
108
bin?: string;
119
}): TargetConfiguration<RunCommandsOptions> {
12-
const {
13-
projectName,
14-
bin = PACKAGE_NAME,
15-
targetName = CP_TARGET_NAME,
16-
} = options ?? {};
10+
const { projectName, bin = PACKAGE_NAME } = options ?? {};
1711
return {
1812
command: `nx g ${bin}:configuration ${objectToCliArgs({
1913
skipTarget: true,
20-
targetName,
2114
...(projectName ? { project: projectName } : {}),
2215
}).join(' ')}`,
2316
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ describe('createConfigurationTarget', () => {
77
expect(
88
createConfigurationTarget({ projectName: 'my-project' }),
99
).toStrictEqual({
10-
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="my-project"`,
10+
command: `nx g ${PACKAGE_NAME}:configuration --project="my-project"`,
1111
});
1212
});
1313

1414
it('should return code-pushup--configuration target without project name', () => {
1515
expect(createConfigurationTarget()).toStrictEqual({
16-
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup"`,
16+
command: `nx g ${PACKAGE_NAME}:configuration`,
1717
});
1818
});
1919
});

0 commit comments

Comments
 (0)