Skip to content

Commit

Permalink
feat: Detect Yarn v2+ (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich authored Aug 29, 2024
1 parent 0994aff commit 6b942ff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
42 changes: 38 additions & 4 deletions src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion test/sourcemaps/tools/sentry-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 6b942ff

Please sign in to comment.