-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up package manager resolution.
- Loading branch information
1 parent
39315be
commit 97f24f8
Showing
5 changed files
with
17 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
import fs from 'fs/promises' | ||
import { Index } from '../types/IndexType' | ||
import { Options } from '../types/Options' | ||
import { PackageManagerName } from '../types/PackageManagerName' | ||
import findLockfile from './findLockfile' | ||
|
||
const defaultPackageManager = 'npm' | ||
// map lockfiles to package managers | ||
const packageManagerLockfileMap: Index<PackageManagerName> = { | ||
'package-lock': 'npm', | ||
yarn: 'yarn', | ||
'pnpm-lock': 'pnpm', | ||
} | ||
|
||
/** | ||
* If the packageManager option was not provided, look at the lockfiles to | ||
* determine which package manager is being used. | ||
* | ||
* @param readdirSync This is only a parameter so that it can be used in tests. | ||
*/ | ||
const determinePackageManager = async ( | ||
options: Options, | ||
// only for testing | ||
readdir: (_path: string) => Promise<string[]> = fs.readdir, | ||
): Promise<PackageManagerName> => { | ||
if (options.packageManager) return options.packageManager | ||
else if (options.global) return defaultPackageManager | ||
else if (options.global) return 'npm' | ||
|
||
const lockfileName = (await findLockfile(options, readdir))?.filename | ||
|
||
return lockfileName === 'package-lock.json' | ||
? 'npm' | ||
: lockfileName === 'yarn.lock' | ||
? 'yarn' | ||
: lockfileName === 'pnpm-lock.yaml' | ||
? 'pnpm' | ||
: defaultPackageManager | ||
return lockfileName ? packageManagerLockfileMap[lockfileName.split('.')[0]] : 'npm' | ||
} | ||
|
||
export default determinePackageManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters