Skip to content

Commit 90acec1

Browse files
committed
feat(cli): add --name option to cdk init (#990)
1 parent a9d5b84 commit 90acec1

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

packages/aws-cdk/lib/cli/cli-type-registry.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,12 @@
913913
"pnpm",
914914
"bun"
915915
]
916+
},
917+
"name": {
918+
"type": "string",
919+
"alias": "n",
920+
"desc": "The name of the new project",
921+
"requiresArg": true
916922
}
917923
},
918924
"implies": {

packages/aws-cdk/lib/cli/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
562562
fromPath: args['from-path'],
563563
templatePath: args['template-path'],
564564
packageManager: args['package-manager'],
565+
name: args.name,
565566
});
566567
}
567568
case 'migrate':

packages/aws-cdk/lib/cli/convert-to-user-input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export function convertYargsToUserInput(args: any): UserInput {
251251
fromPath: args.fromPath,
252252
templatePath: args.templatePath,
253253
packageManager: args.packageManager,
254+
name: args.name,
254255
TEMPLATE: args.TEMPLATE,
255256
};
256257
break;

packages/aws-cdk/lib/cli/parse-command-line-arguments.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,13 @@ export function parseCommandLineArguments(args: Array<string>): any {
900900
type: 'string',
901901
desc: 'The package manager to use to install dependencies. Only applicable for TypeScript and JavaScript projects. Defaults to npm in TypeScript and JavaScript projects.',
902902
choices: ['npm', 'yarn', 'pnpm', 'bun'],
903+
})
904+
.option('name', {
905+
default: undefined,
906+
type: 'string',
907+
alias: 'n',
908+
desc: 'The name of the new project',
909+
requiresArg: true,
903910
}),
904911
)
905912
.command('migrate', 'Migrate existing AWS resources into a CDK app', (yargs: Argv) =>

packages/aws-cdk/lib/cli/user-input.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,15 @@ export interface InitOptions {
14151415
*/
14161416
readonly packageManager?: string;
14171417

1418+
/**
1419+
* The name of the new project
1420+
*
1421+
* aliases: n
1422+
*
1423+
* @default - undefined
1424+
*/
1425+
readonly name?: string;
1426+
14181427
/**
14191428
* Positional argument for init
14201429
*/

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export interface CliInitOptions {
4848
*/
4949
readonly workDir?: string;
5050

51+
/**
52+
* @default undefined
53+
*/
54+
readonly name?: string;
55+
5156
/**
5257
* @default undefined
5358
*/
@@ -122,6 +127,7 @@ export async function cliInit(options: CliInitOptions) {
122127
canUseNetwork,
123128
generateOnly,
124129
workDir,
130+
options.name,
125131
options.stackName,
126132
options.migrate,
127133
options.libVersion,
@@ -427,6 +433,7 @@ export class InitTemplate {
427433
ioHelper: IoHelper,
428434
language: string,
429435
targetDirectory: string,
436+
name?: string,
430437
stackName?: string,
431438
libVersion?: string,
432439
packageManager?: JsPackageManager,
@@ -440,7 +447,7 @@ export class InitTemplate {
440447
}
441448

442449
const projectInfo: ProjectInfo = {
443-
name: decamelize(path.basename(path.resolve(targetDirectory))),
450+
name: name ? decamelize(name) : decamelize(path.basename(path.resolve(targetDirectory))),
444451
stackName,
445452
versions: await loadInitVersions(),
446453
};
@@ -695,6 +702,7 @@ async function initializeProject(
695702
canUseNetwork: boolean,
696703
generateOnly: boolean,
697704
workDir: string,
705+
name?: string,
698706
stackName?: string,
699707
migrate?: boolean,
700708
cdkVersion?: string,
@@ -705,7 +713,7 @@ async function initializeProject(
705713

706714
// Step 2: Copy template files
707715
await ioHelper.defaults.info(`Applying project template ${chalk.green(template.name)} for ${chalk.blue(language)}`);
708-
await template.install(ioHelper, language, workDir, stackName, cdkVersion, packageManager);
716+
await template.install(ioHelper, language, workDir, name, stackName, cdkVersion, packageManager);
709717

710718
if (migrate) {
711719
await template.addMigrateContext(workDir);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ describe('constructs version', () => {
6565
expect(Object.entries(pj.devDependencies)).toContainEqual(['aws-cdk-lib', '2.100']);
6666
});
6767

68+
cliTest('can specify project name with --name option', async (workDir) => {
69+
await cliInit({
70+
ioHelper,
71+
type: 'app',
72+
language: 'typescript',
73+
workDir,
74+
name: 'my-project',
75+
});
76+
77+
const stackFile = await fs.readFile(path.join(workDir, 'lib', 'my-project-stack.ts'), 'utf-8');
78+
expect(stackFile).toContain('export class MyProjectStack');
79+
});
80+
6881
cliTest('asking for a nonexistent template fails', async (workDir) => {
6982
await expect(cliInit({
7083
ioHelper,

0 commit comments

Comments
 (0)