feat(plugin-commands-store): add store path command (#3571)

Add a `pnpm store path` command to print the location of the pnpm store
as the CLI would resolve it. This allows the user to inspect the current
store location, as well as allows external tooling (like shell scripts)
to locate the pnpm store without needing to make assumptions about its
location or needing to load the `@pnpm/store-path` module.

This command differs from running `pnpm get store`, as `pnpm get store`
prints "undefined" in the case that no custom store path has been set,
which requires external tooling to have knowledge about where pnpm
locates its store in the default case.

close #2575
This commit is contained in:
Jacob Gillespie
2021-07-01 14:35:16 +01:00
committed by GitHub
parent 18e270a2ea
commit f5ec0a96f2
3 changed files with 32 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-store": minor
---
Add `pnpm store path` command that prints the path to the current store directory.

View File

@@ -72,6 +72,8 @@ export async function handler (opts: StoreCommandOptions, params: string[]) {
switch (params[0]) {
case 'status':
return statusCmd(opts)
case 'path':
return storePath(opts.dir, opts.storeDir)
case 'prune': {
store = await createOrConnectStoreController(opts)
const storePruneOptions = Object.assign(opts, {

View File

@@ -0,0 +1,25 @@
import os from 'os'
import { store } from '@pnpm/plugin-commands-store'
import prepare from '@pnpm/prepare'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
const REGISTRY = `http://localhost:${REGISTRY_MOCK_PORT}/`
test('CLI prints the current store path', async () => {
prepare()
const candidateStorePath = await store.handler({
dir: process.cwd(),
rawConfig: {
registry: REGISTRY,
},
registries: { default: REGISTRY },
storeDir: '/home/example/.pnpm-store',
}, ['path'])
const expectedStorePath = os.platform() === 'win32'
? '\\home\\example\\.pnpm-store\\v3'
: '/home/example/.pnpm-store/v3'
expect(candidateStorePath).toBe(expectedStorePath)
})