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: detect pnpm version from lockfile #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion src/install-pnpm/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
Expand Down Expand Up @@ -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}`
Comment on lines +85 to +86
Copy link
Member

Choose a reason for hiding this comment

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

what if version will be undefined?

} 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@')) {
Expand All @@ -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';
benmccann marked this conversation as resolved.
Show resolved Hide resolved
benmccann marked this conversation as resolved.
Show resolved Hide resolved
default:
return undefined;
}
}

export default runSelfInstaller
Loading