Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable pnpx with cdk init #30661

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"app": "npx ts-node --prefer-ts-exts bin/%name%.ts",
"app": "%appcommandtool% ts-node --prefer-ts-exts bin/%name%.ts",
"watch": {
"include": ["**"],
"exclude": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"app": "npx ts-node --prefer-ts-exts bin/%name%.ts",
"app": "%appcommandtool% ts-node --prefer-ts-exts bin/%name%.ts",
"watch": {
"include": ["**"],
"exclude": [
Expand Down
23 changes: 19 additions & 4 deletions packages/aws-cdk/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,14 @@ export class InitTemplate {

private async installProcessed(templatePath: string, toFile: string, language: string, project: ProjectInfo) {
const template = await fs.readFile(templatePath, { encoding: 'utf-8' });
await fs.writeFile(toFile, this.expand(template, language, project));
let appCommandTool = 'npx';
if (toFile.includes('cdk.json') && await getCommand() === 'pnpm') {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is hard-coded for pnpm, but we wont support yarn or other package managers etc. Is that the approach we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

per my understanding we do not want to extend this feature pass pnpm following the comments in the original issue I think we should just expand the support to pnpm. Otherwise we expose ourselves into making changes every time there is a possibility of having a new package manager. Any thoughts?

appCommandTool = 'pnpx';
}
await fs.writeFile(toFile, this.expand(template, language, project, appCommandTool));
}

private expand(template: string, language: string, project: ProjectInfo) {
private expand(template: string, language: string, project: ProjectInfo, appCommandTool: string = 'npx') {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const manifest = require(path.join(rootDir(), 'package.json'));
const MATCH_VER_BUILD = /\+[a-f0-9]+$/; // Matches "+BUILD" in "x.y.z-beta+BUILD"
Expand All @@ -173,6 +177,8 @@ export class InitTemplate {
break;
}
return template.replace(/%name%/g, project.name)
// the appcommand is only added to the cdk.json file when initializing a Typescript app.
.replace(/%appcommandtool%/g, appCommandTool)
.replace(/%stackname%/, project.stackName ?? '%name.PascalCased%Stack')
.replace(/%PascalNameSpace%/, project.stackName ? camelCase(project.stackName + 'Stack', { pascalCase: true }) : '%name.PascalCased%')
.replace(/%PascalStackProps%/, project.stackName ? (camelCase(project.stackName, { pascalCase: true }) + 'StackProps') : 'StackProps')
Expand Down Expand Up @@ -344,8 +350,7 @@ async function postInstallJavascript(canUseNetwork: boolean, cwd: string) {
}

async function postInstallTypescript(canUseNetwork: boolean, cwd: string) {
const command = 'npm';

const command = await getCommand();
if (!canUseNetwork) {
warning(`Please run '${command} install'!`);
return;
Expand All @@ -359,6 +364,16 @@ async function postInstallTypescript(canUseNetwork: boolean, cwd: string) {
}
}

async function getCommand(): Promise<string> {
print('Getting command...');
if (process.env.npm_execpath && process.env.npm_execpath.includes('pnpm')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (process.env.npm_execpath && process.env.npm_execpath.includes('pnpm')) {
if (process.env.npm_execpath?.includes('pnpm')) {

print(`Got exec path ${chalk.green(`${process.env.npm_execpath}`)}...`);
print(`Got command ${chalk.green(`${'pnpm'}`)}...`);
return 'pnpm';
Comment on lines +370 to +372
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
print(`Got exec path ${chalk.green(`${process.env.npm_execpath}`)}...`);
print(`Got command ${chalk.green(`${'pnpm'}`)}...`);
return 'pnpm';
return 'pnpm';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding it and testing as we speak

}
return 'npm';
}

async function postInstallJava(canUseNetwork: boolean, cwd: string) {
const mvnPackageWarning = 'Please run \'mvn package\'!';
if (!canUseNetwork) {
Expand Down
Loading