Skip to content

Commit 29e23cc

Browse files
committed
fix(cli): README does not support package manager being used
1 parent 82fb0f3 commit 29e23cc

File tree

9 files changed

+58
-29
lines changed

9 files changed

+58
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export async function makeConfig(): Promise<CliConfig> {
408408
'lib-version': { type: 'string', alias: 'V', default: undefined, desc: 'The version of the CDK library (aws-cdk-lib) to initialize built-in templates with. Defaults to the version that was current when this CLI was built.' },
409409
'from-path': { type: 'string', desc: 'Path to a local custom template directory or multi-template repository', requiresArg: true, conflicts: ['lib-version'] },
410410
'template-path': { type: 'string', desc: 'Path to a specific template within a multi-template repository', requiresArg: true },
411-
'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 },
411+
'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) },
412412
},
413413
implies: { 'template-path': 'from-path' },
414414
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function invokeBuiltinHooks(ioHelper: IoHelper, target: HookTarget,
7676
case 'javascript':
7777
case 'typescript':
7878
// See above, but for 'package.json'.
79-
await context.substitutePlaceholdersIn('package.json');
79+
await context.substitutePlaceholdersIn('package.json', 'README.md');
8080
}
8181
}
8282

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { versionNumber } from '../../cli/version';
1010
import { cdkHomeDir, formatErrorMessage, rangeFromSemver } from '../../util';
1111
import type { LanguageInfo } from '../language';
1212
import { getLanguageAlias, getLanguageExtensions, SUPPORTED_LANGUAGES } from '../language';
13-
import type { JsPackageManager } from './package-manager';
13+
import { getPmCmdPrefix, type JsPackageManager } from './package-manager';
1414

