import findUp from "find-up" ; import fs from "fs-extra" ; import { dirname , join , resolve , sep } from "path" ; import { npm } from "global-dirs" ; export enum InstallType { npm = "npm" , npmGlobal = "npmGlobal" , yarn = "yarn" , yarnGlobal = "yarnGlobal" , unknown = "unknown" } export async function installType ( path ) { const dir = await getDir ( path ) ; if ( isYarnGlobal ( dir ) ) return InstallType . yarnGlobal ; if ( isNpmGlobal ( dir ) ) return InstallType . npmGlobal ; const manager = await findManager ( dir ) ; if ( manager ) return manager ; return InstallType . unknown ; } /**
 * Returns `true` if `path` is within a yarn global install directory.
 */ function isYarnGlobal ( path ) { const slashes = ( s ) => sep + s + sep ; const winPath = join ( "Yarn" , "Data" , "global" ) ; const otherPath = join ( "yarn" , "global" ) ; return path . includes ( slashes ( otherPath ) ) || path . includes ( slashes ( winPath ) ) ; } /**
 * Returns `true` if `path` is within the npm global install directory.
 */ function isNpmGlobal ( path ) { return path . startsWith ( npm . prefix ) && ! isYarnGlobal ( path ) ; } async function findManager ( dir ) { const lockFile = await findUp ( [ "yarn.lock" , "package-lock.json" ] , { cwd : dir } ) ; if ( ! lockFile ) return undefined ; if ( lockFile . endsWith ( "yarn.lock" ) ) return InstallType . yarn ; return InstallType . npm ; } async function getDir ( path ) { const stat = await fs . stat ( resolve ( path ) ) ; return stat . isDirectory ( ) ? path : dirname ( path ) ; }