From d42889009bb062447e3736460cb0c8480d1c7ef9 Mon Sep 17 00:00:00 2001 From: Jiwon Choi Date: Wed, 9 Oct 2024 13:44:43 +0900 Subject: [PATCH] fix(next-codemod): get package manager is inconsistent in monorepo (#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. --- packages/next-codemod/lib/handle-package.ts | 48 ++++++++------------- packages/next-codemod/package.json | 2 + pnpm-lock.yaml | 14 ++++++ 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/packages/next-codemod/lib/handle-package.ts b/packages/next-codemod/lib/handle-package.ts index d4b298642f216..6ad4355adc41b 100644 --- a/packages/next-codemod/lib/handle-package.ts +++ b/packages/next-codemod/lib/handle-package.ts @@ -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 { diff --git a/packages/next-codemod/package.json b/packages/next-codemod/package.json index 5ac7897bc8597..90d2ace2c6c7a 100644 --- a/packages/next-codemod/package.json +++ b/packages/next-codemod/package.json @@ -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", @@ -34,6 +35,7 @@ }, "bin": "./bin/next-codemod.js", "devDependencies": { + "@types/find-up": "4.0.0", "@types/jscodeshift": "0.11.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6037fdfdfb6f6..785776b3d59df 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1520,6 +1520,9 @@ importers: execa: specifier: 4.0.3 version: 4.0.3 + find-up: + specifier: 4.1.0 + version: 4.1.0 globby: specifier: 11.0.1 version: 11.0.1 @@ -1536,6 +1539,9 @@ importers: specifier: 2.4.2 version: 2.4.2 devDependencies: + '@types/find-up': + specifier: 4.0.0 + version: 4.0.0 '@types/jscodeshift': specifier: 0.11.0 version: 0.11.0 @@ -5024,6 +5030,10 @@ packages: '@types/express@4.17.2': resolution: {integrity: sha512-5mHFNyavtLoJmnusB8OKJ5bshSzw+qkMIBAobLrIM48HJvunFva9mOa6aBwh64lBFyNwBbs0xiEFuj4eU/NjCA==} + '@types/find-up@4.0.0': + resolution: {integrity: sha512-QlRNKeOPFWKisbNtKVOOGXw3AeLbkw8UmT/EyEGM6brfqpYffKBcch7f1y40NYN9O90aK2+K0xBMDJfOAsg2qg==} + deprecated: This is a stub types definition. find-up provides its own type definitions, so you do not need this installed. + '@types/fontkit@2.0.0': resolution: {integrity: sha512-Qe+6szpPLTNsqkDFs2MScJyB51d5Hpobyg/T0QoUWO53WuNOTNLsV8fkE4QQPOJbhOMN5wlwmNeDdsh/e6Uqdg==} @@ -19582,6 +19592,10 @@ snapshots: '@types/express-serve-static-core': 4.17.33 '@types/serve-static': 1.13.3 + '@types/find-up@4.0.0': + dependencies: + find-up: 4.1.0 + '@types/fontkit@2.0.0': dependencies: '@types/node': 20.12.3