Skip to content

Commit

Permalink
Clean up package manager resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Feb 9, 2023
1 parent 39315be commit 97f24f8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ const cliOptions: CLIOption[] = [
long: 'packageManager',
short: 'p',
arg: 's',
// manual default to allow overriding auto yarn detection
description: 'npm, yarn, pnpm, staticRegistry (default: npm).',
help: extendedHelpPackageManager,
type: `'npm' | 'yarn' | 'pnpm' | 'staticRegistry'`,
Expand Down
22 changes: 10 additions & 12 deletions src/lib/determinePackageManager.ts
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
2 changes: 1 addition & 1 deletion src/lib/findLockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Options } from '../types/Options'
/**
* Goes up the filesystem tree until it finds a package-lock.json or yarn.lock.
*
* @param readdirSync This is only a parameter so that it can be used in tests.
* @param readdir This is only a parameter so that it can be used in tests.
* @returns The path of the directory that contains the lockfile and the
* filename of the lockfile.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/lib/getPackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { PackageManager } from '../types/PackageManager'
*/
function getPackageManager(name: Maybe<string>): PackageManager {
// default to npm
if (!name || name === 'deno') {
if (!name) {
return packageManagers.npm
}

Expand Down
12 changes: 5 additions & 7 deletions src/lib/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,17 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =

const autoPre = target === 'newest' || target === 'greatest'

const format = options.format || []

const packageManager = await determinePackageManager(options)

// only print 'Using yarn' when autodetected
if (!options.packageManager && packageManager === 'yarn') {
print(options, 'Using yarn')
// print 'Using yarn/pnpm/etc' when autodetected
if (!options.packageManager && packageManager !== 'npm') {
print(options, `Using ${packageManager}`)
}

const resolvedOptions = {
const resolvedOptions: Options = {
...options,
...(options.deep ? { packageFile: '**/package.json' } : null),
...(format.length > 0 ? { format } : null),
...(options.format && options.format.length > 0 ? { format: options.format } : null),
filter: args || filter,
// add shortcut for any keys that start with 'json'
json,
Expand Down

0 comments on commit 97f24f8

Please sign in to comment.