Skip to content

Commit 80d4d15

Browse files
fix(cli): improve language selection logic in cliInit function (#806)
Fixes #660 ### Description Changes When running `cdk init`, if the specified command type can only be executed in a single language, allow the command to be executed without selecting a language. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 4821d4e commit 80d4d15

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,28 @@ export async function cliInit(options: CliInitOptions) {
5252
await printAvailableTemplates(ioHelper, options.language);
5353
throw new ToolkitError(`Unknown init template: ${type}`);
5454
}
55-
if (!options.language && template.languages.length === 1) {
56-
const language = template.languages[0];
57-
await ioHelper.defaults.warn(
58-
`No --language was provided, but '${type}' supports only '${language}', so defaulting to --language=${language}`,
55+
56+
const language = await (async () => {
57+
if (options.language) {
58+
return options.language;
59+
}
60+
if (template.languages.length === 1) {
61+
const templateLanguage = template.languages[0];
62+
await ioHelper.defaults.warn(
63+
`No --language was provided, but '${type}' supports only '${templateLanguage}', so defaulting to --language=${templateLanguage}`,
64+
);
65+
return templateLanguage;
66+
}
67+
await ioHelper.defaults.info(
68+
`Available languages for ${chalk.green(type)}: ${template.languages.map((l) => chalk.blue(l)).join(', ')}`,
5969
);
60-
}
61-
if (!options.language) {
62-
await ioHelper.defaults.info(`Available languages for ${chalk.green(type)}: ${template.languages.map((l) => chalk.blue(l)).join(', ')}`);
6370
throw new ToolkitError('No language was selected');
64-
}
71+
})();
6572

6673
await initializeProject(
6774
ioHelper,
6875
template,
69-
options.language,
76+
language,
7077
canUseNetwork,
7178
generateOnly,
7279
workDir,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ describe('constructs version', () => {
2222
expect(await fs.pathExists(path.join(workDir, 'lib'))).toBeTruthy();
2323
});
2424

25+
cliTest("when type is 'lib' and language is not specified, it default language to TypeScript", async (workDir) => {
26+
await cliInit({
27+
ioHelper,
28+
type: 'lib',
29+
workDir,
30+
});
31+
32+
// Check that tsconfig.json and lib/ got created in the current directory
33+
expect(await fs.pathExists(path.join(workDir, 'tsconfig.json'))).toBeTruthy();
34+
expect(await fs.pathExists(path.join(workDir, 'lib'))).toBeTruthy();
35+
});
36+
2537
cliTest('can override requested version with environment variable', async (workDir) => {
2638
await cliInit({
2739
ioHelper,

0 commit comments

Comments
 (0)