Skip to content

Commit cad7630

Browse files
committed
add validate option logic
1 parent 0eeb924 commit cad7630

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

packages/aws-cdk/lib/commands/init/init.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export interface CliInitOptions {
9191
* Initialize a CDK package in the current directory
9292
*/
9393
export async function cliInit(options: CliInitOptions) {
94+
await ensureValidCliInitOptions(options, options.ioHelper);
95+
9496
const ioHelper = options.ioHelper;
9597
const canUseNetwork = options.canUseNetwork ?? true;
9698
const generateOnly = options.generateOnly ?? false;
@@ -128,6 +130,15 @@ export async function cliInit(options: CliInitOptions) {
128130
);
129131
}
130132

133+
/**
134+
* Validate CLI init options and handle invalid or incompatible option combinations
135+
*/
136+
async function ensureValidCliInitOptions(options: CliInitOptions, ioHelper: IoHelper) {
137+
if (options.packageManager && !['javascript', 'typescript'].includes(options.language ?? '')) {
138+
await ioHelper.defaults.warn(`--package-manager option is only applicable for JavaScript and TypeScript projects. Ignoring the provided value: ${options.packageManager}`);
139+
}
140+
}
141+
131142
/**
132143
* Load a local custom template from file system path
133144
* @param fromPath - Path to the local template directory or multi-template repository

packages/aws-cdk/test/commands/init.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,61 @@ describe('constructs version', () => {
14231423
expect(await fs.pathExists(path.join(workDir, 'requirements.txt'))).toBeTruthy();
14241424
});
14251425
});
1426+
1427+
describe('validate CLI init options', () => {
1428+
test.each([
1429+
'python',
1430+
'java',
1431+
'go',
1432+
'csharp',
1433+
'fsharp',
1434+
])('warns when package-manager option is specified for non-JS language=%s', async (language) => {
1435+
await withTempDir(async (workDir) => {
1436+
const warnSpy = jest.spyOn(ioHelper.defaults, 'warn');
1437+
1438+
await cliInit({
1439+
ioHelper,
1440+
type: 'app',
1441+
language,
1442+
packageManager: 'npm',
1443+
canUseNetwork: false,
1444+
generateOnly: true,
1445+
workDir,
1446+
});
1447+
1448+
expect(warnSpy).toHaveBeenCalledWith(
1449+
expect.stringContaining('--package-manager option is only applicable for JavaScript and TypeScript projects'),
1450+
);
1451+
1452+
warnSpy.mockRestore();
1453+
});
1454+
});
1455+
1456+
test.each([
1457+
'typescript',
1458+
'javascript',
1459+
])('does not warn when package-manager option is specified for language=%s', async (language) => {
1460+
await withTempDir(async (workDir) => {
1461+
const warnSpy = jest.spyOn(ioHelper.defaults, 'warn');
1462+
1463+
await cliInit({
1464+
ioHelper,
1465+
type: 'app',
1466+
language,
1467+
packageManager: 'npm',
1468+
canUseNetwork: false,
1469+
generateOnly: true,
1470+
workDir,
1471+
});
1472+
1473+
expect(warnSpy).not.toHaveBeenCalledWith(
1474+
expect.stringContaining('--package-manager option is only applicable for JavaScript and TypeScript projects'),
1475+
);
1476+
1477+
warnSpy.mockRestore();
1478+
});
1479+
});
1480+
})
14261481
});
14271482

14281483
test('when no version number is present (e.g., local development), the v2 templates are chosen by default', async () => {

0 commit comments

Comments
 (0)