1515
/* eslint-disable @typescript-eslint/no-var-requires */ // Packages don't have @types module
1616
// eslint-disable-next-line @typescript-eslint/no-require-imports
@@ -423,7 +423,14 @@ export class InitTemplate {
423423
* @param libVersion - the version of the CDK library to use
424424
* @default undefined
425425
*/
426-
public async install(ioHelper: IoHelper, language: string, targetDirectory: string, stackName?: string, libVersion?: string) {
426+
public async install(
427+
ioHelper: IoHelper,
428+
language: string,
429+
targetDirectory: string,
430+
stackName?: string,
431+
libVersion?: string,
432+
packageManager?: JsPackageManager,
433+
): Promise<void> {
427434
if (this.languages.indexOf(language) === -1) {
428435
await ioHelper.defaults.error(
429436
`The ${chalk.blue(language)} language is not supported for ${chalk.green(this.name)} ` +
@@ -455,7 +462,7 @@ export class InitTemplate {
455462
await this.installFilesWithoutProcessing(sourceDirectory, targetDirectory);
456463
} else {
457464
// For built-in templates, process placeholders as usual
458-
await this.installFiles(sourceDirectory, targetDirectory, language, projectInfo);
465+
await this.installFiles(sourceDirectory, targetDirectory, language, projectInfo, packageManager);
459466
await this.applyFutureFlags(targetDirectory);
460467
await invokeBuiltinHooks(
461468
ioHelper,
@@ -465,27 +472,33 @@ export class InitTemplate {
465472
const fileProcessingPromises = fileNames.map(async (fileName) => {
466473
const fullPath = path.join(targetDirectory, fileName);
467474
const template = await fs.readFile(fullPath, { encoding: 'utf-8' });
468-
await fs.writeFile(fullPath, expandPlaceholders(template, language, projectInfo));
475+
await fs.writeFile(fullPath, expandPlaceholders(template, language, projectInfo, packageManager));
469476
});
470477
/* eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism */ // Processing a small, known set of template files
471478
await Promise.all(fileProcessingPromises);
472479
},
473-
placeholder: (ph: string) => expandPlaceholders(`%${ph}%`, language, projectInfo),
480+
placeholder: (ph: string) => expandPlaceholders(`%${ph}%`, language, projectInfo, packageManager),
474481
},
475482
);
476483
}
477484
}
478485

479-
private async installFiles(sourceDirectory: string, targetDirectory: string, language: string, project: ProjectInfo) {
486+
private async installFiles(
487+
sourceDirectory: string,
488+
targetDirectory: string,
489+
language: string,
490+
project: ProjectInfo,
491+
packageManager?: JsPackageManager,
492+
): Promise<void> {
480493
for (const file of await fs.readdir(sourceDirectory)) {
481494
const fromFile = path.join(sourceDirectory, file);
482-
const toFile = path.join(targetDirectory, expandPlaceholders(file, language, project));
495+
const toFile = path.join(targetDirectory, expandPlaceholders(file, language, project, packageManager));
483496
if ((await fs.stat(fromFile)).isDirectory()) {
484497
await fs.mkdir(toFile);
485-
await this.installFiles(fromFile, toFile, language, project);
498+
await this.installFiles(fromFile, toFile, language, project, packageManager);
486499
continue;
487500
} else if (file.match(/^.*\.template\.[^.]+$/)) {
488-
await this.installProcessed(fromFile, toFile.replace(/\.template(\.[^.]+)$/, '$1'), language, project);
501+
await this.installProcessed(fromFile, toFile.replace(/\.template(\.[^.]+)$/, '$1'), language, project, packageManager);
489502
continue;
490503
} else if (file.match(/^.*\.hook\.(d.)?[^.]+$/)) {
491504
// Ignore
@@ -496,9 +509,15 @@ export class InitTemplate {
496509
}
497510
}
498511

499-
private async installProcessed(templatePath: string, toFile: string, language: string, project: ProjectInfo) {
512+
private async installProcessed(
513+
templatePath: string,
514+
toFile: string,
515+
language: string,
516+
project: ProjectInfo,
517+
packageManager?: JsPackageManager,
518+
) {
500519
const template = await fs.readFile(templatePath, { encoding: 'utf-8' });
501-
await fs.writeFile(toFile, expandPlaceholders(template, language, project));
520+
await fs.writeFile(toFile, expandPlaceholders(template, language, project, packageManager));
502521
}
503522

504523
/**
@@ -548,7 +567,7 @@ export class InitTemplate {
548567
}
549568
}
550569

551-
export function expandPlaceholders(template: string, language: string, project: ProjectInfo) {
570+
export function expandPlaceholders(template: string, language: string, project: ProjectInfo, packageManager?: JsPackageManager) {
552571
const cdkVersion = project.versions['aws-cdk-lib'];
553572
const cdkCliVersion = project.versions['aws-cdk'];
554573
let constructsVersion = project.versions.constructs;
@@ -582,7 +601,8 @@ export function expandPlaceholders(template: string, language: string, project:
582601
.replace(/%cdk-home%/g, cdkHomeDir())
583602
.replace(/%name\.PythonModule%/g, project.name.replace(/-/g, '_'))
584603
.replace(/%python-executable%/g, pythonExecutable())
585-
.replace(/%name\.StackName%/g, project.name.replace(/[^A-Za-z0-9-]/g, '-'));
604+
.replace(/%name\.StackName%/g, project.name.replace(/[^A-Za-z0-9-]/g, '-'))
605+
.replace(/%pm-cmd%/g, getPmCmdPrefix(packageManager ?? 'npm'));
586606
}
587607

588608
interface ProjectInfo {
@@ -679,7 +699,7 @@ async function initializeProject(
679699

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

684704
if (migrate) {
685705
await template.addMigrateContext(workDir);
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
export const JS_PACKAGE_MANAGERS = ['npm', 'yarn', 'pnpm', 'bun'] as const;
1+
export const JS_PACKAGE_MANAGERS = [
2+
{ name: 'npm', commandPrefix: 'npm run' },
3+
{ name: 'yarn', commandPrefix: 'yarn' },
4+
{ name: 'pnpm', commandPrefix: 'pnpm' },
5+
{ name: 'bun', commandPrefix: 'bun run' },
6+
] as const;
27

3-
export type JsPackageManager = (typeof JS_PACKAGE_MANAGERS)[number];
8+
export type JsPackageManager = (typeof JS_PACKAGE_MANAGERS)[number]['name'];
9+
10+
export const getPmCmdPrefix = (packageManager: JsPackageManager): string => {
11+
return JS_PACKAGE_MANAGERS.find(pm => pm.name === packageManager)!.commandPrefix;
12+
};

packages/aws-cdk/lib/init-templates/app/javascript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `cdk.json` file tells the CDK Toolkit how to execute your app. The build ste
66

77
## Useful commands
88

9-
* `npm run test` perform the jest unit tests
9+
* `%pm-cmd% test` perform the jest unit tests
1010
* `npx cdk deploy` deploy this stack to your default AWS account/region
1111
* `npx cdk diff` compare deployed stack with current state
1212
* `npx cdk synth` emits the synthesized CloudFormation template

packages/aws-cdk/lib/init-templates/app/typescript/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ The `cdk.json` file tells the CDK Toolkit how to execute your app.
66

77
## Useful commands
88

9-
* `npm run build` compile typescript to js
10-
* `npm run watch` watch for changes and compile
11-
* `npm run test` perform the jest unit tests
9+
* `%pm-cmd% build` compile typescript to js
10+
* `%pm-cmd% watch` watch for changes and compile
11+
* `%pm-cmd% test` perform the jest unit tests
1212
* `npx cdk deploy` deploy this stack to your default AWS account/region
1313
* `npx cdk diff` compare deployed stack with current state
1414
* `npx cdk synth` emits the synthesized CloudFormation template

packages/aws-cdk/lib/init-templates/lib/typescript/README.template.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ The construct defines an interface (`%name.PascalCased%Props`) to configure the
77

88
## Useful commands
99

10-
* `npm run build` compile typescript to js
11-
* `npm run watch` watch for changes and compile
12-
* `npm run test` perform the jest unit tests
10+
* `%pm-cmd% build` compile typescript to js
11+
* `%pm-cmd% watch` watch for changes and compile
12+
* `%pm-cmd% test` perform the jest unit tests

packages/aws-cdk/lib/init-templates/sample-app/javascript/README.template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The `cdk.json` file tells the CDK Toolkit how to execute your app. The build ste
77

88
## Useful commands
99

10-
* `npm run test` perform the jest unit tests
10+
* `%pm-cmd% test` perform the jest unit tests
1111
* `cdk deploy` deploy this stack to your default AWS account/region
1212
* `cdk diff` compare deployed stack with current state
1313
* `cdk synth` emits the synthesized CloudFormation template

packages/aws-cdk/lib/init-templates/sample-app/typescript/README.template.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ The `cdk.json` file tells the CDK Toolkit how to execute your app.
77

88
## Useful commands
99

10-
* `npm run build` compile typescript to js
11-
* `npm run watch` watch for changes and compile
12-
* `npm run test` perform the jest unit tests
10+
* `%pm-cmd% build` compile typescript to js
11+
* `%pm-cmd% watch` watch for changes and compile
12+
* `%pm-cmd% test` perform the jest unit tests
1313
* `cdk deploy` deploy this stack to your default AWS account/region
1414
* `cdk diff` compare deployed stack with current state
1515
* `cdk synth` emits the synthesized CloudFormation template

0 commit comments

Comments
 (0)