From 06b39c6ef961a52f4fe86efbe87b1696a19a3787 Mon Sep 17 00:00:00 2001 From: James Telfer <792299+jamestelfer@users.noreply.github.com> Date: Fri, 23 Jun 2023 16:01:50 +1000 Subject: [PATCH] fix: support pnpm when collecting local package info Use `pnpnm-lock.yaml` as a signal that `pnpm` is the package manager in use, and use `pnpm list` to discover environmental information in that case. --- packages/@cdktf/commons/src/debug.ts | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/packages/@cdktf/commons/src/debug.ts b/packages/@cdktf/commons/src/debug.ts index 1bf8544d27..e01b8212ac 100644 --- a/packages/@cdktf/commons/src/debug.ts +++ b/packages/@cdktf/commons/src/debug.ts @@ -6,6 +6,7 @@ import { logger } from "./logging"; import { exec } from "./util"; import { terraformVersion } from "./terraform"; import { DISPLAY_VERSION } from "./version"; +import { pathExists } from "fs-extra"; export function getLanguage(projectPath = process.cwd()): string | undefined { try { @@ -72,8 +73,60 @@ export function getNodeVersion() { async function getNodeModuleVersion( packageName: string +): Promise { + // Use the presence of the pnpm lock file as a signal that + // we should interrogate packages via pnpm instead of npm. + const usingPnpm = await pathExists("pnpm-lock.yaml"); + + return usingPnpm + ? getPnpmNodeModuleVersion(packageName) + : getNpmNodeModuleVersion(packageName); +} + +async function getPnpmNodeModuleVersion( + packageName: string +): Promise { + let output; + + try { + output = await exec("pnpm", ["list", packageName, "--json"], { + env: { ...process.env }, + }); + } catch (e) { + logger.debug(`Unable to run 'pnpm list ${packageName} --json': ${e}`); + return undefined; + } + + let json; + try { + json = JSON.parse(output); + } catch (e) { + logger.debug( + `Unable to parse output of 'pnpm list ${packageName} --json': ${e}` + ); + return undefined; + } + + if ( + !json || + !Array.isArray(json) || + json.length === 0 || + !json[0]?.dependencies?.[packageName]?.version + ) { + logger.debug( + `Unable to find '${packageName}' in 'npm list ${packageName} --json': ${output}` + ); + return undefined; + } + + return json[0].dependencies[packageName].version; +} + +async function getNpmNodeModuleVersion( + packageName: string ): Promise { let output; + try { output = await exec("npm", ["list", packageName, "--json"], { env: { ...process.env },