From 6b942ff464dda095b7c253a1b0c879358a850d3a Mon Sep 17 00:00:00 2001 From: Krystof Woldrich <31292499+krystofwoldrich@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:42:55 +0200 Subject: [PATCH] feat: Detect Yarn v2+ (#652) --- CHANGELOG.md | 1 + src/utils/package-manager.ts | 42 +++++++++++++++++++++--- test/sourcemaps/tools/sentry-cli.test.ts | 3 +- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cec743d1..327f1d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - fix(nextjs): Don't inject replay integration in server configs (#651) - fix(deps): fix(deps): Add `recast` as a direct dependency (#653) - fix: Fix issue stream URL for self-hosted instances (#645) +- feat: Detect Yarn v2+ (#652) Work in this release contributed by @MaximAL. Thank you for your contributions! diff --git a/src/utils/package-manager.ts b/src/utils/package-manager.ts index 183b9a3d..3c7aaeae 100644 --- a/src/utils/package-manager.ts +++ b/src/utils/package-manager.ts @@ -14,6 +14,7 @@ export interface PackageManager { /* The command that the package manager uses to run a script from package.json */ runScriptCommand: string; flags: string; + detect: () => boolean; } export const BUN: PackageManager = { @@ -24,15 +25,46 @@ export const BUN: PackageManager = { buildCommand: 'bun run build', runScriptCommand: 'bun run', flags: '', + detect: () => fs.existsSync(path.join(process.cwd(), BUN.lockFile)), }; -export const YARN: PackageManager = { +export const YARN_V1: PackageManager = { name: 'yarn', - label: 'Yarn', + label: 'Yarn V1', lockFile: 'yarn.lock', installCommand: 'yarn add', buildCommand: 'yarn build', runScriptCommand: 'yarn', flags: '--ignore-workspace-root-check', + detect: () => { + try { + return fs + .readFileSync(path.join(process.cwd(), YARN_V1.lockFile), 'utf-8') + .slice(0, 500) + .includes('yarn lockfile v1'); + } catch (e) { + return false; + } + }, +}; +/** YARN V2/3/4 */ +export const YARN_V2: PackageManager = { + name: 'yarn', + label: 'Yarn V2/3/4', + lockFile: 'yarn.lock', + installCommand: 'yarn add', + buildCommand: 'yarn build', + runScriptCommand: 'yarn', + flags: '', + detect: () => { + try { + return fs + .readFileSync(path.join(process.cwd(), YARN_V2.lockFile), 'utf-8') + .slice(0, 500) + .includes('__metadata'); + } catch (e) { + return false; + } + }, }; export const PNPM: PackageManager = { name: 'pnpm', @@ -42,6 +74,7 @@ export const PNPM: PackageManager = { buildCommand: 'pnpm build', runScriptCommand: 'pnpm', flags: '--ignore-workspace-root-check', + detect: () => fs.existsSync(path.join(process.cwd(), PNPM.lockFile)), }; export const NPM: PackageManager = { name: 'npm', @@ -51,14 +84,15 @@ export const NPM: PackageManager = { buildCommand: 'npm run build', runScriptCommand: 'npm run', flags: '', + detect: () => fs.existsSync(path.join(process.cwd(), NPM.lockFile)), }; -export const packageManagers = [BUN, YARN, PNPM, NPM]; +export const packageManagers = [BUN, YARN_V1, YARN_V2, PNPM, NPM]; export function detectPackageManger(): PackageManager | null { return traceStep('detect-package-manager', () => { for (const packageManager of packageManagers) { - if (fs.existsSync(path.join(process.cwd(), packageManager.lockFile))) { + if (packageManager.detect()) { Sentry.setTag('package-manager', packageManager.name); return packageManager; } diff --git a/test/sourcemaps/tools/sentry-cli.test.ts b/test/sourcemaps/tools/sentry-cli.test.ts index 112456c6..8e83addc 100644 --- a/test/sourcemaps/tools/sentry-cli.test.ts +++ b/test/sourcemaps/tools/sentry-cli.test.ts @@ -38,7 +38,8 @@ describe('addSentryCommandToBuildCommand', () => { [ packageManagerHelpers.NPM, packageManagerHelpers.PNPM, - packageManagerHelpers.YARN, + packageManagerHelpers.YARN_V1, + packageManagerHelpers.YARN_V2, packageManagerHelpers.BUN, ], ])('adds the cli command to the script command (%s)', async (_, pacMan) => {