Skip to content

Commit

Permalink
Add support for pnpm -g (#1265)
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Feb 6, 2023
1 parent 0ad6714 commit 8b38173
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/getPackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function getPackageManager(packageManagerNameOrObject: Maybe<string | PackageMan
return (packageManagers as any)[key]
}

return !packageManagerNameOrObject || packageManagerNameOrObject === 'pnpm'
return !packageManagerNameOrObject
? packageManagers.npm // default to npm
: // use present package manager if name is specified
typeof packageManagerNameOrObject === 'string'
Expand Down
7 changes: 6 additions & 1 deletion src/lib/runGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ async function runGlobal(options: Options): Promise<Index<string> | void> {
// since global packages do not have a package.json, return the upgraded deps directly (no version range replacements)
printJson(options, upgraded)
} else if (instruction.length) {
const upgradeCmd = options.packageManager === 'yarn' ? 'yarn global upgrade' : 'npm -g install'
const upgradeCmd =
options.packageManager === 'yarn'
? 'yarn global upgrade'
: options.packageManager === 'pnpm'
? 'pnpm -g add'
: 'npm -g install'

print(
options,
Expand Down
2 changes: 2 additions & 0 deletions src/package-managers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Index } from '../types/IndexType'
import { PackageManager } from '../types/PackageManager'
import * as gitTags from './gitTags'
import * as npm from './npm'
import * as pnpm from './pnpm'
import * as staticRegistry from './staticRegistry'
import * as yarn from './yarn'

export default {
npm,
pnpm,
yarn,
gitTags,
staticRegistry,
Expand Down
41 changes: 41 additions & 0 deletions src/package-managers/pnpm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import spawn from 'spawn-please'
import keyValueBy from '../lib/keyValueBy'
import { Index } from '../types/IndexType'
import { Options } from '../types/Options'
import { Version } from '../types/Version'
import { list as npmList } from './npm'

// return type of pnpm ls --json
type PnpmList = {
path: string
private: boolean
dependencies: Index<{
from: string
version: Version
resolved: string
}>
}[]

/** Fetches the list of all installed packages. */
export const list = async (options: Options = {}): Promise<Index<string | undefined>> => {
// use npm for local ls
if (!options.global) return npmList(options)

const cmd = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'
const result = JSON.parse(await spawn(cmd, ['ls', '-g', '--json'])) as PnpmList
const list = keyValueBy(result[0].dependencies || {}, (name, { version }) => ({
[name]: version,
}))
return list
}

export {
defaultPrefix,
distTag,
getPeerDependencies,
greatest,
latest,
minor,
newest,
packageAuthorChanged,
} from './npm'

0 comments on commit 8b38173

Please sign in to comment.