Skip to content

Commit

Permalink
fix(next-codemod): get package manager is inconsistent in monorepo (#…
Browse files Browse the repository at this point in the history
…70983)

### Why?

Uses `npm add` on pnpm workspace as whenever error occurs during the process, it uses `npm` which may not be consistent.

![CleanShot 2024-10-09 at 06 32 34](https://github.com/user-attachments/assets/f4180974-a685-4b3f-a23e-654dd9945baf)

### How?

Use `find-up` to traverse and look for lock files.
  • Loading branch information
devjiwonchoi authored and kdy1 committed Oct 10, 2024
1 parent a1169c5 commit d428890
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
48 changes: 18 additions & 30 deletions packages/next-codemod/lib/handle-package.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
import fs from 'fs'
import path from 'path'
import findUp from 'find-up'
import execa from 'execa'
import { basename } from 'node:path'

export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun'

export function getPkgManager(baseDir: string): PackageManager {
try {
for (const { lockFile, packageManager } of [
{ lockFile: 'yarn.lock', packageManager: 'yarn' },
{ lockFile: 'pnpm-lock.yaml', packageManager: 'pnpm' },
{ lockFile: 'package-lock.json', packageManager: 'npm' },
{ lockFile: 'bun.lockb', packageManager: 'bun' },
]) {
if (fs.existsSync(path.join(baseDir, lockFile))) {
return packageManager as PackageManager
}
}
const userAgent = process.env.npm_config_user_agent
if (userAgent) {
if (userAgent.startsWith('yarn')) {
return 'yarn'
} else if (userAgent.startsWith('pnpm')) {
return 'pnpm'
}
}
try {
execa.sync('yarn --version', { stdio: 'ignore' })
return 'yarn'
} catch {
try {
execa.sync('pnpm --version', { stdio: 'ignore' })
return 'pnpm'
} catch {
execa.sync('bun --version', { stdio: 'ignore' })
return 'bun'
const lockFile = findUp.sync(
['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb'],
{ cwd: baseDir }
)
if (lockFile) {
switch (basename(lockFile)) {
case 'package-lock.json':
return 'npm'
case 'yarn.lock':
return 'yarn'
case 'pnpm-lock.yaml':
return 'pnpm'
case 'bun.lockb':
return 'bun'
default:
return 'npm'
}
}
} catch {
Expand Down
2 changes: 2 additions & 0 deletions packages/next-codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"commander": "12.1.0",
"compare-versions": "6.1.1",
"execa": "4.0.3",
"find-up": "4.1.0",
"globby": "11.0.1",
"is-git-clean": "1.1.0",
"jscodeshift": "17.0.0",
Expand All @@ -34,6 +35,7 @@
},
"bin": "./bin/next-codemod.js",
"devDependencies": {
"@types/find-up": "4.0.0",
"@types/jscodeshift": "0.11.0"
}
}
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d428890

Please sign in to comment.