Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/aws-cdk/lib/cli/cli-type-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,12 @@
"pnpm",
"bun"
]
},
"name": {
"type": "string",
"alias": "n",
"desc": "The name of the new project",
"requiresArg": true
}
},
"implies": {
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
fromPath: args['from-path'],
templatePath: args['template-path'],
packageManager: args['package-manager'],
name: args.name,
});
}
case 'migrate':
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli/convert-to-user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export function convertYargsToUserInput(args: any): UserInput {
fromPath: args.fromPath,
templatePath: args.templatePath,
packageManager: args.packageManager,
name: args.name,
TEMPLATE: args.TEMPLATE,
};
break;
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk/lib/cli/parse-command-line-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,13 @@ export function parseCommandLineArguments(args: Array<string>): any {
type: 'string',
desc: 'The package manager to use to install dependencies. Only applicable for TypeScript and JavaScript projects. Defaults to npm in TypeScript and JavaScript projects.',
choices: ['npm', 'yarn', 'pnpm', 'bun'],
})
.option('name', {
default: undefined,
type: 'string',
alias: 'n',
desc: 'The name of the new project',
requiresArg: true,
}),
)
.command('migrate', 'Migrate existing AWS resources into a CDK app', (yargs: Argv) =>
Expand Down
9 changes: 9 additions & 0 deletions packages/aws-cdk/lib/cli/user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,15 @@ export interface InitOptions {
*/
readonly packageManager?: string;

/**
* The name of the new project
*
* aliases: n
*
* @default - undefined
*/
readonly name?: string;

/**
* Positional argument for init
*/
Expand Down
12 changes: 10 additions & 2 deletions packages/aws-cdk/lib/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export interface CliInitOptions {
*/
readonly workDir?: string;

/**
* @default undefined
*/
readonly name?: string;

/**
* @default undefined
*/
Expand Down Expand Up @@ -122,6 +127,7 @@ export async function cliInit(options: CliInitOptions) {
canUseNetwork,
generateOnly,
workDir,
options.name,
options.stackName,
options.migrate,
options.libVersion,
Expand Down Expand Up @@ -427,6 +433,7 @@ export class InitTemplate {
ioHelper: IoHelper,
language: string,
targetDirectory: string,
name?: string,
stackName?: string,
libVersion?: string,
packageManager?: JsPackageManager,
Expand All @@ -440,7 +447,7 @@ export class InitTemplate {
}

const projectInfo: ProjectInfo = {
name: decamelize(path.basename(path.resolve(targetDirectory))),
name: name ? decamelize(name) : decamelize(path.basename(path.resolve(targetDirectory))),
stackName,
versions: await loadInitVersions(),
};
Expand Down Expand Up @@ -695,6 +702,7 @@ async function initializeProject(
canUseNetwork: boolean,
generateOnly: boolean,
workDir: string,
name?: string,
stackName?: string,
migrate?: boolean,
cdkVersion?: string,
Expand All @@ -705,7 +713,7 @@ async function initializeProject(

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

if (migrate) {
await template.addMigrateContext(workDir);
Expand Down
13 changes: 13 additions & 0 deletions packages/aws-cdk/test/commands/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ describe('constructs version', () => {
expect(Object.entries(pj.devDependencies)).toContainEqual(['aws-cdk-lib', '2.100']);
});

cliTest('can specify project name with --name option', async (workDir) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be

cliTest('can specify project name with --project-name option', async (workDir) => {

instead of

cliTest('can specify project name with --name option', async (workDir) => {

await cliInit({
ioHelper,
type: 'app',
language: 'typescript',
workDir,
name: 'my-project',
});

const stackFile = await fs.readFile(path.join(workDir, 'lib', 'my-project-stack.ts'), 'utf-8');
expect(stackFile).toContain('export class MyProjectStack');
});

cliTest('asking for a nonexistent template fails', async (workDir) => {
await expect(cliInit({
ioHelper,
Expand Down
Loading