Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export async function makeConfig(): Promise<CliConfig> {
'from-path': { type: 'string', desc: 'Path to a local custom template directory or multi-template repository', requiresArg: true, conflicts: ['lib-version'] },
'template-path': { type: 'string', desc: 'Path to a specific template within a multi-template repository', requiresArg: true },
'package-manager': { 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: JS_PACKAGE_MANAGERS.map(({ name }) => name) },
'project-name': { type: 'string', alias: 'n', desc: 'The name of the new project', requiresArg: true },
},
implies: { 'template-path': 'from-path' },
},
Expand Down
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"
]
},
"project-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'],
projectName: args.name,
});
}
case 'migrate':
Expand Down
2 changes: 2 additions & 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,
projectName: args.projectName,
TEMPLATE: args.TEMPLATE,
};
break;
Expand Down Expand Up @@ -495,6 +496,7 @@ export function convertConfigToUserInput(config: any): UserInput {
fromPath: config.init?.fromPath,
templatePath: config.init?.templatePath,
packageManager: config.init?.packageManager,
projectName: config.init?.projectName,
};
const migrateOptions = {
stackName: config.migrate?.stackName,
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('project-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 projectName?: 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 projectName?: string;

/**
* @default undefined
*/
Expand Down Expand Up @@ -122,6 +127,7 @@ export async function cliInit(options: CliInitOptions) {
canUseNetwork,
generateOnly,
workDir,
options.projectName,
options.stackName,
options.migrate,
options.libVersion,
Expand Down Expand Up @@ -427,6 +433,7 @@ export class InitTemplate {
ioHelper: IoHelper,
language: string,
targetDirectory: string,
projectName?: 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: projectName ? decamelize(projectName) : 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,
projectName?: 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, projectName, 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,
projectName: '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