From 44def7846aa7916938f25c7142bea3cbf792da54 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 28 May 2024 12:08:13 -0700 Subject: [PATCH] feat: detect pnpm version from lockfile --- src/install-pnpm/run.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/install-pnpm/run.ts b/src/install-pnpm/run.ts index 76a33e1..122527e 100644 --- a/src/install-pnpm/run.ts +++ b/src/install-pnpm/run.ts @@ -5,6 +5,7 @@ import { readFileSync } from 'fs' import path from 'path' import { execPath } from 'process' import util from 'util' +import * as yaml from 'yaml' import { Inputs } from '../inputs' export async function runSelfInstaller(inputs: Inputs): Promise { @@ -78,10 +79,18 @@ Otherwise, please specify the pnpm version in the action configuration.`) } if (typeof packageManager !== 'string') { - throw new Error(`No pnpm version is specified. + if (GITHUB_WORKSPACE) { + try { + const { lockfileVersion } = yaml.parse(readFileSync(path.join(GITHUB_WORKSPACE, 'pnpm-lock.yaml'), 'utf8')) + const version = getPnpmVersionFromLockfile(lockfileVersion); + return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}` + } catch (error: unknown) { + throw new Error(`No pnpm version is specified. Please specify it by one of the following ways: - in the GitHub Action config with the key "version" - in the package.json with the key "packageManager"`) + } + } } if (!packageManager.startsWith('pnpm@')) { @@ -95,4 +104,21 @@ Please specify it by one of the following ways: return packageManager } +function getPnpmVersionFromLockfile( + lockfileVersion: string | undefined +): string | undefined { + switch (true) { + case lockfileVersion === '5.3': + return '6'; + case lockfileVersion === '5.4': + return '7'; + case lockfileVersion === '6.0' || lockfileVersion === '6.1': + return '8'; + case lockfileVersion === '7.0' || lockfileVersion === '9.0': + return '9'; + default: + return undefined; + } +} + export default runSelfInstaller