aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.71k stars 3.94k forks source link

cdk: add option to specify projectName to `cdk init` #31992

Open ren-yamanashi opened 4 weeks ago

ren-yamanashi commented 4 weeks ago

Describe the feature

When running cdk init, I want to pass projectName as a command option.

Use Case

I'm always frustrated when I have to manually change the stack name and file names generated by cdk init.
This could be resolved by making a projectName option available.

Proposed Solution

I think the steps for possible implementation are as follows

  1. Add name option

For example, add name to the command options.
(see: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk/lib/cli.ts#L304-L308)

    .command('init [TEMPLATE]', 'Create a new, empty CDK project from a template.', (yargs: Argv) => yargs
      .option('language', { type: 'string', alias: 'l', desc: 'The language to be used for the new project (default can be configured in ~/.cdk.json)', choices: initTemplateLanguages })
+    .option('name', { type: 'string', alias: 'n', desc: 'The name of new project' })
      .option('list', { type: 'boolean', desc: 'List the available templates' })
      .option('generate-only', { type: 'boolean', default: false, desc: 'If true, only generates project files, without executing additional operations such as setting up a git repo, installing dependencies or compiling the project' }),
    )
  1. Modify the implementation of packages/aws-cdk/lib/init.ts

Add the name property to CliInitOptions and modify the install method as follows (see: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk/lib/init.ts)

-  public async install(language: string, targetDirectory: string, stackName?: string) {
+  public async install(language: string, targetDirectory: string, name?:string, stackName?: string) {
  // ... some implementations
    const projectInfo: ProjectInfo = {
-     name: decamelize(path.basename(path.resolve(targetDirectory))),
+     name: decamelize(name ?? path.basename(path.resolve(targetDirectory))),
      stackName,
    };
  // ... some implementations
  }

Other Information

No response

Acknowledgements

CDK version used

2.146.0

Environment details (OS name and version, etc.)

macOS Sonoma v14.0

tmokmss commented 3 weeks ago

related: #29695

ashishdhingra commented 3 weeks ago

Findings:

megarach0 commented 2 weeks ago

Findings:

The comment for #29695 mentioned above was made in April. Any update on how we should deal with this issue going forward?