From 189eb90b3a04eaec372b1c724107c8271750f414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Sat, 27 Apr 2024 10:58:21 +0300 Subject: [PATCH] feat: override package manager version --- tests/excalidraw.ts | 2 ++ types.d.ts | 1 + utils.ts | 19 ++++++++++++------- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/excalidraw.ts b/tests/excalidraw.ts index 9ce9f5d..ff939e6 100644 --- a/tests/excalidraw.ts +++ b/tests/excalidraw.ts @@ -7,5 +7,7 @@ export async function test(options: RunOptions) { branch: 'master', repo: 'excalidraw/excalidraw', test: 'test:app', + agent: 'yarn', + agentVersion: '2.4.2', }) } diff --git a/types.d.ts b/types.d.ts index bc1e7bc..4a7a644 100644 --- a/types.d.ts +++ b/types.d.ts @@ -17,6 +17,7 @@ export interface RunOptions { skipGit?: boolean release?: string agent?: Agent + agentVersion?: string build?: Task | Task[] test?: Task | Task[] beforeInstall?: Task | Task[] diff --git a/utils.ts b/utils.ts index 4cf975c..04aa927 100644 --- a/utils.ts +++ b/utils.ts @@ -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) { @@ -421,12 +421,15 @@ function isLocalOverride(v: string): boolean { async function overridePackageManagerVersion( pkg: { [key: string]: any }, pm: string, + agentVersion?: string, ): Promise { - 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' @@ -434,8 +437,9 @@ async function overridePackageManagerVersion( } 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) { @@ -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 @@ -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) {