Skip to content

Commit

Permalink
feat: save the metadata separately from the CAFS (#3578)
Browse files Browse the repository at this point in the history
ref #2574
  • Loading branch information
zkochan committed Jul 4, 2021
1 parent b3478c7 commit 691f647
Show file tree
Hide file tree
Showing 44 changed files with 320 additions and 157 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-years-tap.md
@@ -0,0 +1,5 @@
---
"@pnpm/client": minor
---

New config setting added: cacheDir.
19 changes: 19 additions & 0 deletions .changeset/orange-days-brake.md
@@ -0,0 +1,19 @@
---
"@pnpm/client": major
"@pnpm/default-resolver": major
"@pnpm/npm-resolver": major
"@pnpm/outdated": major
"@pnpm/plugin-commands-audit": major
"@pnpm/plugin-commands-import": major
"@pnpm/plugin-commands-installation": major
"@pnpm/plugin-commands-listing": major
"@pnpm/plugin-commands-outdated": major
"@pnpm/plugin-commands-publishing": major
"@pnpm/plugin-commands-rebuild": major
"@pnpm/plugin-commands-script-runners": major
"@pnpm/plugin-commands-store": major
"@pnpm/store-connection-manager": major
"supi": minor
---

New required option added: cacheDir.
4 changes: 2 additions & 2 deletions packages/client/test/index.ts
Expand Up @@ -4,15 +4,15 @@ import createClient, { createResolver } from '@pnpm/client'
test('createClient()', () => {
const client = createClient({
authConfig: { registry: 'https://registry.npmjs.org/' },
storeDir: '',
cacheDir: '',
})
expect(typeof client === 'object').toBeTruthy()
})

test('createResolver()', () => {
const resolver = createResolver({
authConfig: { registry: 'https://registry.npmjs.org/' },
storeDir: '',
cacheDir: '',
})
expect(typeof resolver === 'function').toBeTruthy()
})
1 change: 1 addition & 0 deletions packages/config/src/Config.ts
Expand Up @@ -89,6 +89,7 @@ export interface Config {
alwaysAuth?: boolean

// pnpm specific configs
cacheDir: string
storeDir?: string
virtualStoreDir?: string
verifyStoreIntegrity?: boolean
Expand Down
20 changes: 20 additions & 0 deletions packages/config/src/getCacheDir.ts
@@ -0,0 +1,20 @@
import os from 'os'
import path from 'path'

export default function getCacheDir (
opts: {
env: NodeJS.ProcessEnv
platform: string
}
) {
if (opts.env.XDG_CACHE_HOME) {
return path.join(opts.env.XDG_CACHE_HOME, 'pnpm')
}
if (opts.platform !== 'win32') {
return path.join(os.homedir(), '.cache/pnpm')
}
if (opts.env.LOCALAPPDATA) {
return path.join(opts.env.LOCALAPPDATA, 'pnpm-cache')
}
return path.join(os.homedir(), '.pnpm-cache')
}
4 changes: 4 additions & 0 deletions packages/config/src/index.ts
Expand Up @@ -13,6 +13,7 @@ import realpathMissing from 'realpath-missing'
import whichcb from 'which'
import getScopeRegistries, { normalizeRegistry } from './getScopeRegistries'
import findBestGlobalPrefix from './findBestGlobalPrefix'
import getCacheDir from './getCacheDir'
import {
Config,
ConfigWithDeprecatedSettings,
Expand Down Expand Up @@ -358,6 +359,9 @@ export default async (
warnings.push('The "store" setting has been renamed to "store-dir". Please use the new name.')
pnpmConfig.storeDir = pnpmConfig['store']
}
if (!pnpmConfig.cacheDir) {
pnpmConfig.cacheDir = getCacheDir(process)
}
if (pnpmConfig['hoist'] === false) {
delete pnpmConfig.hoistPattern
}
Expand Down
26 changes: 26 additions & 0 deletions packages/config/test/getCacheDir.test.ts
@@ -0,0 +1,26 @@
import os from 'os'
import path from 'path'
import getCacheDir from '../lib/getCacheDir'

test('getCacheDir()', () => {
expect(getCacheDir({
env: {
XDG_CACHE_HOME: '/home/foo/cache',
},
platform: 'linux',
})).toBe(path.join('/home/foo/cache', 'pnpm'))
expect(getCacheDir({
env: {},
platform: 'linux',
})).toBe(path.join(os.homedir(), '.cache/pnpm'))
expect(getCacheDir({
env: {
LOCALAPPDATA: '/localappdata',
},
platform: 'win32',
})).toBe(path.join('/localappdata', 'pnpm-cache'))
expect(getCacheDir({
env: {},
platform: 'win32',
})).toBe(path.join(os.homedir(), '.pnpm-cache'))
})
2 changes: 1 addition & 1 deletion packages/default-resolver/test/index.ts
Expand Up @@ -5,7 +5,7 @@ import { createFetchFromRegistry } from '@pnpm/fetch'
test('createResolver()', () => {
const getCredentials = () => ({ authHeaderValue: '', alwaysAuth: false })
const resolve = createResolver(createFetchFromRegistry({}), getCredentials, {
storeDir: '.store',
cacheDir: '.cache',
})
expect(typeof resolve).toEqual('function')
})
7 changes: 5 additions & 2 deletions packages/headless/test/utils/testDefaults.ts
@@ -1,3 +1,4 @@
import path from 'path'
import createClient from '@pnpm/client'
import { HeadlessOptions } from '@pnpm/headless'
import createStore from '@pnpm/package-store'
Expand All @@ -22,7 +23,9 @@ export default async function testDefaults (
fetchOpts?: any, // eslint-disable-line
storeOpts?: any, // eslint-disable-line
): Promise<HeadlessOptions> {
let storeDir = opts?.storeDir ?? tempy.directory()
const tmp = tempy.directory()
let storeDir = opts?.storeDir ?? path.join(tmp, 'store')
const cacheDir = path.join(tmp, 'cache')
const lockfileDir = opts?.lockfileDir ?? process.cwd()
const { include, pendingBuilds, projects, registries } = await readProjectsContext(
[
Expand All @@ -37,7 +40,7 @@ export default async function testDefaults (
const { resolve, fetchers } = createClient({
authConfig,
retry: retryOpts,
storeDir,
cacheDir,
...resolveOpts,
...fetchOpts,
})
Expand Down
8 changes: 4 additions & 4 deletions packages/npm-resolver/src/index.ts
Expand Up @@ -54,7 +54,7 @@ const META_DIR = 'metadata'
const FULL_META_DIR = 'metadata-full'

export interface ResolverFactoryOptions {
storeDir: string
cacheDir: string
fullMetadata?: boolean
offline?: boolean
preferOffline?: boolean
Expand All @@ -67,8 +67,8 @@ export default function createResolver (
getCredentials: GetCredentials,
opts: ResolverFactoryOptions
) {
if (typeof opts.storeDir !== 'string') { // eslint-disable-line
throw new TypeError('`opts.storeDir` is required and needs to be a string')
if (typeof opts.cacheDir !== 'string') { // eslint-disable-line
throw new TypeError('`opts.cacheDir` is required and needs to be a string')
}
const fetchOpts = {
retry: opts.retry ?? {},
Expand All @@ -91,7 +91,7 @@ export default function createResolver (
metaDir: opts.fullMetadata ? FULL_META_DIR : META_DIR,
offline: opts.offline,
preferOffline: opts.preferOffline,
storeDir: opts.storeDir,
cacheDir: opts.cacheDir,
}),
})
}
Expand Down
4 changes: 2 additions & 2 deletions packages/npm-resolver/src/pickPackage.ts
Expand Up @@ -57,7 +57,7 @@ export default async (
fetch: (pkgName: string, registry: string, authHeaderValue?: string) => Promise<PackageMeta>
metaDir: string
metaCache: PackageMetaCache
storeDir: string
cacheDir: string
offline?: boolean
preferOffline?: boolean
},
Expand All @@ -77,7 +77,7 @@ export default async (
}

const registryName = getRegistryName(opts.registry)
const pkgMirror = path.join(ctx.storeDir, ctx.metaDir, registryName, `${encodePkgName(spec.name)}.json`)
const pkgMirror = path.join(ctx.cacheDir, ctx.metaDir, registryName, `${encodePkgName(spec.name)}.json`)
const limit = metafileOperationLimits[pkgMirror] = metafileOperationLimits[pkgMirror] || pLimit(1)

let metaCachedInStore: PackageMeta | null | undefined
Expand Down

0 comments on commit 691f647

Please sign in to comment.