UNPKG

@naandalist/patch-package

Version:
73 lines (65 loc) 2.03 kB
import fs from "fs-extra" import { join } from "./path" import chalk from "chalk" import process from "process" import { findWorkspacesRoot } from "find-workspaces" export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap" function printNoYarnLockfileError() { console.log(` ${chalk.red.bold("**ERROR**")} ${chalk.red( `The --use-yarn option was specified but there is no yarn.lock file`, )} `) } function printNoLockfilesError() { console.log(` ${chalk.red.bold("**ERROR**")} ${chalk.red( `No package-lock.json, npm-shrinkwrap.json, or yarn.lock file. You must use either npm@>=5, yarn, or npm-shrinkwrap to manage this project's dependencies.`, )} `) } function printSelectingDefaultMessage() { console.info( `${chalk.bold( "patch-package", )}: you have both yarn.lock and package-lock.json Defaulting to using ${chalk.bold("npm")} You can override this setting by passing --use-yarn or deleting package-lock.json if you don't need it `, ) } export const detectPackageManager = ( appPath: string, overridePackageManager: PackageManager | null, ): PackageManager => { const lockfilePath = findWorkspacesRoot(appPath)?.location ?? appPath const packageLockExists = fs.existsSync( join(lockfilePath, "package-lock.json"), ) const shrinkWrapExists = fs.existsSync( join(lockfilePath, "npm-shrinkwrap.json"), ) const yarnLockExists = fs.existsSync(join(lockfilePath, "yarn.lock")) if ((packageLockExists || shrinkWrapExists) && yarnLockExists) { if (overridePackageManager) { return overridePackageManager } else { printSelectingDefaultMessage() return shrinkWrapExists ? "npm-shrinkwrap" : "npm" } } else if (packageLockExists || shrinkWrapExists) { if (overridePackageManager === "yarn") { printNoYarnLockfileError() process.exit(1) } else { return shrinkWrapExists ? "npm-shrinkwrap" : "npm" } } else if (yarnLockExists) { return "yarn" } printNoLockfilesError() process.exit(1) }