Skip to content

Commit f2edba0

Browse files
Mac Miniclaude
authored andcommitted
feat(add-generate-api-target): add support for custom target name
Allow users to provide a custom target name when running the add-generate-api-target generator. If a build target exists, automatically add the custom target to its dependsOn array. Closes #38 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 60b6cb3 commit f2edba0

4 files changed

Lines changed: 135 additions & 1 deletion

File tree

packages/nx-plugin-openapi/src/generators/add-generate-api-target/generator.spec.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
logger,
66
readProjectConfiguration,
77
Tree,
8+
updateProjectConfiguration,
89
} from '@nx/devkit';
910
import { addGenerateApiGenerator } from './generator';
1011
import { AddGenerateApiSchema } from './schema';
@@ -206,4 +207,108 @@ describe('add-generate-api-target generator', () => {
206207
'[@lambda-solutions/nx-plugin-openapi] ✨ Successfully added generate-api target to test-app project'
207208
);
208209
});
210+
211+
describe('custom target name', () => {
212+
it('should use custom target name when provided', async () => {
213+
const options: AddGenerateApiSchema = {
214+
project: 'test-app',
215+
inputSpec: 'swagger.json',
216+
outputPath: 'libs/api',
217+
targetName: 'generate-client',
218+
};
219+
220+
await addGenerateApiGenerator(tree, options);
221+
222+
const projectConfig = readProjectConfiguration(tree, 'test-app');
223+
const target = projectConfig.targets['generate-client'];
224+
225+
expect(target).toBeDefined();
226+
expect(projectConfig.targets['generate-api']).toBeUndefined();
227+
expect(target.executor).toBe(
228+
'@lambda-solutions/nx-plugin-openapi:generate-api'
229+
);
230+
});
231+
232+
it('should add custom target to build dependsOn', async () => {
233+
// Add build target
234+
const projectConfig = readProjectConfiguration(tree, 'test-app');
235+
projectConfig.targets['build'] = {
236+
executor: '@nx/webpack:build',
237+
options: {},
238+
};
239+
updateProjectConfiguration(tree, 'test-app', projectConfig);
240+
241+
const options: AddGenerateApiSchema = {
242+
project: 'test-app',
243+
inputSpec: 'swagger.json',
244+
outputPath: 'libs/api',
245+
targetName: 'generate-client',
246+
};
247+
248+
await addGenerateApiGenerator(tree, options);
249+
250+
const updatedConfig = readProjectConfiguration(tree, 'test-app');
251+
expect(updatedConfig.targets['build'].dependsOn).toContain('generate-client');
252+
});
253+
254+
it('should add default target name to build dependsOn when no custom name', async () => {
255+
// Add build target
256+
const projectConfig = readProjectConfiguration(tree, 'test-app');
257+
projectConfig.targets['build'] = {
258+
executor: '@nx/webpack:build',
259+
options: {},
260+
};
261+
updateProjectConfiguration(tree, 'test-app', projectConfig);
262+
263+
const options: AddGenerateApiSchema = {
264+
project: 'test-app',
265+
inputSpec: 'swagger.json',
266+
outputPath: 'libs/api',
267+
};
268+
269+
await addGenerateApiGenerator(tree, options);
270+
271+
const updatedConfig = readProjectConfiguration(tree, 'test-app');
272+
expect(updatedConfig.targets['build'].dependsOn).toContain('generate-api');
273+
});
274+
275+
it('should not duplicate target in build dependsOn', async () => {
276+
// Add build target with existing dependsOn
277+
const projectConfig = readProjectConfiguration(tree, 'test-app');
278+
projectConfig.targets['build'] = {
279+
executor: '@nx/webpack:build',
280+
options: {},
281+
dependsOn: ['generate-client'],
282+
};
283+
updateProjectConfiguration(tree, 'test-app', projectConfig);
284+
285+
const options: AddGenerateApiSchema = {
286+
project: 'test-app',
287+
inputSpec: 'swagger.json',
288+
outputPath: 'libs/api',
289+
targetName: 'generate-client',
290+
};
291+
292+
await addGenerateApiGenerator(tree, options);
293+
294+
const updatedConfig = readProjectConfiguration(tree, 'test-app');
295+
expect(updatedConfig.targets['build'].dependsOn).toEqual(['generate-client']);
296+
expect(updatedConfig.targets['build'].dependsOn.length).toBe(1);
297+
});
298+
299+
it('should log correct success message with custom target name', async () => {
300+
const options: AddGenerateApiSchema = {
301+
project: 'test-app',
302+
inputSpec: 'swagger.json',
303+
outputPath: 'libs/api',
304+
targetName: 'my-custom-api',
305+
};
306+
307+
await addGenerateApiGenerator(tree, options);
308+
309+
expect(mockedLogger.info).toHaveBeenCalledWith(
310+
'[@lambda-solutions/nx-plugin-openapi] ✨ Successfully added my-custom-api target to test-app project'
311+
);
312+
});
313+
});
209314
});

packages/nx-plugin-openapi/src/generators/add-generate-api-target/generator.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function addGenerateApiGenerator(
1515
tree: Tree,
1616
options: AddGenerateApiSchema
1717
) {
18-
const targetName = 'generate-api';
18+
const targetName = options.targetName || 'generate-api';
1919

2020
// Read the project configuration
2121
const projectConfig = readProjectConfiguration(tree, options.project);
@@ -33,6 +33,9 @@ export async function addGenerateApiGenerator(
3333
// Add the new target
3434
addTarget({ projectConfig, targetName, options });
3535

36+
// Update build target's dependsOn if it exists
37+
updateBuildTargetDependsOn({ projectConfig, targetName });
38+
3639
// Update the project configuration
3740
updateProjectConfiguration(tree, options.project, projectConfig);
3841

@@ -78,3 +81,22 @@ function addTarget(args: {
7881
},
7982
};
8083
}
84+
85+
function updateBuildTargetDependsOn(args: {
86+
projectConfig: ProjectConfiguration;
87+
targetName: string;
88+
}) {
89+
const { projectConfig, targetName } = args;
90+
const buildTarget = projectConfig.targets?.['build'];
91+
92+
if (buildTarget) {
93+
if (!buildTarget.dependsOn) {
94+
buildTarget.dependsOn = [];
95+
}
96+
97+
// Check if the target is already in dependsOn
98+
if (!buildTarget.dependsOn.includes(targetName)) {
99+
buildTarget.dependsOn.push(targetName);
100+
}
101+
}
102+
}

packages/nx-plugin-openapi/src/generators/add-generate-api-target/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface AddGenerateApiSchema {
66
configFile?: string;
77
skipValidateSpec?: boolean;
88
addToGitignore?: boolean;
9+
targetName?: string;
910
}

packages/nx-plugin-openapi/src/generators/add-generate-api-target/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
"description": "Whether to add the output path to .gitignore",
3737
"default": true,
3838
"x-prompt": "Would you like to add the output path to .gitignore?"
39+
},
40+
"targetName": {
41+
"type": "string",
42+
"description": "Custom name for the generate-api target",
43+
"default": "generate-api",
44+
"x-prompt": "What name would you like to use for the target? (default: generate-api)"
3945
}
4046
},
4147
"required": ["project", "inputSpec", "outputPath"]

0 commit comments

Comments
 (0)