Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestelfer committed Jun 23, 2023
1 parent 420ae5f commit 13174e3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions packages/@cdktf/commons/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -72,8 +73,58 @@ export function getNodeVersion() {

async function getNodeModuleVersion(
packageName: string
): Promise<string | undefined> {
const usingPnpm = await pathExists("pnpm-lock.yaml");

return usingPnpm
? getPnpmNodeModuleVersion(packageName)
: getNpmNodeModuleVersion(packageName);
}

async function getPnpmNodeModuleVersion(
packageName: string
): Promise<string | undefined> {
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<string | undefined> {
let output;

try {
output = await exec("npm", ["list", packageName, "--json"], {
env: { ...process.env },
Expand Down

0 comments on commit 13174e3

Please sign in to comment.