Skip to content

Commit

Permalink
feat: override package manager version
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 27, 2024
1 parent 2e61389 commit 189eb90
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions tests/excalidraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export async function test(options: RunOptions) {
branch: 'master',
repo: 'excalidraw/excalidraw',
test: 'test:app',
agent: 'yarn',
agentVersion: '2.4.2',
})
}
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface RunOptions {
skipGit?: boolean
release?: string
agent?: Agent
agentVersion?: string
build?: Task | Task[]
test?: Task | Task[]
beforeInstall?: Task | Task[]
Expand Down
19 changes: 12 additions & 7 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export async function runInRepo(options: RunOptions & RepoOptions) {
...localOverrides,
}
}
await applyPackageOverrides(dir, pkg, overrides)
await applyPackageOverrides(dir, pkg, overrides, options.agentVersion)
await beforeBuildCommand?.(pkg.scripts)
await buildCommand?.(pkg.scripts)
if (test) {
Expand Down Expand Up @@ -421,21 +421,25 @@ function isLocalOverride(v: string): boolean {
async function overridePackageManagerVersion(
pkg: { [key: string]: any },
pm: string,
agentVersion?: string,
): Promise<boolean> {
const versionInUse = pkg.packageManager?.startsWith(`${pm}@`)
? pkg.packageManager.substring(pm.length + 1)
: await $`${pm} --version`
let overrideWithVersion: string | null = null
let overrideWithVersion: string | undefined = agentVersion

if (pm === 'pnpm') {
const versionInUse = pkg.packageManager?.startsWith(`${pm}@`)
? pkg.packageManager.substring(pm.length + 1)
: await $`${pm} --version`

if (semver.eq(versionInUse, '7.18.0')) {
// avoid bug with absolute overrides in pnpm 7.18.0
overrideWithVersion = '7.18.1'
}
}
if (overrideWithVersion) {
console.warn(
`detected ${pm}@${versionInUse} used in ${pkg.name}, changing pkg.packageManager and pkg.engines.${pm} to enforce use of ${pm}@${overrideWithVersion}`,
`Changing pkg.packageManager and pkg.engines.${pm} to enforce use of ${pm}@${overrideWithVersion}`,
)

// corepack reads this and uses pnpm @ newVersion then
pkg.packageManager = `${pm}@${overrideWithVersion}`
if (!pkg.engines) {
Expand All @@ -458,6 +462,7 @@ export async function applyPackageOverrides(
dir: string,
pkg: any,
overrides: Overrides = {},
agentVersion?: string,
) {
const useFileProtocol = (v: string) =>
isLocalOverride(v) ? `file:${path.resolve(v)}` : v
Expand All @@ -479,7 +484,7 @@ export async function applyPackageOverrides(
// pnpm@6, pnpm@7 => pnpm
const pm = agent?.split('@')[0]

await overridePackageManagerVersion(pkg, pm)
await overridePackageManagerVersion(pkg, pm, agentVersion)

if (pm === 'pnpm') {
if (!pkg.devDependencies) {
Expand Down

0 comments on commit 189eb90

Please sign in to comment.