Skip to content

Commit

Permalink
feat: enable pnpx with cdk init
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Konlog committed Jun 25, 2024
1 parent 8ebfade commit ecd757e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
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') {
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')) {
print(`Got exec path ${chalk.green(`${process.env.npm_execpath}`)}...`);
print(`Got command ${chalk.green(`${'pnpm'}`)}...`);
return 'pnpm';
}
return 'npm';
}

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

0 comments on commit ecd757e

Please sign in to comment.